cmake_minimum_required(VERSION 3.25.1)

set(IS_OS_WINDOWS FALSE)
set(IS_OS_LINUX FALSE)
set(IS_OS_MAC FALSE)

set(IS_ARCH_AMD64 FALSE)
set(IS_ARCH_X86 FALSE)
set(IS_ARCH_ARM64 FALSE)
set(IS_ARCH_ARM FALSE)

project(EHS CXX C)

if (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
    set(IS_OS_WINDOWS TRUE)
    set(USER_HOME_DIRECTORY "$ENV{USERPROFILE}")
    string(REPLACE "\\" "/" USER_HOME_DIRECTORY "${USER_HOME_DIRECTORY}")
    message("Building for the Windows operating system.")
elseif (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
    set(IS_OS_LINUX TRUE)
    set(USER_HOME_DIRECTORY $ENV{HOME})
    add_compile_options(-Wno-stringop-overflow)
    message("Building for the Linux operating system.")
elseif (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Darwin")
    set(IS_OS_MAC TRUE)
    set(USER_HOME_DIRECTORY $ENV{HOME})
    message("Building for the Mac operating system.")
endif ()

if ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "AMD64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64")
    set(IS_ARCH_AMD64 TRUE)
    enable_language(ASM_NASM)
    message("Building for the AMD64 architecture.")
elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "arm64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64")
    set(IS_ARCH_ARM64 TRUE)
    enable_language(ASM)
    message("Building for the ARM64 architecture.")
endif ()

set(CMAKE_CXX_STANDARD 20)

set(EHS_SOURCES
    src/EHS.cpp include/ehs/EHS.h
    src/Type.cpp include/ehs/Type.h
    src/BaseObj.cpp include/ehs/BaseObj.h
    src/GC.cpp include/ehs/GC.h
    src/Log.cpp include/ehs/Log.h
    src/URI.cpp include/ehs/URI.h
    src/Math.cpp include/ehs/Math.h
    src/Color4.cpp include/ehs/Color4.h
    src/Color3.cpp include/ehs/Color3.h
    src/Version.cpp include/ehs/Version.h
    src/Base64.cpp include/ehs/Base64.h
    src/Data.cpp include/ehs/Data.h
    src/Range.cpp include/ehs/Range.h
    src/Util.cpp include/ehs/Util.h
    src/Task.cpp include/ehs/Task.h
    src/DataType.cpp include/ehs/DataType.h
    include/ehs/Anchor.h
    include/ehs/Dock.h
    include/ehs/HashMap.h
    include/ehs/HRNG.h
    include/ehs/Link.h
    include/ehs/LinkedList.h
    include/ehs/Mat2.h
    include/ehs/Mat3.h
    include/ehs/Mat4.h
    include/ehs/PRNG.h
    include/ehs/Quat.h
    include/ehs/Rect.h
    include/ehs/Str.h
    include/ehs/Types.h
    include/ehs/UTF.h
    include/ehs/Vec2.h
    include/ehs/Vec3.h
    include/ehs/Vec4.h
    include/ehs/Serializer.h
    include/ehs/Array.h
    include/ehs/Vector.h
    include/ehs/SArray.h
    src/PtrData.cpp include/ehs/PtrData.h
    include/ehs/UniPtr.h
    include/ehs/ShdPtr.h
    include/ehs/WkPtr.h

    src/db/DbVarTmpl.cpp include/ehs/db/DbVarTmpl.h

    src/system/CPU.cpp include/ehs/system/CPU.h
    src/system/Thread.cpp include/ehs/system/Thread.h
    src/system/BaseMutex.cpp include/ehs/system/BaseMutex.h
    src/system/BaseSemaphore.cpp include/ehs/system/BaseSemaphore.h
    src/system/BaseSystem.cpp include/ehs/system/BaseSystem.h
    src/system/BaseOpen.cpp include/ehs/system/BaseOpen.h
    include/ehs/system/Architecture.h
    include/ehs/system/Mutex.h
    include/ehs/system/Open.h
    include/ehs/system/OS.h
    include/ehs/system/Semaphore.h
    include/ehs/system/System.h

    src/json/Json.cpp include/ehs/json/Json.h
    src/json/JsonBase.cpp include/ehs/json/JsonBase.h
    src/json/JsonNum.cpp include/ehs/json/JsonNum.h
    src/json/JsonBool.cpp include/ehs/json/JsonBool.h
    src/json/JsonStr.cpp include/ehs/json/JsonStr.h
    src/json/JsonObj.cpp include/ehs/json/JsonObj.h
    src/json/JsonArray.cpp include/ehs/json/JsonArray.h
    src/json/JsonVar.cpp include/ehs/json/JsonVar.h

    src/io/Resource.cpp include/ehs/io/Resource.h
    src/io/Console.cpp include/ehs/io/Console.h
    src/io/RIFF_Chunk.cpp include/ehs/io/RIFF_Chunk.h
    src/io/RIFF.cpp include/ehs/io/RIFF.h
    src/io/BaseWindow.cpp include/ehs/io/BaseWindow.h
    src/io/BaseFile.cpp include/ehs/io/BaseFile.h
    src/io/Glyph.cpp include/ehs/io/Glyph.h
    src/io/FontAtlas.cpp include/ehs/io/FontAtlas.h
    src/io/BaseFileMonitor.cpp include/ehs/io/BaseFileMonitor.h
    include/ehs/io/COM.h
    include/ehs/io/File.h
    include/ehs/io/FileMonitor.h
    include/ehs/io/Window.h

    src/io/socket/Request.cpp include/ehs/io/socket/Request.h
    src/io/socket/Response.cpp include/ehs/io/socket/Response.h
    src/io/socket/BaseDNS.cpp include/ehs/io/socket/BaseDNS.h
    src/io/socket/BaseUDP.cpp include/ehs/io/socket/BaseUDP.h
    src/io/socket/BaseTCP.cpp include/ehs/io/socket/BaseTCP.h
    src/io/socket/SSL.cpp include/ehs/io/socket/SSL.h

    src/io/socket/rest/Twitch.cpp include/ehs/io/socket/rest/Twitch.h
    src/io/socket/rest/TwitchChat.cpp include/ehs/io/socket/rest/TwitchChat.h
    src/io/socket/rest/Spotify.cpp include/ehs/io/socket/rest/Spotify.h
    include/ehs/io/socket/Socket.h
    include/ehs/io/socket/TCP.h
    include/ehs/io/socket/UDP.h
    include/ehs/io/socket/DNS.h

    src/io/audio/Audio.cpp include/ehs/io/audio/Audio.h
    src/io/audio/BaseAudioDevice.cpp include/ehs/io/audio/BaseAudioDevice.h
    src/io/audio/AudioCodec.cpp include/ehs/io/audio/AudioCodec.h
    include/ehs/io/audio/AudioDevice.h

    src/io/img/PNG.cpp include/ehs/io/img/PNG.h
    src/io/img/Img.cpp include/ehs/io/img/Img.h
    src/io/img/PNG_Chunk.cpp include/ehs/io/img/PNG_Chunk.h
    src/io/img/ImgCodec.cpp include/ehs/io/img/ImgCodec.h

    include/ehs/io/mdl/Vertex.h
    src/io/model/Mesh.cpp include/ehs/io/mdl/Mesh.h
    src/io/model/Bone.cpp include/ehs/io/mdl/Bone.h
    src/io/model/Mdl.cpp include/ehs/io/mdl/Mdl.h
    src/io/model/Animation.cpp include/ehs/io/mdl/Animation.h
    src/io/model/AnimBone.cpp include/ehs/io/mdl/AnimBone.h
    src/io/model/KeyFrame.cpp include/ehs/io/mdl/KeyFrame.h
    src/io/model/PropertyChange.cpp include/ehs/io/mdl/PropertyChange.h

    src/io/hid/ButtonState.cpp include/ehs/io/hid/ButtonState.h
    src/io/hid/Button.cpp include/ehs/io/hid/Button.h
    src/io/hid/Mouse.cpp include/ehs/io/hid/Mouse.h
    src/io/hid/Keyboard.cpp include/ehs/io/hid/Keyboard.h
    src/io/hid/HID.cpp include/ehs/io/hid/HID.h
    src/io/hid/InputHandler.cpp include/ehs/io/hid/InputHandler.h
    src/io/hid/Input.cpp include/ehs/io/hid/Input.h
    src/io/model/MdlCodec.cpp
    include/ehs/io/mdl/MdlCodec.h
    include/ehs/io/UsbBase.h
    src/io/UsbBase.cpp
    include/ehs/db/DbTable.h
    include/ehs/db/DbObject.h
    include/ehs/db/DbVar.h
    src/db/DbVar.cpp
    include/ehs/db/Database.h
    src/db/DbObject.cpp
    src/db/Database.cpp
    src/db/Database.cpp
    src/db/DbTable.cpp
    include/ehs/io/BaseDirectory.h
    src/io/BaseDirectory.cpp
    include/ehs/io/Directory.h
)

if (IS_OS_WINDOWS)
    list(APPEND EHS_SOURCES
        src/io/socket/UDP_W32.cpp include/ehs/io/socket/UDP_W32.h
        src/io/socket/TCP_W32.cpp include/ehs/io/socket/TCP_W32.h
        src/io/socket/DNS_W32.cpp include/ehs/io/socket/DNS_W32.h
        src/system/Semaphore_W32.cpp include/ehs/system/Semaphore_W32.h
        src/system/System_W32.cpp include/ehs/system/System_W32.h
        src/system/Mutex_W32.cpp include/ehs/system/Mutex_W32.h
        src/system/Open_W32.cpp include/ehs/system/Open_W32.h
        src/io/audio/audioDevice_W32.cpp include/ehs/io/audio/audioDevice_W32.h
        src/io/File_W32.cpp include/ehs/io/File_W32.h
        src/io/FileMonitor_W32.cpp include/ehs/io/FileMonitor_W32.h
        src/io/Window_W32.cpp include/ehs/io/Window_W32.h
        src/io/COM.cpp include/ehs/io/COM.h
        src/system/CPU_MSVC_AMD64.asm src/HRNG_MSVC.asm src/Math_MSVC_AMD64.asm
        src/io/Directory_W32.cpp include/ehs/io/Directory_W32.h
    )
elseif (IS_OS_LINUX)
    list(APPEND EHS_SOURCES
        src/io/socket/UDP_BSD.cpp include/ehs/io/socket/UDP_BSD.h
        src/io/socket/TCP_BSD.cpp include/ehs/io/socket/TCP_BSD.h
        src/io/socket/DNS_LNX.cpp include/ehs/io/socket/DNS_LNX.h
        src/system/Semaphore_P.cpp include/ehs/system/Semaphore_P.h
        src/system/System_LNX.cpp include/ehs/system/System_LNX.h
        src/system/Open_UNX.cpp include/ehs/system/Open_UNX.h
        src/io/File_UNX.cpp include/ehs/io/File_UNX.h
        src/io/FileMonitor_UNX.cpp include/ehs/io/FileMonitor_UNX.h
        src/system/Mutex_PT.cpp include/ehs/system/Mutex_PT.h
        src/io/audio/AudioDevice_PW.cpp include/ehs/io/audio/AudioDevice_PW.h
        src/system/FileSystem.cpp include/ehs/system/FileSystem.h
        src/system/User.cpp include/ehs/system/User.h
        src/io/Directory_LNX.cpp include/ehs/io/Directory_LNX.h
        src/io/Usb_LNX.cpp include/ehs/io/Usb_LNX.h
    )

    #set(LINUX_WINDOW_SYSTEM "Wayland" CACHE STRING "Linux Window System")

    if (LINUX_WINDOW_SYSTEM STREQUAL "Wayland")
        add_compile_definitions(EHS_WS_WAYLAND)
        list(APPEND EHS_SOURCES
                src/io/xdg-shell-protocol.c include/ehs/io/xdg-shell-client-protocol.h
                src/io/xdg-decoration.c include/ehs/io/xdg-decoration.h
                src/io/Window_Way.cpp include/ehs/io/Window_Way.h)
        message("Building for Wayland.")
    elseif (LINUX_WINDOW_SYSTEM STREQUAL "XCB")
        add_compile_definitions(EHS_WS_XCB)
        list(APPEND EHS_SOURCES src/io/Window_XCB.cpp include/ehs/io/Window_XCB.h)
        message("Building for XCB.")
    endif ()

    if (IS_ARCH_AMD64)
        list(APPEND EHS_SOURCES src/system/CPU_GCC_AMD64.asm src/HRNG_GCC.asm src/Math_GCC_AMD64.asm)
    elseif (IS_ARCH_ARM64)
        list(APPEND EHS_SOURCES src/system/CPU_ARM64.cpp src/HRNG_ARM64.cpp src/Math_GCC_ARM64.s)
    endif ()
endif()

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
#message("${CMAKE_CXX_FLAGS}")

add_library(EHS_Stc STATIC ${EHS_SOURCES})
add_library(EHS_Dyn SHARED ${EHS_SOURCES})
add_executable(StrToHash src/StrToHash.cpp)

target_compile_definitions(EHS_Dyn PRIVATE EHS_LIB_EXPORT)

target_include_directories(EHS_Stc PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_include_directories(EHS_Dyn PUBLIC ${PROJECT_SOURCE_DIR}/include)

if (IS_OS_LINUX)
    set(CMAKE_INSTALL_PREFIX "${USER_HOME_DIRECTORY}/.local")
    install(TARGETS EHS_Dyn LIBRARY DESTINATION "bin")

    find_package(PkgConfig REQUIRED)
    pkg_check_modules(PIPEWIRE REQUIRED libpipewire-0.3)

    target_include_directories(EHS_Stc PRIVATE ${PIPEWIRE_INCLUDE_DIRS})
    target_include_directories(EHS_Dyn PRIVATE ${PIPEWIRE_INCLUDE_DIRS})
elseif (IS_OS_WINDOWS)
    set(CMAKE_INSTALL_PREFIX "${USER_HOME_DIRECTORY}/EHS")
    install(TARGETS EHS_Dyn LIBRARY DESTINATION "lib")
    install(TARGETS EHS_Dyn RUNTIME DESTINATION "bin")

    file(GLOB DLL_FILES "${CMAKE_CURRENT_BINARY_DIR}/*.dll")
    install(FILES ${DLL_FILES} DESTINATION "bin")
endif ()

install(TARGETS EHS_Stc DESTINATION "lib")
install(TARGETS StrToHash DESTINATION "bin")

install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/" DESTINATION "include")

find_package(ZLIB REQUIRED)
if (ZLIB_FOUND)
    message(STATUS "ZLIB was found.")
else ()
    message(STATUS "ZLIB was not found.")
endif ()

find_package(OpenSSL REQUIRED)
if (OpenSSL_FOUND)
    message(STATUS "OpenSSL was found.")
else ()
    message(STATUS "OpenSSL was not found.")
endif ()

target_link_libraries(EHS_Stc OpenSSL::SSL OpenSSL::Crypto ZLIB::ZLIB)
target_link_libraries(EHS_Dyn OpenSSL::SSL OpenSSL::Crypto ZLIB::ZLIB)

if (IS_OS_WINDOWS)
    target_link_libraries(EHS_Dyn avrt ws2_32)
    target_link_libraries(StrToHash ws2_32 avrt EHS_Stc)
elseif (IS_OS_LINUX)
    if (LINUX_WINDOW_SYSTEM STREQUAL "Wayland")
        target_link_libraries(EHS_Dyn wayland-client)
        target_link_libraries(StrToHash wayland-client)
    elseif (LINUX_WINDOW_SYSTEM STREQUAL "XCB")
        target_link_libraries(EHS_Dyn xcb xcb-cursor xcb-xfixes xcb-xinput)
        target_link_libraries(StrToHash xcb xcb-cursor xcb-xfixes xcb-xinput)
    endif ()

    target_link_libraries(EHS_Dyn z asound pipewire-0.3)
    target_link_libraries(StrToHash z asound EHS_Stc)
endif ()