find_package(PkgConfig REQUIRED)

if(WITH_OPENSSL)
  if(BUILD_SHARED_LIBS)
    list(APPEND SSL_ARGS -DOPENSSL_USE_STATIC_LIBS=OFF)
  else()
    list(APPEND SSL_ARGS -DOPENSSL_USE_STATIC_LIBS=ON)
  endif()
endif()

if(WITH_CJSON)
  list(APPEND SSL_ARGS -DWITH_CJSON_REQUIRED=ON)
else()
  list(APPEND SSL_ARGS -DWITH_JSON_DISABLED=ON)
endif()

set(FREERDP_EXTRA_CMAKE_ARGS
    -DWITHOUT_WINPR_3x_DEPRECATED:BOOL=ON
    -DWITHOUT_FREERDP_3x_DEPRECATED:BOOL=ON
    -DWITH_CLIENT:BOOL=OFF
    -DWITH_CLIENT_SDL:BOOL=OFF
    -DWITH_SAMPLE:BOOL=OFF
    -DWITH_SERVER:BOOL=OFF
    -DWITH_MANPAGES:BOOL=OFF
    -DWITH_SYSTEMD:BOOL=OFF
    -DPKG_CONFIG_EXECUTABLE:FILEPATH=${PKG_CONFIG_EXECUTABLE}
    ${SSL_ARGS}
    -DWITH_INTERNAL_RC4:BOOL=ON
    -DWITH_INTERNAL_MD4:BOOL=ON
    -DWITH_INTERNAL_MD5:BOOL=ON
    -DWITH_OPENSSL:BOOL=${WITH_OPENSSL}
    -DWITH_VIDEOTOOLBOX:BOOL=ON
    -DWITH_FFMPEG:BOOL=${WITH_FFMPEG}
    -DWITH_VIDEO_FFMPEG:BOOL=${WITH_FFMPEG}
    -DWITH_DSP_FFMPEG:BOOL=${WITH_FFMPEG}
    -DWITH_SWSCALE:BOOL=${WITH_FFMPEG}
    -DWITH_OPUS:BOOL=${WITH_OPUS}
    -DWITH_OPENH264:BOOL=${WITH_OPENH264}
    -DWINPR_UTILS_IMAGE_PNG:BOOL=${WITH_PNG}
    -DWINPR_UTILS_IMAGE_WEBP:BOOL=${WITH_WEBP}
    -DWINPR_UTILS_IMAGE_JPEG:BOOL=${WITH_JPEG}
    -DWITH_JPEG:BOOL=${WITH_JPEG}
    -DWITH_URIPARSER:BOOL=${WITH_URIPARSER}
    ${IOS_FFMPEG_ARGS}
)

ExternalProject_Add(
  freerdp
  DOWNLOAD_EXTRACT_TIMESTAMP OFF
  SOURCE_DIR ${FREERDP_SOURCE_DIR}
  LIST_SEPARATOR |
  CMAKE_CACHE_ARGS ${IOS_CMAKE_CACHE_ARGS}
  CMAKE_ARGS ${IOS_CMAKE_ARGS} ${FREERDP_EXTRA_CMAKE_ARGS}
  DEPENDS ${IOS_NATIVE_DEPS}
  BUILD_BYPRODUCTS
    ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/libfreerdp3.3.dylib
    ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/libfreerdp-client3.3.dylib
    ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/libwinpr3.3.dylib
)

file(MAKE_DIRECTORY ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_INCLUDEDIR}
     ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_INCLUDEDIR}/freerdp3 ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_INCLUDEDIR}/winpr3
)

add_library(freerdp3-lib SHARED IMPORTED GLOBAL)
set_target_properties(
  freerdp3-lib PROPERTIES IMPORTED_LOCATION "${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/libfreerdp3.3.dylib"
                          INTERFACE_INCLUDE_DIRECTORIES "${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_INCLUDEDIR}/freerdp3"
)
add_dependencies(freerdp3-lib freerdp)

add_library(freerdp-client3-lib SHARED IMPORTED GLOBAL)
set_target_properties(
  freerdp-client3-lib
  PROPERTIES IMPORTED_LOCATION "${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/libfreerdp-client3.3.dylib"
             INTERFACE_INCLUDE_DIRECTORIES "${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_INCLUDEDIR}/freerdp3"
)
add_dependencies(freerdp-client3-lib freerdp)

add_library(winpr3-lib SHARED IMPORTED GLOBAL)
set_target_properties(
  winpr3-lib PROPERTIES IMPORTED_LOCATION "${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/libwinpr3.3.dylib"
                        INTERFACE_INCLUDE_DIRECTORIES "${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_INCLUDEDIR}/winpr3"
)
add_dependencies(winpr3-lib freerdp)
