# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

###############################################################################################
# Setup application name
###############################################################################################

set(APPLICATION_NAME qmt_rgb2pct)

###############################################################################################
# All source files needed to compile
###############################################################################################
set( SRCS
    main.cpp
    CApp.cpp
)

set( HDRS
    version.h
    CApp.h
)

set( UIS
)

set( RCS
)


###############################################################################################
# Some Qt magic
###############################################################################################

qt6_wrap_ui(UI_HDRS ${UIS})
qt6_add_resources(RC_SRCS ${RCS})


###############################################################################################
# Build source file and include paths lists
###############################################################################################
set(MAININP
    ${SRCS}
    ${HDRS}
    ${UI_HDRS}
    ${RC_SRCS}
    ${${APPLICATION_NAME}_QM_FILES}
    ${${APPLICATION_NAME}_DESKTOP_FILES}
)

include_directories(
    SYSTEM # this prevents warnings from non-QMS headers
    ${CMAKE_BINARY_DIR}
    ${GDAL_INCLUDE_DIRS}
    ${PROJ_INCLUDE_DIRS}
)

if(APPLE)
     INCLUDE_DIRECTORIES(/System/Library/Frameworks/Foundation.framework)
     INCLUDE_DIRECTORIES(/System/Library/Frameworks/DiskArbitration.framework)
endif(APPLE)


###############################################################################################
# Build the executable and define necessary libraries.
###############################################################################################
add_executable(${APPLICATION_NAME} WIN32 ${MAININP})

if(${UPDATE_TRANSLATIONS_PURGE_OBSOLETE})
    set(UPDATE_OPTIONS -noobsolete)
else()
    set(UPDATE_OPTIONS )
endif()
set( TRANSLATIONS
    locale/${APPLICATION_NAME}.ts
    locale/${APPLICATION_NAME}_de.ts
)

if(${UPDATE_TRANSLATIONS})
    qt_add_translations(${APPLICATION_NAME}
        TS_FILES ${TRANSLATIONS}
        QM_FILES_OUTPUT_VARIABLE ${APPLICATION_NAME}_QM_FILES
        LUPDATE_TARGET ${APPLICATION_NAME}_lupdate
        SOURCE_TARGETS ${APPLICATION_NAME}
        LUPDATE_OPTIONS ${UPDATE_OPTIONS}
    )
    add_dependencies(${APPLICATION_NAME} ${APPLICATION_NAME}_lupdate)
else(${UPDATE_TRANSLATIONS})
    qt_add_lrelease(
        TS_FILES  ${TRANSLATIONS}
        QM_FILES_OUTPUT_VARIABLE ${APPLICATION_NAME}_QM_FILES
    )
    qt_add_resources(${APPLICATION_NAME} "translations"
        PREFIX "/locale"
        BASE "${CMAKE_CURRENT_BINARY_DIR}"
        FILES "${${APPLICATION_NAME}_QM_FILES}"
    )
endif(${UPDATE_TRANSLATIONS})


target_compile_definitions(${APPLICATION_NAME} PUBLIC
    -DVER_MAJOR=${PROJECT_VERSION_MAJOR}
    -DVER_MINOR=${PROJECT_VERSION_MINOR}
    -DVER_STEP=${PROJECT_VERSION_PATCH}
    -DVER_TWEAK=${VERSION_SUFFIX}
    -DAPPLICATION_NAME=${APPLICATION_NAME}
)

target_link_libraries(${APPLICATION_NAME}
    Qt6::Core
    ${GDAL_LIBRARIES}
    ${PROJ_LIBRARIES}
)

if(APPLE)
     target_link_libraries(${APPLICATION_NAME}
     ${Foundation_LIBRARY}
     ${DiskArbitration_LIBRARY}
    )
endif(APPLE)


###############################################################################################
# Install target related stuff
###############################################################################################
install(TARGETS     ${APPLICATION_NAME}                     DESTINATION     ${BIN_INSTALL_DIR})

if (UNIX AND NOT WIN32 AND NOT APPLE)
    install(FILES   ${${APPLICATION_NAME}_QM_FILES}         DESTINATION     ${DATA_INSTALL_PREFIX}/${APPLICATION_NAME}/translations)
    install(FILES   ${${APPLICATION_NAME}_DESKTOP_FILES}    DESTINATION     ${XDG_APPS_DIR})
endif (UNIX AND NOT WIN32 AND NOT APPLE)
if(WIN32)
    qt_generate_deploy_app_script(
        TARGET ${APPLICATION_NAME}
        OUTPUT_SCRIPT deploy_script_${APPLICATION_NAME}
        NO_UNSUPPORTED_PLATFORM_ERROR
    )
    install(SCRIPT ${deploy_script_${APPLICATION_NAME}})
endif()
