set(HTP_N3N_ROOT "${CMAKE_CURRENT_LIST_DIR}/../../third_party/n3n")
get_filename_component(HTP_N3N_ROOT "${HTP_N3N_ROOT}" ABSOLUTE)

if(NOT EXISTS "${HTP_N3N_ROOT}/src/edge_utils.c")
    message(FATAL_ERROR "n3n submodule is missing. Run: git submodule update --init --recursive")
endif()

find_package(Perl REQUIRED)
set(HTP_N3N_GENERATED "${CMAKE_CURRENT_BINARY_DIR}/n3n-generated")
file(MAKE_DIRECTORY "${HTP_N3N_GENERATED}")

foreach(asset management_index.html management_script.js)
    get_filename_component(symbol "${asset}" NAME_WE)
    add_custom_command(
        OUTPUT "${HTP_N3N_GENERATED}/${asset}.h"
        COMMAND "${PERL_EXECUTABLE}" "${HTP_N3N_ROOT}/libs/connslot/file2strbufc"
                "${HTP_N3N_ROOT}/src/${asset}" "${symbol}" > "${HTP_N3N_GENERATED}/${asset}.h"
        DEPENDS "${HTP_N3N_ROOT}/src/${asset}" "${HTP_N3N_ROOT}/libs/connslot/file2strbufc"
        VERBATIM)
endforeach()

add_library(htp_connslot STATIC
    "${HTP_N3N_ROOT}/libs/connslot/connslot.c"
    "${HTP_N3N_ROOT}/libs/connslot/jsonrpc.c"
    "${HTP_N3N_ROOT}/libs/connslot/strbuf.c")
target_include_directories(htp_connslot PUBLIC "${HTP_N3N_ROOT}/libs")
target_compile_definitions(htp_connslot PRIVATE METRICS)

set(HTP_N3N_SOURCES
    aes.c auth.c base64.c benchmark.c benchmark_pdu.c cc20.c conffile.c conffile_defs.c curve25519.c
    edge_utils.c header_encryption.c hexdump.c initfuncs.c json.c logging.c
    mainloop.c management.c metrics.c minilzo.c n2n.c n2n_port_mapping.c n2n_regex.c
    network_traffic_filter.c pearson.c peer_info.c pktbuf.c random_numbers.c resolve.c
    sn_selection.c sn_utils.c speck.c tf.c transform.c
    transform_aes.c transform_cc20.c transform_lzo.c transform_none.c
    transform_null.c transform_speck.c transform_tf.c transform_zstd.c wire.c)
list(TRANSFORM HTP_N3N_SOURCES PREPEND "${HTP_N3N_ROOT}/src/")

add_library(htp_n3n_upstream STATIC
    ${HTP_N3N_SOURCES}
    "${CMAKE_CURRENT_SOURCE_DIR}/src/tuntap_mobile.c"
    "${HTP_N3N_GENERATED}/management_index.html.h"
    "${HTP_N3N_GENERATED}/management_script.js.h")
target_include_directories(htp_n3n_upstream PUBLIC
    "${HTP_N3N_ROOT}/include"
    "${HTP_N3N_ROOT}/src"
    "${HTP_N3N_ROOT}/libs"
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
    "${HTP_N3N_GENERATED}")
target_compile_definitions(htp_n3n_upstream PRIVATE
    VERSION="3.4.6"
    BUILDDATE="HT-Portal-mobile"
    HAVE_BRIDGING_SUPPORT
    N3N_EMBEDDED_MANAGED_LIFECYCLE
    SKIP_MULTICAST_PEERS_DISCOVERY)

if(ANDROID)
    target_compile_definitions(htp_n3n_upstream PRIVATE
        CONFIG_RUNDIR="/data/data/net.hivetechnology.o20010_06/cache")
    target_link_libraries(htp_n3n_upstream PUBLIC htp_connslot log m)
elseif(APPLE)
    target_compile_definitions(htp_n3n_upstream PRIVATE CONFIG_RUNDIR="/tmp")
    target_link_libraries(htp_n3n_upstream PUBLIC htp_connslot m)
else()
    target_compile_definitions(htp_n3n_upstream PRIVATE CONFIG_RUNDIR="/tmp")
    target_link_libraries(htp_n3n_upstream PUBLIC htp_connslot m pthread)
endif()

add_library(htp_n3n STATIC src/n3n_adapter.c src/n3n_path.c)
target_include_directories(htp_n3n PUBLIC include)
target_include_directories(htp_n3n PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/src"
    "${HTP_N3N_ROOT}/include"
    "${HTP_N3N_ROOT}/src"
    "${HTP_N3N_ROOT}/libs"
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
target_compile_definitions(htp_n3n PRIVATE VERSION="3.4.6")
target_link_libraries(htp_n3n PUBLIC htp_packet_bridge htp_n3n_upstream)
