#[[
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/>.
]]

find_program(SH_EXE sh REQUIRED)

FetchContent_Declare(
    netlib_DCUHRE
    URL "http://calgo.acm.org/698.gz"
    URL_HASH SHA256=27d891c14cabaefddd7c9cd6d3a6a2ae5b60eafa70901c32914f019363fda501
    DOWNLOAD_NAME "dcuhre.shar.gz"
    DOWNLOAD_NO_EXTRACT TRUE
)
FetchContent_MakeAvailable(netlib_DCUHRE)
FetchContent_GetProperties(
    netlib_DCUHRE
    SOURCE_DIR DCUHRE_FORTRAN_DIR
)

set(
    DCUHRE_LIB_SINGLE_FILENAMES
    "s07hre"
    "s09hre"
    "s113re"
    "s132re"
    "sadhre"
    "schhre"
    "scuhre"
    "sfshre"
    "sinhre"
    "srlhre"
    "strhre"
)
set(
    DCUHRE_TEST_SINGLE_FILENAMES
    "stest1"
    "stest2"
    CACHE
    INTERNAL
    ""
)
set(
    DCUHRE_LIB_DOUBLE_FILENAMES
    "d07hre"
    "d09hre"
    "d113re"
    "d132re"
    "dadhre"
    "dchhre"
    "dcuhre"
    "dfshre"
    "dinhre"
    "drlhre"
    "dtrhre"
)
set(
    DCUHRE_TEST_DOUBLE_FILENAMES
    "dtest1"
    "dtest2"
    CACHE
    INTERNAL
    ""
)

set(
    DCUHRE_FORTRAN_FILENAMES
    ${DCUHRE_LIB_SINGLE_FILENAMES}
    ${DCUHRE_TEST_SINGLE_FILENAMES}
    ${DCUHRE_LIB_DOUBLE_FILENAMES}
    ${DCUHRE_TEST_DOUBLE_FILENAMES}
)
list(TRANSFORM DCUHRE_FORTRAN_FILENAMES APPEND ".f")
set(DCUHRE_FORTRAN_SOURCES ${DCUHRE_FORTRAN_FILENAMES})
list(TRANSFORM DCUHRE_FORTRAN_SOURCES PREPEND "${DCUHRE_FORTRAN_DIR}/")
set(DCUHRE_BYPRODUCTS "makefile" "READ.ME")
list(TRANSFORM DCUHRE_BYPRODUCTS PREPEND "${DCUHRE_FORTRAN_DIR}/")

add_custom_command(
    OUTPUT ${DCUHRE_FORTRAN_SOURCES}
    COMMAND ${GZIP_EXE} -dkc "dcuhre.shar.gz" | ${SH_EXE}
    COMMAND ${CMAKE_COMMAND} -E touch ${DCUHRE_FORTRAN_SOURCES} ${DCUHRE_BYPRODUCTS}
    DEPENDS "${DCUHRE_FORTRAN_DIR}/dcuhre.shar.gz"
    BYPRODUCTS ${DCUHRE_BYPRODUCTS}
    WORKING_DIRECTORY "${DCUHRE_FORTRAN_DIR}"
    COMMENT "Extracting gzipped shell archive"
    VERBATIM
)

set(DCUHRE_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")

set(
    DCUHRE_SOURCES
    ${DCUHRE_LIB_SINGLE_FILENAMES}
    ${DCUHRE_TEST_SINGLE_FILENAMES}
    ${DCUHRE_LIB_DOUBLE_FILENAMES}
    ${DCUHRE_TEST_DOUBLE_FILENAMES}
)
list(TRANSFORM DCUHRE_SOURCES APPEND ".c")
list(TRANSFORM DCUHRE_SOURCES PREPEND "${DCUHRE_SOURCE_DIR}/")

add_custom_command(
    OUTPUT ${DCUHRE_SOURCES}
    COMMAND Netlib::f2c -a -d${DCUHRE_SOURCE_DIR} ${DCUHRE_FORTRAN_FILENAMES}
    DEPENDS ${DCUHRE_FORTRAN_SOURCES}
    WORKING_DIRECTORY "${DCUHRE_FORTRAN_DIR}"
    COMMENT "Converting DCUHRE from Fortran to C"
    VERBATIM
)

add_library(DCUHRE)
add_library(Netlib::DCUHRE ALIAS DCUHRE)

target_compile_features(DCUHRE PRIVATE c_std_90)
target_compile_options(DCUHRE PRIVATE -w)
target_link_libraries(DCUHRE PUBLIC Netlib::libf2c)

set_target_properties(
    DCUHRE PROPERTIES
    VERSION ${CalgoIntegrators_VERSION}
    SOVERSION ${CalgoIntegrators_VERSION_MAJOR}
    RELEASE_POSTFIX "${CALGO_INTEGRATORS_RELEASE_POSTFIX}"
    DEBUG_POSTFIX "${CALGO_INTEGRATORS_DEBUG_POSTFIX}"
    C_STANDARD 90
    INSTALL_RPATH_USE_LINK_PATH TRUE
)

set(
    DCUHRE_LIB_SOURCES
    ${DCUHRE_LIB_SINGLE_FILENAMES}
    ${DCUHRE_LIB_DOUBLE_FILENAMES}
)
list(TRANSFORM DCUHRE_LIB_SOURCES APPEND ".c")
list(TRANSFORM DCUHRE_LIB_SOURCES PREPEND "${DCUHRE_SOURCE_DIR}/")

target_sources(DCUHRE PRIVATE ${DCUHRE_LIB_SOURCES})
target_sources(DCUHRE PUBLIC
    FILE_SET HEADERS
    BASE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}"
    FILES "${CMAKE_CURRENT_SOURCE_DIR}/dcuhre.h"
)

target_include_directories(DCUHRE PUBLIC
    "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
)
