# ------------------------------------------------------------------------------------------------
# DIPELM library (to be shared by DIPELM and DIPOLE_TOOLS)

add_library(libdipelm
    "dipelm_procs.f90"
    "dipelm_smooth.f90"
    "special_functions.f90"
    "dipelm_types.f90"
    "dipelm_io.f90"
    "sorting.f90"
    "dipelm_defs.f90"
)

target_compile_options(libdipelm PUBLIC ${OpenMP_Fortran_FLAGS})

target_include_directories(libdipelm PUBLIC
    "${CMAKE_CURRENT_BINARY_DIR}/../compak/mod"
    "${CMAKE_CURRENT_BINARY_DIR}/../libouter/mod"
    "${CMAKE_CURRENT_BINARY_DIR}/../libutil/mod"
    ${GBTOLIB_INCLUDE_DIRS}
)

target_link_libraries(libdipelm
    libcompak
    libouter
    libutil
    ${GBTOLIB_LIBRARIES}
    ${LAPACK_LIBRARIES}
    ${BLAS_LIBRARIES}
)

set_target_properties(libdipelm PROPERTIES
    OUTPUT_NAME dipelm
    LINK_FLAGS "${OpenMP_Fortran_FLAGS}"
)

# ------------------------------------------------------------------------------------------------
# DIPELM program

add_executable(dipelm "dipelm.f90")

target_link_libraries(dipelm libdipelm)

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

# ------------------------------------------------------------------------------------------------
# DIPOLE_TOOLS program
#
# NOTE: At the moment compiled using old DIPELM source code due to significant changes between
#       the versions. This will be unified with the new DIPELM and eventually DIPOLE_TOOLS will
#       be directly linked to the DIPELM library in the same way as DIPELM.

add_executable(dipole_tools
    "old_dipelm/anglib.f90"
    "old_dipelm/angmom_procs.f90"
    "old_dipelm/dipelmdefs.f90"
    "old_dipelm/dipelmio.f90"
    "old_dipelm/dipelmprocs.f90"
    "old_dipelm/readsh.f"
    "dipole_tools.f90"
)

target_compile_options(dipole_tools PUBLIC ${OpenMP_Fortran_FLAGS})

target_include_directories(dipole_tools PUBLIC
    "${CMAKE_CURRENT_BINARY_DIR}/../compak/mod"
    "${CMAKE_CURRENT_BINARY_DIR}/../libouter/mod"
    "${CMAKE_CURRENT_BINARY_DIR}/../libutil/mod"
    ${GBTOLIB_INCLUDE_DIRS}
)

target_link_libraries(dipole_tools
    libcompak
    libouter
    libutil
    ${GBTOLIB_LIBRARIES}
    ${LAPACK_LIBRARIES}
    ${BLAS_LIBRARIES}
)

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

# ------------------------------------------------------------------------------------------------
# DIPELM documentation

if (DOXYGEN_FOUND)
    set(DOXYGEN_IN "${CMAKE_CURRENT_LIST_DIR}/doc/Doxyfile")

    add_custom_target(dipelm-doc ALL
        COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_IN}
        WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/doc"
        COMMENT "Generating documentation with Doxygen"
        VERBATIM)

    install(DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/doc/html/"
            DESTINATION "${CMAKE_INSTALL_DOCDIR}/dipelm-doxygen")
endif (DOXYGEN_FOUND)

# ------------------------------------------------------------------------------------------------
# Installation targets

install(TARGETS libdipelm dipelm dipole_tools
        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}/dipelm")
