#[[
CMake build script for ACM Collected Algorithms: DCUHRE and DQUAD
Copyright (C) 2024  Tim Kaune

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program.  If not, see
<https://www.gnu.org/licenses/>.
]]

include(CMakePackageConfigHelpers)

if (NOT DEFINED CalgoIntegrators_INSTALL_CMAKEDIR)
    set(
        CalgoIntegrators_INSTALL_CMAKEDIR
        "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}-${PROJECT_VERSION}"
        CACHE
        STRING
        "Path to Calgo Integrators CMake files"
    )
endif ()

target_include_directories(DCUHRE PUBLIC
    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)

target_include_directories(DQUAD PUBLIC
    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)

if (NOT NetlibF2C_FOUND)
    # NetlibF2C has been fetched into the same build tree
    set_property(
        TARGET DCUHRE DQUAD
        APPEND
        PROPERTY
        INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}"
    )
endif ()

if (BUILD_SHARED_LIBS)
    set(type shared)
else ()
    set(type static)
endif ()

install(
    TARGETS DCUHRE
    EXPORT CalgoIntegrators_DCUHRE_Targets
    LIBRARY COMPONENT dcuhre
    NAMELINK_COMPONENT dcuhre
    ARCHIVE COMPONENT dcuhre
    FILE_SET HEADERS COMPONENT dcuhre
    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

install(
    EXPORT CalgoIntegrators_DCUHRE_Targets
    DESTINATION "${CalgoIntegrators_INSTALL_CMAKEDIR}"
    FILE CalgoIntegrators_DCUHRE_Targets-${type}.cmake
    NAMESPACE Netlib::
    COMPONENT dcuhre
)

install(
    TARGETS DQUAD
    EXPORT CalgoIntegrators_DQUAD_Targets
    LIBRARY COMPONENT dquad
    NAMELINK_COMPONENT dquad
    ARCHIVE COMPONENT dquad
    FILE_SET HEADERS COMPONENT dquad
    DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

install(
    EXPORT CalgoIntegrators_DQUAD_Targets
    DESTINATION "${CalgoIntegrators_INSTALL_CMAKEDIR}"
    FILE CalgoIntegrators_DQUAD_Targets-${type}.cmake
    NAMESPACE Netlib::
    COMPONENT dquad
)

install(
    FILES
    "../3rdparty/ACM Software License Agreement.txt"
    RENAME copyright
    DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/doc/${PROJECT_NAME}-${PROJECT_VERSION}"
    COMPONENT dcuhre
)

install(
    FILES
    "../3rdparty/ACM Software License Agreement.txt"
    RENAME copyright
    DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/doc/${PROJECT_NAME}-${PROJECT_VERSION}"
    COMPONENT dquad
)

write_basic_package_version_file(
    CalgoIntegratorsConfigVersion.cmake
    COMPATIBILITY SameMajorVersion
)

file(READ "../cmake/HandlePolicies.cmake" HANDLE_POLICIES_SCRIPT OFFSET 1140)

if (DEFINED LoadStaticSharedTargets_SOURCE_DIR)
    cmake_path(APPEND CMAKE_INSTALL_PREFIX "${LoadStaticSharedTargets_INSTALL_CMAKEDIR}" OUTPUT_VARIABLE LoadStaticSharedTargets_DIR)
endif ()

if (DEFINED NetlibF2C_SOURCE_DIR)
    cmake_path(APPEND CMAKE_INSTALL_PREFIX "${NetlibF2C_INSTALL_CMAKEDIR}" OUTPUT_VARIABLE NetlibF2C_DIR)
endif ()

configure_package_config_file(
    "CalgoIntegratorsConfig.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/CalgoIntegratorsConfig.cmake"
    INSTALL_DESTINATION "${CalgoIntegrators_INSTALL_CMAKEDIR}"
    PATH_VARS LoadStaticSharedTargets_DIR NetlibF2C_DIR
)

install(
    FILES
    "${CMAKE_CURRENT_BINARY_DIR}/CalgoIntegratorsConfigVersion.cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/CalgoIntegratorsConfig.cmake"
    DESTINATION "${CalgoIntegrators_INSTALL_CMAKEDIR}"
    COMPONENT dcuhre
)

install(
    FILES
    "${CMAKE_CURRENT_BINARY_DIR}/CalgoIntegratorsConfigVersion.cmake"
    "${CMAKE_CURRENT_BINARY_DIR}/CalgoIntegratorsConfig.cmake"
    DESTINATION "${CalgoIntegrators_INSTALL_CMAKEDIR}"
    COMPONENT dquad
)
