cmake_minimum_required(VERSION 3.19)
include(FetchContent)

option(USE_NETCDF "NetCDF build in mode" OFF)
option(USE_SFX "Build with new sfx module"    ON)
option(USE_CONFIG_PARSER "Build config parser"    ON)
option(BUILD_DOC    "Build documentation"    OFF)

project(OceanVerticalMixing)
enable_language(Fortran)
#set new directories

if(NOT USE_NETCDF)
    add_definitions(-DOBL_EXCLUDE_NETCDF)
endif(NOT USE_NETCDF)

if(BUILD_DOC)
    find_package(Doxygen)
    if (DOXYGEN_FOUND)
        set(DOXYGEN_IN ../doxygen/config)    # doxygen config file

        # option ALL allows to build the docs together with the code
        add_custom_target( doc_doxygen ALL
            COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_IN}
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} )
    else()
    message("Doxygen need to be installed to generate the doxygen documentation")
    endif (DOXYGEN_FOUND)
endif(BUILD_DOC)

set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/modules/)

set(SOURCES
    obl_common.f90
    obl_grid.f90
    obl_tslice.f90
    obl_tseries.f90
    obl_tforcing.f90
    obl_state.f90
    obl_output.f90
    obl_common_phys.f90
    obl_math.f90
    obl_state_eq.f90
    obl_init.f90
    obl_bc.f90
    obl_turb_common.f90
    obl_diag.f90
    obl_k_epsilon.f90
    obl_pph.f90
    obl_pph_dyn.f90
    obl_fluxes.f90
    obl_scm.f90
    obl_main.f90
    io_metadata.f90
    io.f90
    obl_io_plt.f90
    obl_run_kato.f90
    obl_run_papa_fluxes.f90
    obl_run_papa_meteo.f90
    obl_run_cbl.f90
    obl_run_cyclone.f90
    obl_config.f90
    vermix_inmom.f90
)

set(HEADERS
    obl_def.fi
)

add_executable(obl ${SOURCES})

#Compilation keys
if (CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
    target_compile_options(obl PRIVATE $<$<COMPILE_LANGUAGE:Fortran>: -cpp -O2>)
elseif (CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
    target_compile_options(obl PRIVATE $<$<COMPILE_LANGUAGE:Fortran>: -cpp -O2>)
endif ()
IF (USE_SFX)
    #fetch sfx model
    FetchContent_Declare(
            inmcm60_sfx
            GIT_REPOSITORY http://tesla.parallel.ru/inmcm-mirror/sfx.git
            GIT_TAG        origin/main)
    FetchContent_MakeAvailable(inmcm60_sfx)
    FetchContent_GetProperties(inmcm60_sfx)
    if(NOT inmcm60_sfx_POPULATED)
        FetchContent_Populate(sfx_lib)
        add_subdirectory(${inmcm60_sfx_SOURCE_DIR} ${inmcm60_sfx_BINARY_DIR}  EXCLUDE_FROM_ALL)
    endif()
    add_definitions(-DUSE_SFX)
    if(USE_CONFIG)
    add_definitions(-DUSE_CONFIG_PARSER)
    endif (USE_CONFIG)
    target_link_libraries(obl PRIVATE sfx_lib)
else ()
    if(USE_CONFIG)
        message(ERROR "Config can be only used with new sfx enabled")
    endif (USE_CONFIG)
endif (USE_SFX)

add_library(ocevermix ocevermix.f90)

if(USE_NETCDF)
    #NetCDF handling
    execute_process (
            COMMAND bash -c "nf-config --fflags | grep -Eo '^[^ ]+' | tr -d '\n'"
            OUTPUT_VARIABLE netcdf_inc
    )
    cmake_path(SET netcfd_inc_dir " ${netcdf_inc}" )
    message("CMake path is ${netcdf_inc}")
    target_compile_options(obl PRIVATE ${netcdf_inc})
    include_directories(${netcdf_inc})
    execute_process (
            COMMAND bash -c "nf-config --flibs | tr -d '\n'"
            OUTPUT_VARIABLE temp
    )
    separate_arguments(netcdff_libs UNIX_COMMAND "${temp}")
    execute_process (
            COMMAND bash -c "nc-config --libs | tr -d '\n'"
            OUTPUT_VARIABLE temp
    )
    separate_arguments(netcdf_libs UNIX_COMMAND "${temp}")
    execute_process (
            COMMAND bash -c "nc-config --all | grep -Eo '\-\-static.*' | cut -d ' ' -f10- | tr -d '\n'"
            OUTPUT_VARIABLE temp
    )
    separate_arguments(netcdf_static UNIX_COMMAND "${temp}")
    #target_include_directories(lib_scm_io PRIVATE ${netCDF_INCLUDE_DIR})



    target_link_options(obl PRIVATE ${netcdff_libs})
    target_link_options(obl PRIVATE ${netcdf_libs})
    target_link_options(obl PRIVATE ${netcdf_static})
endif(USE_NETCDF)

add_custom_command(
        TARGET obl POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/meteo-init/
        ${CMAKE_CURRENT_BINARY_DIR}/meteo-init/)

add_custom_command(
        TARGET obl POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_SOURCE_DIR}/meteo-forcing/
        ${CMAKE_CURRENT_BINARY_DIR}/meteo-forcing/)