cmake_minimum_required(VERSION 3.10)

project(tipp10)

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

if(WIN32)
    # add application icon
    set(WIN_RC_FILE tipp10.rc)
endif()

find_package(Qt6 6.4 COMPONENTS Core Multimedia PrintSupport Sql Widgets REQUIRED)

set(HDRS
    def/defines.h
    games/abcrainwidget.h
    games/charball.h
    sql/chartablesql.h
    sql/connection.h
    sql/keyboardsql.h
    sql/lessontablesql.h
    sql/startsql.h
    sql/trainingsql.h
    widget/errormessage.h
    widget/evaluationwidget.h
    widget/fingerwidget.h
    widget/helpbrowser.h
    widget/illustrationdialog.h
    widget/keyboard.h
    widget/lessondialog.h
    widget/lessonprintdialog.h
    widget/lessonresult.h
    widget/mainwindow.h
    widget/numpad.h
    widget/progressionwidget.h
    widget/regexpdialog.h
    widget/settings.h
    widget/startwidget.h
    widget/statusbar.h
    widget/tickerboard.h
    widget/trainingwidget.h
)

set(SRCS
    main.cpp
    games/abcrainwidget.cpp
    games/charball.cpp
    sql/chartablesql.cpp
    sql/keyboardsql.cpp
    sql/lessontablesql.cpp
    sql/startsql.cpp
    sql/trainingsql.cpp
    widget/errormessage.cpp
    widget/evaluationwidget.cpp
    widget/fingerwidget.cpp
    widget/helpbrowser.cpp
    widget/illustrationdialog.cpp
    widget/keyboard.cpp
    widget/lessondialog.cpp
    widget/lessonprintdialog.cpp
    widget/lessonresult.cpp
    widget/mainwindow.cpp
    widget/numpad.cpp
    widget/progressionwidget.cpp
    widget/regexpdialog.cpp
    widget/settings.cpp
    widget/startwidget.cpp
    widget/statusbar.cpp
    widget/tickerboard.cpp
    widget/trainingwidget.cpp
)

set(UI_FILES
    widget/regexpdialog.ui
    widget/settings.ui
)

set(QRC_FILES
    tipp10.qrc
)

set(TS_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/translations/tipp10_de.ts
    ${CMAKE_CURRENT_SOURCE_DIR}/translations/tipp10_en.ts
    ${CMAKE_CURRENT_SOURCE_DIR}/translations/tipp10_fr.ts
    ${CMAKE_CURRENT_SOURCE_DIR}/translations/tipp10_nb_NO.ts
    ${CMAKE_CURRENT_SOURCE_DIR}/translations/tipp10_nl.ts
    ${CMAKE_CURRENT_SOURCE_DIR}/translations/tipp10_pl.ts
    ${CMAKE_CURRENT_SOURCE_DIR}/translations/tipp10_ta.ts
    ${CMAKE_CURRENT_SOURCE_DIR}/translations/tipp10_uk.ts
)

add_executable(tipp10 ${SRCS} ${HDRS} ${QRC_FILES} ${UI_FILES} ${WIN_RC_FILE})
target_include_directories(tipp10 PRIVATE ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(tipp10 Qt6::Core Qt6::Multimedia Qt6::PrintSupport
    Qt6::Sql Qt6::Widgets
)
find_package(Qt6LinguistTools)
if(Qt6LinguistTools_FOUND)
    set_source_files_properties(${TS_FILES}
        PROPERTIES OUTPUT_LOCATION "${CMAKE_BINARY_DIR}/share/tipp10/translations"
    )
    qt6_add_translations(tipp10 TS_FILES ${TS_FILES} QM_FILES_OUTPUT_VARIABLE QM_FILES)
endif()

target_compile_definitions(tipp10 PRIVATE INSTALLPREFIX="${CMAKE_INSTALL_PREFIX}")
target_compile_options(tipp10 PRIVATE
     $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:GNU>>:
          -Wall -Wextra -Wpedantic>
     $<$<CXX_COMPILER_ID:MSVC>:
          /W4>)

set(SOUND_FILES
    error.wav
    metronome.wav
)

option(PORTABLE "Build portable version" OFF)
target_compile_definitions(tipp10 PRIVATE APP_PORTABLE=$<BOOL:${PORTABLE}>)

if(WIN32)
    target_compile_definitions(tipp10 PRIVATE APP_WIN)
elseif(UNIX)
    if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
        target_compile_definitions(tipp10 PRIVATE APP_MAC)
    else()
        target_compile_definitions(tipp10 PRIVATE APP_X11)
    endif()
endif()

if(WIN32)
    set_target_properties(tipp10 PROPERTIES WIN32_EXECUTABLE TRUE)
endif()

set_target_properties(tipp10 PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>/
)

file(COPY release/tipp10v2.template ${SOUND_FILES}
    DESTINATION ${CMAKE_BINARY_DIR}/share/tipp10/
)

install(TARGETS tipp10
    RUNTIME DESTINATION bin
)

install(FILES release/tipp10v2.template ${SOUND_FILES}
    DESTINATION share/tipp10)

install(FILES ${QM_FILES}
    DESTINATION share/tipp10/translations)

install(DIRECTORY release/help
    DESTINATION share/tipp10)


if(UNIX AND NOT APPLE)
    install(FILES com.gitlab.tipp10.tipp10.metainfo.xml
        DESTINATION share/metainfo)

    install(FILES com.gitlab.tipp10.tipp10.desktop
        DESTINATION share/applications)

    install(FILES com.gitlab.tipp10.tipp10.svg
        DESTINATION share/icons/hicolor/scalable/apps)
endif()
