cmake_minimum_required (VERSION 3.15)

project(Tenmon)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")

option(SANITIZE_ADDRESS_LEAK "Enable -fsanitize=address -fsanitize=leak" OFF)
if(SANITIZE_ADDRESS_LEAK)
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address -fsanitize=leak")
endif(SANITIZE_ADDRESS_LEAK)

find_package(Qt6 COMPONENTS Widgets Sql OpenGLWidgets Qml Charts REQUIRED)
find_library(EXIF_LIB exif REQUIRED)
find_library(FITS_LIB cfitsio REQUIRED)
find_library(RAW_LIB NAMES raw_r REQUIRED)
find_library(WCS_LIB wcs wcslib REQUIRED)
find_library(LCMS2_LIB lcms2 REQUIRED)
find_library(STELLARSOLVER_LIB NAMES stellarsolver stellarsolver6)

add_subdirectory(libXISF)

set(TENMON_SRC
    src/about.cpp src/about.h
    src/batchprocessing.cpp src/batchprocessing.h src/batchprocessing.ui
    src/chartgraph.h src/chartgraph.cpp
    src/database.cpp src/database.h
    src/databaseview.cpp src/databaseview.h
    src/delete.cpp
    src/filemanager.h src/filemanager.cpp src/filemanager.ui
    src/filesystemwidget.cpp src/filesystemwidget.h
    src/fitskeyword.ui
    src/histogram.cpp src/histogram.h
    src/httpdownloader.h src/httpdownloader.cpp
    src/imageinfo.cpp src/imageinfo.h
    src/imageinfodata.cpp src/imageinfodata.h
    src/imageringlist.cpp src/imageringlist.h
    src/imagescrollarea.cpp src/imagescrollarea.h
    src/imagewidget.h src/imagewidget.cpp
    src/loadimage.h src/loadimage.cpp
    src/loadrunable.cpp src/loadrunable.h
    src/main.cpp
    src/mainwindow.cpp src/mainwindow.h
    src/markedfiles.cpp src/markedfiles.h
    src/mtfparam.h
    src/rawimage.cpp src/rawimage.h
    src/rawimage_sse.cpp
    src/scriptengine.cpp src/scriptengine.h
    src/settingsdialog.cpp src/settingsdialog.h
    src/statusbar.cpp src/statusbar.h
    src/stfslider.cpp src/stfslider.h
    src/stretchtoolbar.cpp src/stretchtoolbar.h
    src/tfloat16.h
    thumbnailer/genthumbnail.cpp thumbnailer/genthumbnail.h
)

qt_add_resources(TENMON_SRC resources/resources.qrc)
qt_add_resources(TENMON_SRC shaders/shaders.qrc)
qt_add_resources(TENMON_SRC scripts/scripts.qrc)
if(WIN32)
    list(APPEND TENMON_SRC resources/icon.rc)
    set(tenmon_ICON "")
elseif(APPLE)
    set(tenmon_ICON ${CMAKE_CURRENT_SOURCE_DIR}/resources/tenmon.icns)
    find_package(Qt6 COMPONENTS DBus REQUIRED)
    set_source_files_properties(${tenmon_ICON} PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
else()
    set(tenmon_ICON "")
    find_package(Qt6 COMPONENTS DBus REQUIRED)
endif()

qt_add_executable(tenmon WIN32 MACOSX_BUNDLE ${tenmon_ICON} ${TENMON_SRC})

find_path(FITS_INCLUDE fitsio2.h PATH_SUFFIXES cfitsio REQUIRED)
target_include_directories(tenmon PRIVATE ${FITS_INCLUDE} ${CMAKE_BINARY_DIR} ${libXISF_SOURCE_DIR} "src")

option(COLOR_MANAGMENT "Enable sRGB framebuffer support for gamma correct images and color profiles support" ON)
if(COLOR_MANAGMENT)
    target_compile_definitions(tenmon PRIVATE "COLOR_MANAGMENT")
endif(COLOR_MANAGMENT)

find_path(STELLARSOLVER_INCLUDE stellarsolver.h PATH_SUFFIXES libstellarsolver)
if(STELLARSOLVER_INCLUDE AND STELLARSOLVER_LIB)
    target_include_directories(tenmon PRIVATE ${STELLARSOLVER_INCLUDE})
    if(MXE)
        find_library(GSL_LIB gsl REQUIRED)
        find_library(GSLCBLAS_LIB gslcblas REQUIRED)
        target_link_libraries(tenmon PRIVATE ${STELLARSOLVER_LIB} ${GSL_LIB} ${GSLCBLAS_LIB} boost_regex-mt-x64)
    else(MXE)
        target_link_libraries(tenmon PRIVATE ${STELLARSOLVER_LIB})
    endif(MXE)
    target_compile_definitions(tenmon PRIVATE "PLATESOLVER")
    target_sources(tenmon PRIVATE
        src/solver.cpp src/solver.h
        src/platesolving.cpp src/platesolving.h src/platesolving.ui
        src/platesolvingsettings.cpp src/platesolvingsettings.h src/platesolvingsettings.ui
    )
    message(STATUS "Found stellarsolver ${STELLARSOLVER_INCLUDE} ${STELLARSOLVER_LIB}")
endif(STELLARSOLVER_INCLUDE AND STELLARSOLVER_LIB)

target_link_libraries(tenmon PRIVATE Qt6::Widgets Qt6::Sql Qt6::OpenGLWidgets Qt6::Qml Qt6::Charts ${EXIF_LIB} ${FITS_LIB} ${RAW_LIB} ${WCS_LIB} ${LCMS2_LIB} XISF)
if(APPLE)
    target_link_libraries(tenmon PRIVATE Qt6::DBus "-framework CoreFoundation")
elseif(UNIX)
    target_link_libraries(tenmon PRIVATE Qt6::DBus)
endif(APPLE)

if(LIBRAW_STATIC)
    add_compile_definitions("LIBRAW_NODLL")
    target_link_libraries(tenmon PRIVATE jasper)
endif()

option(FLATPAK "Flatpak build" OFF)
if(FLATPAK)
    target_compile_definitions(tenmon PRIVATE FLATPAK)
endif(FLATPAK)

install(TARGETS tenmon BUNDLE DESTINATION .)
if(UNIX AND NOT APPLE)
    include(GNUInstallDirs)
    find_program(XDG-DESKTOP-MENU_EXECUTABLE xdg-desktop-menu)
    if(XDG-DESKTOP-MENU_EXECUTABLE)
        install(SCRIPT install.cmake)
    else()
        install(FILES space.nouspiro.tenmon.desktop DESTINATION "${CMAKE_INSTALL_DATADIR}/applications")
        install(FILES resources/space.nouspiro.tenmon.png DESTINATION "${CMAKE_INSTALL_DATADIR}/icons/hicolor/64x64/apps")
        install(FILES resources/space.nouspiro.tenmon_128.png DESTINATION "${CMAKE_INSTALL_DATADIR}/icons/hicolor/128x128/apps" RENAME space.nouspiro.tenmon.png)
        install(FILES space.nouspiro.tenmon.xisf.xml DESTINATION "${CMAKE_INSTALL_DATADIR}/mime/packages")
    endif()
    install(FILES space.nouspiro.tenmon.metainfo.xml DESTINATION "${CMAKE_INSTALL_DATADIR}/metainfo")
endif(UNIX AND NOT APPLE)

option(RELEASE_BUILD "Release build" OFF)
if(RELEASE_BUILD)
    add_custom_target(tenmon_version COMMAND ${CMAKE_COMMAND} -Dlocal_dir="${CMAKE_CURRENT_SOURCE_DIR}" -Doutput_dir="${CMAKE_CURRENT_BINARY_DIR}"
        -P "${CMAKE_CURRENT_SOURCE_DIR}/gitversion.cmake")
    add_dependencies(tenmon tenmon_version)
else()
    execute_process(COMMAND ${CMAKE_COMMAND} -Dlocal_dir=${CMAKE_CURRENT_SOURCE_DIR} -Doutput_dir=${CMAKE_CURRENT_BINARY_DIR}
        -P "${CMAKE_CURRENT_SOURCE_DIR}/gitversion.cmake")
endif()

add_subdirectory(thumbnailer)
