cmake_minimum_required(VERSION 3.20)
option(BUILD_DOC    "Build documentation"    OFF)
option(USE_NETCDF "Enable netcdf library for tests output" OFF)
option(WITH_RRTMG "with(out) RRTMG radiation transfer" OFF)
option(WITH_TESTS "Generate and compile test cases" OFF)

#project cmake includes
include(FetchContent)
# Project main definitions
project(scm_abl)
enable_language(Fortran)
enable_language(C)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/modules)

if(BUILD_DOC)
    find_package(Doxygen)
    if (DOXYGEN_FOUND)
        set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/doxygen/config)    # doxygen config file

        # option ALL allows to build the docs together with the code
        add_custom_target( pbl_doc ALL
                COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_IN}
                WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} ) # WORKING_DIRECTORY: Execute the command with the given current working directory. If it is a relative path it will be interpreted relative to the build tree directory corresponding to the current source directory.
    else()
        message("Doxygen need to be installed to generate the doxygen documentation")
    endif (DOXYGEN_FOUND)
endif(BUILD_DOC)


set(lib_files
    src/parkinds.f90
    src/pbl_grid.f90
    src/phys_fluid.f90
    src/state_utils.f90
    src/scm_state_data.f90
    src/pbl_turb_data.f90
    src/pbl_solver.f90
    src/fo_stability_functions.f90
    src/pbl_fo_turb.f90
    src/pbl_turb_common.f90
    src/pbl_dry_contrgradient.f90
    src/diag_pbl.f90)

add_library(abl_lib ${lib_files})


target_compile_options(abl_lib
        PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -cpp>)
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
        target_compile_options(abl_lib
                PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -traceback -check all -ftrapuv -debug all >)
    elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        target_compile_options(abl_lib
                PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -O0 -fbacktrace -ffpe-trap=zero,overflow,underflow >)
    endif()
endif()

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
        target_compile_options(abl_lib
                PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -traceback -check all -ftrapuv -debug all >)
    elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
        target_compile_options(abl_lib
                PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -fbacktrace -ffpe-trap=zero,overflow,underflow >)
    endif()
endif()

if (WITH_TESTS)
    # RRTMG option
    if (WITH_RRTMG)
        add_definitions(-DUSE_RRTMG)
        add_subdirectory(radiation)
    endif()
    # adding sfx and config-parser stuff
    #fetch sfx model
    FetchContent_Declare(
            sfxlib
            GIT_REPOSITORY http://tesla.parallel.ru/inmcm-mirror/sfx.git
            GIT_TAG        origin/main)
    FetchContent_MakeAvailable(sfxlib)
    FetchContent_GetProperties(sfxlib)


    #Config parser from sfx_lib is needed
    if (WITH_RRTMG)
        list(APPEND test_files  src/rrtm_interface.f90)
    endif()
    #gabls1 experiment
    add_executable(gabls1 src/tests/gabls1.f90
            src/config-utils.f90
            src/scm_io_default.f90
            src/scm_sfx_data.f90)
    target_include_directories(gabls1 PUBLIC ${CMAKE_BINARY_DIR}/modules/)
    target_link_libraries( gabls1 PRIVATE abl_lib )
    target_link_libraries(gabls1 PRIVATE sfx_lib)
    target_link_libraries(gabls1 PRIVATE config_parser_F config_parser_CXX)

    #gabls2 experiment
    add_executable(gabls2 src/tests/gabls2.f90
            src/config-utils.f90
            src/scm_io_default.f90
            src/scm_sfx_data.f90)
    target_include_directories(gabls2 PUBLIC ${CMAKE_BINARY_DIR}/modules/)
    target_link_libraries( gabls2 PRIVATE abl_lib )
    target_link_libraries(gabls2 PRIVATE sfx_lib)
    target_link_libraries(gabls2 PRIVATE config_parser_F config_parser_CXX)

    #cbl experiment
    add_executable(cbl_exp src/tests/cbl_exp.f90
            src/config-utils.f90
            src/scm_io_default.f90
            src/scm_sfx_data.f90)
    target_include_directories(cbl_exp PUBLIC ${CMAKE_BINARY_DIR}/modules/)
    target_link_libraries( cbl_exp PRIVATE abl_lib )
    target_link_libraries(cbl_exp PRIVATE sfx_lib)
    target_link_libraries(cbl_exp PRIVATE config_parser_F config_parser_CXX)
    if(WITH_RRTMG)
        target_link_libraries(gabls1 PRIVATE rrtm)
        target_link_libraries(gabls2 PRIVATE rrtm)
        target_link_libraries(cbl_exp PRIVATE rrtm)
    endif()
    target_compile_options(gabls1
            PRIVATE $<$<COMPILE_LANGUAGE:Fortran>: -cpp>)
    target_compile_options(gabls2
            PRIVATE $<$<COMPILE_LANGUAGE:Fortran>: -cpp>)
    target_compile_options(cbl_exp
            PRIVATE $<$<COMPILE_LANGUAGE:Fortran>: -cpp>)
    if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
        if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
            target_compile_options(gabls1
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -traceback -check all -ftrapuv -debug all >)
            target_compile_options(gabls2
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -traceback -check all -ftrapuv -debug all >)
            target_compile_options(cbl_exp
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -traceback -check all -ftrapuv -debug all >)
        elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
            target_compile_options(gabls1
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -O0 -fbacktrace -ffpe-trap=zero,overflow,underflow >)
            target_compile_options(gabls2
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -O0 -fbacktrace -ffpe-trap=zero,overflow,underflow >)
            target_compile_options(cbl_exp
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -O0 -fbacktrace -ffpe-trap=zero,overflow,underflow >)
        endif()
    endif()

    if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
        if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
            target_compile_options(gabls1
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -traceback -check all -ftrapuv -debug all >)
                target_compile_options(gabls2
                        PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -traceback -check all -ftrapuv -debug all >)
            target_compile_options(cbl_exp
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -traceback -check all -ftrapuv -debug all >)
        elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
            target_compile_options(gabls1
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -fbacktrace -ffpe-trap=zero,overflow,underflow >)
            target_compile_options(gabls2
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -fbacktrace -ffpe-trap=zero,overflow,underflow >)
            target_compile_options(cbl_exp
                    PUBLIC $<$<COMPILE_LANGUAGE:Fortran>: -g -fbacktrace -ffpe-trap=zero,overflow,underflow >)
        endif()
    endif()

endif()