set(SIXTE_CMAKE_VERSION 3.16)
cmake_minimum_required(VERSION ${SIXTE_CMAKE_VERSION})

project(sixte VERSION 3.3.1)

include(CTest)
include(GNUInstallDirs)

message(STATUS "CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}")


# Throw an error if CMAKE_INSTALL_PREFIX is not set by the user.
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    message(FATAL_ERROR "CMAKE_INSTALL_PREFIX not set!\n"
            "Use -DCMAKE_INSTALL_PREFIX=sixtedir to specify the SIXTE installation directory.")
endif()

# Use for memory debug only!
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer")

### Global compiler flags ###
# TODO: Add -pedantic
set(GLOBAL_COMPILER_FLAGS
        -g -O2 -D_GNU_SOURCE -Wall -Wextra -Wshadow -Wformat=2 -Wunused)
add_library(compiler_flags INTERFACE)
target_compile_features(compiler_flags INTERFACE c_std_99 cxx_std_17)
target_compile_options(compiler_flags INTERFACE
        $<BUILD_INTERFACE:${GLOBAL_COMPILER_FLAGS}>)


### SIMPUT ###
# By default, the SIMPUT installation is expected to be located at
# CMAKE_INSTALL_PREFIX. If SIMPUT is installed to a different location, an
# alternative path can be set via the SIMPUT_ROOT variable.
# Usage: cmake -DSIMPUT_ROOT=simputdir).
set(SIMPUT_ROOT ${CMAKE_INSTALL_PREFIX} CACHE STRING "Directory that contains the SIMPUT installation")
set(SIMPUT_INCLUDE_DIR ${SIMPUT_ROOT}/${CMAKE_INSTALL_INCLUDEDIR})
set(SIMPUT_LIBRARY_DIR ${SIMPUT_ROOT}/${CMAKE_INSTALL_LIBDIR})
set(SIMPUT_LIBRARIES ape_simput atFunctions_simput cfitsio_simput fftw3_simput heainit_simput heaio_simput heasp_simput heautils_simput labnh posstring simput wcs_simput)
find_library(SIMPUT_LIBRARY
        NAMES ${SIMPUT_LIBRARIES}
        PATHS ${SIMPUT_LIBRARY_DIR})
if(NOT SIMPUT_LIBRARY)
    message(FATAL_ERROR "Could not find the SIMPUT libraries at \n${SIMPUT_LIBRARY_DIR}\n"
            "Use -DSIMPUT_ROOT=simputdir to specify SIMPUT installation directory.")
endif()
message(STATUS "Using SIMPUT at ${SIMPUT_ROOT}")


### External packages ###
set(EXT_INCLUDE_DIRS)
set(EXT_LIBRARIES)

# GSL
find_package(GSL REQUIRED)
list(APPEND EXT_INCLUDE_DIRS ${GSL_INCLUDE_DIRS})
list(APPEND EXT_LIBRARIES ${GSL_LIBRARIES})

# Boost
# For CMake 3.30+, set CMP0167=NEW to suppress “FindBoost removed” warning and adopt the new behavior.
if(POLICY CMP0167)
    cmake_policy(SET CMP0167 NEW)
endif()
find_package(Boost REQUIRED)
list(APPEND EXT_INCLUDE_DIRS ${Boost_INCLUDE_DIRS})
list(APPEND EXT_LIBRARIES ${Boost_LIBRARIES})

# Threads
find_package(Threads REQUIRED)
list(APPEND EXT_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})

# CGAL
set(CGAL_DO_NOT_WARN_ABOUT_CMAKE_BUILD_TYPE TRUE)
# Suppress warning about missing CGAL_DATA_DIR by setting to a dummy path.
set(CGAL_DATA_DIR "/dev/null")
find_package(CGAL REQUIRED)
list(APPEND EXT_LIBRARIES CGAL::CGAL)

# CURSES
set(CURSES_NEED_NCURSES TRUE)
find_package(Curses REQUIRED)
list(APPEND EXT_INCLUDE_DIRS ${CURSES_INCLUDE_DIRS})
list(APPEND EXT_LIBRARIES ${CURSES_LIBRARIES})
message(STATUS "Using CURSES_INCLUDE_DIRS at ${CURSES_INCLUDE_DIRS}")
message(STATUS "Using CURSES_LIBRARIES at ${CURSES_LIBRARIES}")

# EXPAT
find_package(EXPAT REQUIRED)
list(APPEND EXT_INCLUDE_DIRS ${EXPAT_INCLUDE_DIRS})
list(APPEND EXT_LIBRARIES ${EXPAT_LIBRARIES})

# SIXTE extlibs
list(APPEND EXT_INCLUDE_DIRS
        ${CMAKE_SOURCE_DIR}/extlib/CCfits
        ${CMAKE_SOURCE_DIR}/extlib/linterp
        ${CMAKE_SOURCE_DIR}/extlib/atmsp
        ${CMAKE_SOURCE_DIR}/extlib/region
        ${CMAKE_SOURCE_DIR}/extlib/progressbar/include/progressbar
        ${CMAKE_CURRENT_SOURCE_DIR}/embree/include)
list(APPEND EXT_LIBRARIES CCfits progressbar pugixml embree)

# Additional libraries
list(APPEND EXT_LIBRARIES m readline)

# GCC < 9 requires explicit linking against stdc++fs for std::filesystem
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9")
    list(APPEND EXT_LIBRARIES stdc++fs)
endif()

### Configured header file ###
configure_file(sixteconfig.h.in sixteconfig.h)
install(FILES "${PROJECT_BINARY_DIR}/sixteconfig.h" DESTINATION include)


### Install sixte-install.sh/csh scripts ###
configure_file(sixte-install.sh.in sixte-install.sh @ONLY)
configure_file(sixte-install.csh.in sixte-install.csh @ONLY)
install(FILES "${PROJECT_BINARY_DIR}/sixte-install.sh"
              "${PROJECT_BINARY_DIR}/sixte-install.csh"
        DESTINATION ${CMAKE_INSTALL_BINDIR})

# test reference files
set(TESTDATA_DIR ${CMAKE_INSTALL_PREFIX}/share/sixte/testdata CACHE STRING "Directory that contains e2e test reference data")

# manually add libraries to RPATH -- fixes some MacOS issues
list( APPEND CMAKE_INSTALL_RPATH ${SIMPUT_LIBRARY_DIR} ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR})

### Project subdirectories ###
add_subdirectory(extlib)
add_subdirectory(libsixt)
add_subdirectory(libsixtcpp)
add_subdirectory(tools)
add_subdirectory(test/tests)

### Packaging ###
set(CPACK_SOURCE_IGNORE_FILES
        /\\\\.gitignore$ /\\\\.idea/ /\\\\.git/ /\\\\.gitlab-ci.yml$
        /\\\\.gitmodules$ /\\\\.tags$ /\\\\.tags1$
        /build/ /cmake-build-debug/ /install/ /output/
        HOWTO_add_tools.txt programming_standards.txt)
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME}-${CMAKE_PROJECT_VERSION}")
set(CPACK_SOURCE_GENERATOR "TGZ")
include(CPack)

#if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
#        install(
#                SCRIPT ${CMAKE_SOURCE_DIR}/mac_solve_sixte_dependencies.sh
#        )
#endif()


