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

add_library(ixwebsocket SHARED
    ${ixwebsocket}
)

target_include_directories(ixwebsocket PRIVATE
    ${CMAKE_SOURCE_DIR}/ixwebsocket/include
)

set_target_properties(ixwebsocket PROPERTIES 
  PUBLIC_HEADER ${CMAKE_SOURCE_DIR}/ixwebsocket/include
)
target_compile_options(ixwebsocket PRIVATE
    -Wall
    -Wextra
    -pedantic
)
if(DEBUG)
    target_compile_options(ixwebsocket PRIVATE
        -g
    )
    target_link_options(ixwebsocket PRIVATE
        -g
    )
else()
    target_compile_options(ixwebsocket PRIVATE
        -O2
    )
endif()

if(APPLE)
    message("Building ixwebsocket for MacOSX Universal into ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}")

    set_target_properties(ixwebsocket PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}
        BUILD_WITH_INSTALL_NAME_DIR TRUE
    )    

    target_compile_definitions(ixwebsocket PRIVATE
        IXWEBSOCKET_USE_TLS
        IXWEBSOCKET_USE_SECURE_TRANSPORT
    )
    target_compile_options(ixwebsocket PRIVATE
        "SHELL:-arch x86_64"
        "SHELL:-arch arm64"
    )
    target_link_options(ixwebsocket PRIVATE
        "SHELL:-arch x86_64"
        "SHELL:-arch arm64"
    )
    target_link_libraries(ixwebsocket PRIVATE
        "-framework Foundation"
        "-framework Security"
    )
elseif(UNIX)
    message("Building ixwebsocket for Linux ${BIT} into ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}/${BIT}")

    set_target_properties(ixwebsocket PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}/${BIT}
        INSTALL_RPATH "\$ORIGIN"
    )

    target_compile_definitions(ixwebsocket PRIVATE
        IXWEBSOCKET_USE_TLS
        IXWEBSOCKET_USE_OPEN_SSL
    )
    target_compile_options(ixwebsocket PRIVATE
        -nodefaultlibs
    )
    
    if (NOT OPENSSL_FOUND)
      find_package(OpenSSL REQUIRED)
    endif()
    message(STATUS "System OpenSSL: " ${OPENSSL_VERSION})

    add_definitions(${OPENSSL_DEFINITIONS})
    target_include_directories(ixwebsocket PRIVATE
        ${OPENSSL_INCLUDE_DIR}
    )
    target_link_libraries(ixwebsocket PRIVATE
        ${OPENSSL_LIBRARIES}
    )
elseif(WIN32)
    if (BIT STREQUAL "32")
        message("Building ixwebsocket for Windows ${BIT} into ${PROJECT_BINARY_DIR}/ESP/${PLUGIN_NAME}")

        set_target_properties(ixwebsocket PROPERTIES
            RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/ESP/${PLUGIN_NAME}"
        )
    else()
        message("Building fileixwebsocket for Windows ${BIT} into ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}/${BIT}")

        set_target_properties(ixwebsocket PROPERTIES
            RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}/${BIT}"
        )
    endif()

    target_compile_definitions(ixwebsocket PRIVATE
        IXWEBSOCKET_USE_TLS
        IXWEBSOCKET_USE_OPEN_SSL
    )
    if(DEBUG)
        target_compile_options(ixwebsocket PRIVATE
            -gcodeview
        )
        target_link_options(ixwebsocket PRIVATE
            -Wl,-pdb=
        )
    endif()
    target_include_directories(ixwebsocket PRIVATE
        ${CMAKE_SOURCE_DIR}/openSSL/win${BIT}/include
    )
    target_link_libraries(ixwebsocket PRIVATE
        wsock32
        ws2_32
        shlwapi
        crypt32
        ${CMAKE_SOURCE_DIR}/openSSL/win${BIT}/libcrypto-1_1.dll
        ${CMAKE_SOURCE_DIR}/openSSL/win${BIT}/libssl-1_1.dll
    )
endif()

target_link_libraries(ixwebsocket PRIVATE
    pthread
)

add_library(ixwebsocket::ixwebsocket ALIAS ixwebsocket)