add_library(libSceHttp SHARED EXCLUDE_FROM_ALL Export.cpp src/Options.cpp src/Parse.cpp src/Uri.cpp src/Client.cpp)
target_include_directories(libSceHttp PRIVATE ${LIBS_INCLUDE_DIR})
target_sources(libSceHttp PRIVATE src/Cookies.cpp)
configure_windows_unwind(libSceHttp)
if(WIN32)
    target_link_libraries(libSceHttp PRIVATE ws2_32)
endif()

if(BUILD_TESTING)
    add_test_executable(guest_http_dates_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttpDates.cpp)
    target_include_directories(guest_http_dates_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http_dates_tests PRIVATE libc)
    configure_windows_unwind(guest_http_dates_tests)
    add_test(NAME guest_http_dates COMMAND guest_http_dates_tests)
    set_tests_properties(guest_http_dates PROPERTIES TIMEOUT 20)
    add_test_executable(guest_http_uri_guest_pointers_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttpUriGuestPointers.cpp)
    target_include_directories(guest_http_uri_guest_pointers_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http_uri_guest_pointers_tests PRIVATE libSceHttp libc)
    configure_windows_unwind(guest_http_uri_guest_pointers_tests)
    add_test(NAME guest_http_uri_guest_pointers COMMAND guest_http_uri_guest_pointers_tests)
    set_tests_properties(guest_http_uri_guest_pointers PROPERTIES TIMEOUT 20)
    if(WIN32)
        target_link_libraries(guest_http_dates_tests PRIVATE ws2_32)
        set_tests_properties(guest_http_dates PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
    endif()

    add_test_executable(guest_http_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttp.cpp)
    target_include_directories(guest_http_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http_tests PRIVATE libSceHttp libc)
    configure_windows_unwind(guest_http_tests)
    add_test(NAME guest_http COMMAND guest_http_tests)
    add_test_executable(guest_http_guest_pointer_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttpGuestPointers.cpp)
    target_include_directories(guest_http_guest_pointer_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http_guest_pointer_tests PRIVATE libSceHttp libc)
    configure_windows_unwind(guest_http_guest_pointer_tests)
    add_test(NAME guest_http_guest_pointers COMMAND guest_http_guest_pointer_tests)
    add_test(NAME guest_http_uri_locale COMMAND guest_http_tests --uri-locale)
    set_tests_properties(guest_http_uri_locale PROPERTIES SKIP_RETURN_CODE 77)
    if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
        set(ANYPS5_HTTP_TEST_LOCALE_DIR "" CACHE PATH "Private locale directory for the HTTP URI regression")
        set(http_test_locale_dir "${ANYPS5_HTTP_TEST_LOCALE_DIR}")
        if(NOT http_test_locale_dir AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
            set(private_locale_dir "${CMAKE_CURRENT_BINARY_DIR}/locales")
            file(MAKE_DIRECTORY "${private_locale_dir}")
            execute_process(
                COMMAND localedef --no-archive -i en_US -f ISO-8859-1 "${private_locale_dir}/en_US.ISO8859-1"
                RESULT_VARIABLE locale_result
                OUTPUT_VARIABLE locale_output
                ERROR_VARIABLE locale_error
                TIMEOUT 30
            )
            if(locale_result STREQUAL "0")
                set(http_test_locale_dir "${private_locale_dir}")
                message(STATUS "HTTP URI locale: ${http_test_locale_dir}")
            else()
                message(STATUS "HTTP URI locale unavailable (${locale_result}): ${locale_output}${locale_error}")
            endif()
        endif()
        if(http_test_locale_dir)
            set_property(TEST guest_http_uri_locale APPEND PROPERTY ENVIRONMENT "LOCPATH=${http_test_locale_dir}")
        endif()
    endif()
    add_test_executable(guest_http_response_header_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttpResponseHeader.cpp)
    target_include_directories(guest_http_response_header_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http_response_header_tests PRIVATE libSceHttp libc)
    configure_windows_unwind(guest_http_response_header_tests)
    add_test(NAME guest_http_response_header COMMAND guest_http_response_header_tests)
    add_test_executable(guest_http_chunk_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttpChunks.cpp)
    target_include_directories(guest_http_chunk_tests PRIVATE ${LIBS_INCLUDE_DIR})
    configure_host_test_executable(guest_http_chunk_tests)
    add_test(NAME guest_http_chunks COMMAND guest_http_chunk_tests)
    set_tests_properties(guest_http_chunks PROPERTIES TIMEOUT 20)
    add_test_executable(guest_http_options_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttpOptions.cpp)
    target_include_directories(guest_http_options_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http_options_tests PRIVATE libSceHttp libc)
    configure_windows_unwind(guest_http_options_tests)
    add_test(NAME guest_http_options COMMAND guest_http_options_tests)
    find_package(Python3 COMPONENTS Interpreter QUIET)
    if(Python3_Interpreter_FOUND)
        add_test_executable(guest_http_client_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttpClient.cpp)
        target_include_directories(guest_http_client_tests PRIVATE ${LIBS_INCLUDE_DIR})
        target_link_libraries(guest_http_client_tests PRIVATE libSceHttp libc)
        configure_windows_unwind(guest_http_client_tests)
        file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/guest_http_client)
        add_test(NAME guest_http_client COMMAND ${Python3_EXECUTABLE}
            ${LIBS_INCLUDE_DIR}/tests/AvPlayerHttpServer.py $<TARGET_FILE:guest_http_client_tests> client)
        set_tests_properties(guest_http_client PROPERTIES
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/guest_http_client TIMEOUT 20)
        file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/guest_http_client_203)
        add_test(NAME guest_http_client_203 COMMAND ${Python3_EXECUTABLE}
            ${LIBS_INCLUDE_DIR}/tests/AvPlayerHttpServer.py $<TARGET_FILE:guest_http_client_tests> client203 --transformed)
        set_tests_properties(guest_http_client_203 PROPERTIES
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/guest_http_client_203 TIMEOUT 20)
        if(WIN32)
            set_tests_properties(guest_http_client guest_http_client_203 PROPERTIES ENVIRONMENT_MODIFICATION
                "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp>")
        endif()
    endif()
    add_test_executable(guest_http_dbg_printf_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttpDbgPrintf.cpp)
    target_include_directories(guest_http_dbg_printf_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http_dbg_printf_tests PRIVATE libSceHttp libc)
    configure_windows_unwind(guest_http_dbg_printf_tests)
    add_test(NAME guest_http_dbg_printf COMMAND guest_http_dbg_printf_tests)
    if(WIN32)
        set_tests_properties(guest_http_dbg_printf PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp>")
    endif()
    if(WIN32)
        set_tests_properties(guest_http guest_http_uri_locale guest_http_response_header guest_http_options PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_http_state_allocation_tests ${LIBS_INCLUDE_DIR}/tests/HttpStateAllocation.cpp Export.cpp src/Options.cpp src/Cookies.cpp src/Uri.cpp)
    target_include_directories(guest_http_state_allocation_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http_state_allocation_tests PRIVATE libc)
    configure_windows_unwind(guest_http_state_allocation_tests)
    add_test(NAME guest_http_state_init COMMAND guest_http_state_allocation_tests init)
    add_test(NAME guest_http_state_init_live COMMAND guest_http_state_allocation_tests init-live)
    add_test(NAME guest_http_state_create_epoll COMMAND guest_http_state_allocation_tests create)
    foreach(budget RANGE 0 3)
        add_test(NAME guest_http_state_term_${budget} COMMAND guest_http_state_allocation_tests term ${budget})
    endforeach()
    set(httpStateTests guest_http_state_init guest_http_state_init_live guest_http_state_create_epoll guest_http_state_term_0
        guest_http_state_term_1 guest_http_state_term_2 guest_http_state_term_3)
    set_tests_properties(${httpStateTests} PROPERTIES TIMEOUT 20)
    if(WIN32)
        set_tests_properties(${httpStateTests} PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
    endif()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_http_cookies_tests ${LIBS_INCLUDE_DIR}/tests/GuestHttpCookies.cpp)
    target_include_directories(guest_http_cookies_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_http_cookies_tests PRIVATE libSceHttp libc)
    configure_windows_unwind(guest_http_cookies_tests)
    add_test(NAME guest_http_cookies COMMAND guest_http_cookies_tests)
    if(WIN32)
        set_tests_properties(guest_http_cookies PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceHttp>")
    endif()
endif()
