#[[
CMake build script for ACM Collected Algorithms: DCUHRE and DQUAD
Copyright (C) 2025  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(GNUInstallDirs)
include(CMakePackageConfigHelpers)

if (NOT DEFINED CalgoIntegrators_INSTALL_CMAKEDIR)
    set(
        CalgoIntegrators_INSTALL_CMAKEDIR
        "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}-${PROJECT_VERSION}"
    )
endif ()

target_include_directories(DCUHRE PUBLIC
    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}-${PROJECT_VERSION}>"
)

target_include_directories(DQUAD PUBLIC
    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}-${PROJECT_VERSION}>"
)

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
    COMPONENT dcuhre
    FILE_SET HEADERS COMPONENT dcuhre DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}-${PROJECT_VERSION}"
)

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
    COMPONENT dquad
    FILE_SET HEADERS COMPONENT dquad DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}-${PROJECT_VERSION}"
)

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
)

if (DEFINED LoadStaticSharedTargets_SOURCE_DIR)
    set(LoadStaticSharedTargets_HINT_VAR "LoadStaticSharedTargets_ROOT")
    set(LoadStaticSharedTargets_HINT_PATH "")
else ()
    set(LoadStaticSharedTargets_HINT_VAR "LoadStaticSharedTargets_DIR")
    set(LoadStaticSharedTargets_HINT_PATH "${LoadStaticSharedTargets_DIR}")
endif ()

if (DEFINED NetlibF2C_SOURCE_DIR)
    set(NetlibF2C_HINT_VAR "NetlibF2C_ROOT")
    set(NetlibF2C_HINT_PATH "")
else ()
    set(NetlibF2C_HINT_VAR "NetlibF2C_DIR")
    set(NetlibF2C_HINT_PATH "${NetlibF2C_DIR}")
endif ()

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

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
)
