file(GLOB socket CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/websocket/*.cpp)
file(GLOB file CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/file/*.cpp)

add_library(germanairlinesva_xplugin SHARED
    ${socket}
    ${file}
    main.cpp
)

target_include_directories(germanairlinesva_xplugin PRIVATE
    ${CMAKE_SOURCE_DIR}/file/include
    ${CMAKE_SOURCE_DIR}/simdata/include
    ${CMAKE_SOURCE_DIR}/websocket/include
    ${CMAKE_SOURCE_DIR}/utilities/include
    ${CMAKE_SOURCE_DIR}/ixwebsocket/include
    ${CMAKE_SOURCE_DIR}/nlohmann/include
    ${CMAKE_SOURCE_DIR}/XPSDK/CHeaders
)

set_target_properties(germanairlinesva_xplugin PROPERTIES
    PREFIX ""
    SUFFIX ".xpl"
)
target_compile_definitions(germanairlinesva_xplugin PRIVATE
    XPLM200
    XPLM210
)
target_compile_options(germanairlinesva_xplugin PRIVATE
    -Wall
    -Wextra
    -pedantic
    -fvisibility=hidden
)
if(DEBUG)
    target_compile_options(germanairlinesva_xplugin PRIVATE
        -g
    )
    target_link_options(germanairlinesva_xplugin PRIVATE
        -g
    )
else()
    target_compile_options(germanairlinesva_xplugin PRIVATE
        -O2
    )
endif()

if(APPLE)
    message("Building for MacOSX Universal into ${PROJECT_BINARY_DIR}/${PLUGIN_NAME}/${BIT}")

    set_target_properties(germanairlinesva_xplugin PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/Plugin/${PLUGIN_NAME}
        INSTALL_RPATH "@loader_path"
        OUTPUT_NAME mac
    )

    target_compile_definitions(germanairlinesva_xplugin PUBLIC
        APL
    )
    target_compile_options(germanairlinesva_xplugin PRIVATE
        "SHELL:-arch i386"
        "SHELL:-arch x86_64"
    )
    target_link_options(germanairlinesva_xplugin PRIVATE
        "SHELL:-arch i386"
        "SHELL:-arch x86_64"
    )
    target_link_libraries(germanairlinesva_xplugin PRIVATE
        ${CMAKE_SOURCE_DIR}/XPSDK/Libraries/Mac/XPLM.framework/XPLM
    )
elseif(UNIX)
    message("Building for Linux ${BIT} into ${PROJECT_BINARY_DIR}/Plugin/${PLUGIN_NAME}/${BIT}")

    set_target_properties(germanairlinesva_xplugin PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/Plugin/${PLUGIN_NAME}/${BIT}
        INSTALL_RPATH "\$ORIGIN"
        OUTPUT_NAME lin
    )

    target_compile_definitions(germanairlinesva_xplugin PUBLIC
        LIN
    )
    target_compile_options(germanairlinesva_xplugin PRIVATE
        -nodefaultlibs
    )
    if(BIT STREQUAL "32")
        target_compile_options(germanairlinesva_xplugin PRIVATE
            -m32
        )
        target_link_options(germanairlinesva_xplugin PRIVATE
            -m32
        )
    endif()
    target_link_libraries(germanairlinesva_xplugin PRIVATE
        pthread
    )
elseif(WIN32)
    message("Building for Windows ${BIT} into ${PROJECT_BINARY_DIR}/Plugin/${PLUGIN_NAME}/${BIT}")

    set_target_properties(germanairlinesva_xplugin PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/Plugin/${PLUGIN_NAME}/${BIT}
        OUTPUT_NAME win
    )

    target_compile_definitions(germanairlinesva_xplugin PUBLIC
        IBM
    )
    if(DEBUG)
        target_compile_options(germanairlinesva_xplugin PRIVATE
            -gcodeview
        )
        target_link_options(germanairlinesva_xplugin PRIVATE
            -Wl,-pdb=
        )
    endif()
    target_link_options(germanairlinesva_xplugin PRIVATE
        -static-libgcc
        -static-libstdc++
    )
    target_link_libraries(germanairlinesva_xplugin PRIVATE
    )
    if(BIT STREQUAL "32")
        target_link_libraries(germanairlinesva_xplugin PRIVATE
            ${CMAKE_SOURCE_DIR}/XPSDK/Libraries/Win/XPLM.lib
        )
    else()
        target_link_libraries(germanairlinesva_xplugin PRIVATE
            ${CMAKE_SOURCE_DIR}/XPSDK/Libraries/Win/XPLM_64.lib
        )
    endif()
    target_sources(germanairlinesva_xplugin PRIVATE
        xPluginWin.cpp
    )
endif()

target_link_libraries(germanairlinesva_xplugin PRIVATE
    simdata
    ixwebsocket
)
