# SPDX-FileCopyrightText: 2026 Jason Self <j@jxself.org>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Copyright (c) 2026  Jason Self <j@jxself.org>
#
# This file is part of Trizbort-qt.
#
# Trizbort-qt is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Trizbort-qt is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Trizbort-qt. If not, see <https://www.gnu.org/licenses/>.

cmake_minimum_required(VERSION 3.16)
project(trizbort-qt VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)   # compile help/help.qrc (built-in help) into the binary

# Bake the program version into a generated Version.h that the About box and the
# --version flag read. The version is declared once in project() above.
# Packagers can override it with -DTRIZBORT_VERSION=<x>; the Debian packaging
# passes the changelog's upstream version this way, keeping the .deb version
# authoritative and the build independent of git.
if(DEFINED TRIZBORT_VERSION AND NOT "${TRIZBORT_VERSION}" STREQUAL "")
    set(TRIZBORT_VERSION_STRING "${TRIZBORT_VERSION}")
else()
    set(TRIZBORT_VERSION_STRING "${PROJECT_VERSION}")
endif()
message(STATUS "Trizbort version: ${TRIZBORT_VERSION_STRING}")
configure_file(src/Version.h.in "${CMAKE_CURRENT_BINARY_DIR}/generated/Version.h" @ONLY)

find_package(Qt6 REQUIRED COMPONENTS Widgets Network Svg)

add_executable(trizbort-qt
    help/help.qrc
    packaging/app_icon.qrc   # embed the app icon SVG (see main.cpp setWindowIcon)
    src/HelpWindow.cpp
    src/main.cpp
    src/MapDocument.cpp
    src/DotNetColor.cpp
    src/TrizbortReader.cpp
    src/TrizbortWriter.cpp
    src/TranscriptAutomapper.cpp
    src/MapScene.cpp
    src/MapView.cpp
    src/MapRender.cpp
    src/MainWindow.cpp
    src/RoomItem.cpp
    src/ConnectionItem.cpp
    src/EditCommands.cpp
    src/RoomDialog.cpp
    src/ConnectionDialog.cpp
    src/SettingsDialog.cpp
    src/export/ExportModel.cpp
    src/export/ExporterFactory.cpp
    src/export/CodeExporter.cpp
    src/export/ZilExporter.cpp
    src/export/AlanExporter.cpp
    src/export/Inform6Exporter.cpp
    src/export/Inform7Exporter.cpp
    src/export/QuestExporter.cpp
)

target_include_directories(trizbort-qt PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/generated")
target_link_libraries(trizbort-qt PRIVATE Qt6::Widgets Qt6::Network Qt6::Svg)

# Platform-native application icon.
#
# The embedded SVG above (app_icon.qrc) gives the running program its
# window/taskbar/dock icon on every platform: setWindowIcon renders it on demand
# via Qt's SVG icon engine (hence the Qt6::Svg link). In addition, Windows and
# macOS want the icon baked into the executable/bundle itself
# The .ico and .icns are committed beside the SVG they are generated from
# (packaging/generate-icons.sh) so a from-source build on any platform needs no
# image tooling.
if(WIN32)
    # Compile a .rc that points the resource compiler at the .ico. The path is
    # made absolute at configure time so rc.exe finds it regardless of its cwd.
    set(TRIZBORT_ICON_PATH "${CMAKE_CURRENT_SOURCE_DIR}/packaging/trizbort-qt.ico")
    configure_file(packaging/trizbort-qt.rc.in
                   "${CMAKE_CURRENT_BINARY_DIR}/trizbort-qt.rc" @ONLY)
    target_sources(trizbort-qt PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/trizbort-qt.rc")
elseif(APPLE)
    # Build a .app bundle and drop the .icns into it as the bundle icon.
    set(TRIZBORT_ICNS "${CMAKE_CURRENT_SOURCE_DIR}/packaging/trizbort-qt.icns")
    target_sources(trizbort-qt PRIVATE "${TRIZBORT_ICNS}")
    set_source_files_properties("${TRIZBORT_ICNS}" PROPERTIES
        MACOSX_PACKAGE_LOCATION "Resources")
    set_target_properties(trizbort-qt PROPERTIES
        MACOSX_BUNDLE TRUE
        MACOSX_BUNDLE_BUNDLE_NAME "Trizbort-qt"
        MACOSX_BUNDLE_GUI_IDENTIFIER "org.jxself.trizbort-qt"
        MACOSX_BUNDLE_ICON_FILE "trizbort-qt.icns"
        MACOSX_BUNDLE_BUNDLE_VERSION "${TRIZBORT_VERSION_STRING}"
        MACOSX_BUNDLE_SHORT_VERSION_STRING "${TRIZBORT_VERSION_STRING}")
endif()

# Installation (FHS layout via GNUInstallDirs; used by `cmake --install` and by
# the Debian packaging).
include(GNUInstallDirs)

# RUNTIME covers the plain executable (GNU/Linux. Windows and the .deb); BUNDLE
# covers the macOS .app when MACOSX_BUNDLE is set above. CMake requires a
# BUNDLE DESTINATION as soon as the target can be a bundle, even though
# macOS binaries are produced with macdeployqt rather than `cmake --install`.
install(TARGETS trizbort-qt
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
        BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR})

install(FILES packaging/trizbort-qt.desktop
        DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
install(FILES packaging/trizbort-qt.svg
        DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/scalable/apps)
install(FILES packaging/trizbort-qt.xml
        DESTINATION ${CMAKE_INSTALL_DATADIR}/mime/packages)
install(FILES packaging/org.jxself.trizbort-qt.metainfo.xml
        DESTINATION ${CMAKE_INSTALL_DATADIR}/metainfo)
install(FILES packaging/trizbort-qt.1
        DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
