cmake_minimum_required(VERSION 3.16)

project(BareCode
    VERSION 1.0.0
    DESCRIPTION "A modular code editor built with Qt6"
    LANGUAGES CXX
)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

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

# ---------------------------------------------------------------------------
# Plattform-Erkennung
# Haiku definiert kein eigenes CMake-Flag — wir erkennen es über den
# Systemnamen. CMAKE_SYSTEM_NAME ist "Haiku" auf Haiku OS.
# ---------------------------------------------------------------------------
if(WIN32)
    set(PLATFORM_WINDOWS TRUE)
    add_compile_definitions(PLATFORM_WINDOWS)
    set(CMAKE_WIN32_EXECUTABLE ON)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Haiku")
    set(PLATFORM_HAIKU TRUE)
    add_compile_definitions(PLATFORM_HAIKU)
elseif(APPLE)
    set(PLATFORM_MACOS TRUE)
    add_compile_definitions(PLATFORM_MACOS)
elseif(UNIX)
    set(PLATFORM_LINUX TRUE)
    add_compile_definitions(PLATFORM_LINUX)
endif()

# ---------------------------------------------------------------------------
# Qt6
# ---------------------------------------------------------------------------
find_package(Qt6 REQUIRED COMPONENTS
    Core
    Gui
    Widgets
    Concurrent
)

qt_standard_project_setup()

# Include cmake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# Collect sources
add_subdirectory(src)

# Resources
qt_add_resources(BARECODE_RESOURCES resources/resources.qrc)

# ---------------------------------------------------------------------------
# Executable
# Unter Windows: .rc-Datei für EXE-Icon einbinden
# ---------------------------------------------------------------------------
if(PLATFORM_WINDOWS)
    qt_add_executable(BareCode
        main.cpp
        BareCode.rc
        ${BARECODE_RESOURCES}
    )
else()
    qt_add_executable(BareCode
        main.cpp
        ${BARECODE_RESOURCES}
    )
endif()

target_link_libraries(BareCode PRIVATE
    BareCode_Core
    BareCode_Editor
    BareCode_FileTree
    BareCode_Highlighter
    Qt6::Core
    Qt6::Gui
    Qt6::Widgets
)

target_include_directories(BareCode PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)

# ---------------------------------------------------------------------------
# Installation
# ---------------------------------------------------------------------------
include(GNUInstallDirs)

install(TARGETS BareCode
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

# ---- Linux (FreeDesktop) --------------------------------------------------
if(PLATFORM_LINUX)
    foreach(SIZE 16 32 48 64 128 256 512)
        install(FILES resources/icon_${SIZE}.png
            DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/${SIZE}x${SIZE}/apps
            RENAME barecode.png
        )
    endforeach()

    install(FILES BareCode.desktop
        DESTINATION ${CMAKE_INSTALL_DATADIR}/applications
    )
endif()

# ---- Haiku ----------------------------------------------------------------
# Haiku verwendet kein FreeDesktop-System. Icons und MIME-Typen werden
# nativ über 'mimeset' gesetzt. Die PNG-Icons legen wir in den
# Haiku-typischen Pfad, ein post-install Skript ruft mimeset auf.
if(PLATFORM_HAIKU)
    install(FILES resources/icon_256.png
        DESTINATION ${CMAKE_INSTALL_DATADIR}/BareCode
        RENAME BareCode.png
    )

    # mimeset nach der Installation ausführen um MIME-Typ zu registrieren
    install(CODE "
        execute_process(
            COMMAND mimeset -f \"\$ENV{DESTDIR}${CMAKE_INSTALL_FULL_BINDIR}/BareCode\"
            RESULT_VARIABLE _mimeset_result
        )
        if(NOT _mimeset_result EQUAL 0)
            message(WARNING \"mimeset konnte nicht ausgeführt werden – MIME-Typ muss manuell gesetzt werden.\")
        endif()
    ")
endif()

# ---- macOS ----------------------------------------------------------------
if(PLATFORM_MACOS)
    set_target_properties(BareCode PROPERTIES
        MACOSX_BUNDLE TRUE
        MACOSX_BUNDLE_BUNDLE_NAME "BareCode"
        MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
        MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION}
    )
endif()

