# ------------------------------------------------------------------------------------------------
# Outer programs

add_executable(outer     "outer.f90")
add_executable(outer-run "outer-run.f90")

target_compile_options(outer     PUBLIC ${OpenMP_Fortran_FLAGS})
target_compile_options(outer-run PUBLIC ${OpenMP_Fortran_FLAGS})

target_link_libraries(outer     libcompak libouter libutil libcouplings ${GBTOLIB_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})
target_link_libraries(outer-run libcompak libouter libutil libcouplings ${GBTOLIB_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})

set_target_properties(outer     PROPERTIES LINK_FLAGS "${OpenMP_Fortran_FLAGS}")
set_target_properties(outer-run PROPERTIES LINK_FLAGS "${OpenMP_Fortran_FLAGS}")

# ------------------------------------------------------------------------------------------------
# DCS, Rates & BORNCROS programs (etc)

add_executable(dcs "dcs.f")
add_executable(borncros "borncros.f")
add_executable(rates "rates.f90")
add_executable(photodelays  "photodelays.F90")

target_include_directories(photodelays PUBLIC
    "${CMAKE_CURRENT_BINARY_DIR}/../multidip/mod"
    "${CMAKE_CURRENT_BINARY_DIR}/../compak/mod"
    "${CMAKE_CURRENT_BINARY_DIR}/../libutil/mod"
    ${GBTOLIB_INCLUDE_DIRS}
)

set_target_properties(dcs PROPERTIES LINK_FLAGS "${OpenMP_Fortran_FLAGS}")
set_target_properties(borncros PROPERTIES LINK_FLAGS "${OpenMP_Fortran_FLAGS}")
set_target_properties(rates PROPERTIES LINK_FLAGS "${OpenMP_Fortran_FLAGS}")
set_target_properties(photodelays PROPERTIES LINK_FLAGS "${OpenMP_Fortran_FLAGS}")

target_link_libraries(dcs libouter libutil ${GBTOLIB_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})
target_link_libraries(borncros libouter libutil ${GBTOLIB_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})
target_link_libraries(photodelays libmultidip libcompak libouter libutil ${GBTOLIB_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})

# ------------------------------------------------------------------------------------------------
# RMT interface

add_executable(rmt_interface
    "../libouter/swinterf_rmt.f"
    "rmt_interface.f90"
)

target_compile_options(rmt_interface PUBLIC ${OpenMP_Fortran_FLAGS})

target_include_directories(rmt_interface PUBLIC
    "${CMAKE_CURRENT_BINARY_DIR}/../libouter/mod"
)

target_link_libraries(rmt_interface
    librmt_interface
    libouter
    libutil
    libcouplings
    ${GBTOLIB_LIBRARIES}
    ${LAPACK_LIBRARIES}
    ${BLAS_LIBRARIES}
)

set_target_properties(rmt_interface PROPERTIES LINK_FLAGS "${OpenMP_Fortran_FLAGS}")

# ------------------------------------------------------------------------------------------------
# Outer subprogram executables

set(outer_subprograms "swinterf" "eigenp" "tmatrx" "ixsecs" "reson"
                      "bound" "timedel" "rsolve" "mpi_rsolve" "pfarm_interface" "kmatadgeneral")

set(swinterf_PROC           "sw_interf")
set(eigenp_PROC             "eigen_p")
set(tmatrx_PROC             "t_matrx")
set(ixsecs_PROC             "i_xsecs")
set(reson_PROC              "reson_f")
set(bound_PROC              "bound_f")
set(timedel_PROC            "timedel")
set(rsolve_PROC             "r_solve_compak")
set(mpi_rsolve_PROC         "mpi_r_solve")
set(pfarm_interface_PROC    "pfarm_interface")
set(kmatadgeneral_PROC      "kmatadgeneral")

foreach(prog ${outer_subprograms})
    string(CONCAT prog_SRC
        "program ${prog}_launcher\n"
        "   use const_gbl, only: stdout\n"
        "   use mpi_gbl, only: mpi_mod_start\n"
        "   use ukrmol_interface_gbl, only: destroy_ukrmolp, finalize_mpi\n"
        "   logical, parameter :: master_writes_to_stdout = .false.\n"
        "   logical, parameter :: use_shared_memory = .true.\n"
        "   integer :: ifail = 0\n"
        "   call print_ukrmol_header(stdout)\n"
        "   call mpi_mod_start(master_writes_to_stdout, use_shared_memory)\n"
        "   call ${${prog}_PROC}(ifail)\n"
        "   call destroy_ukrmolp\n"
        "   call finalize_mpi\n"
        "   if (ifail /= 0) stop 1\n"
        "end program\n")
    file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${prog}.f90" CONTENT ${prog_SRC})
    add_executable(${prog} "${CMAKE_CURRENT_BINARY_DIR}/${prog}.f90")
    set_target_properties(${prog} PROPERTIES LINK_FLAGS "${OpenMP_Fortran_FLAGS}")
    target_link_libraries(${prog} libcompak libouter libutil ${GBTOLIB_LIBRARIES} ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES})
endforeach()

# ------------------------------------------------------------------------------------------------
# Installation target

install(TARGETS outer outer-run rmt_interface photodelays ${outer_subprograms} dcs borncros rates
        RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
        ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
        LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_Fortran_MODULE_DIRECTORY}/"
        DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/outer")
