include(GNUInstallDirs)

set(DEPS_INSTALL_DIR "${CMAKE_BINARY_DIR}/deps")
file(MAKE_DIRECTORY ${DEPS_INSTALL_DIR})

list(APPEND CMAKE_PREFIX_PATH ${DEPS_INSTALL_DIR})

option(WITH_OPENSSL "Build and enable OpenSSL" ON)
option(WITH_FFMPEG "Build and enable FFmpeg codec support" ON)
option(WITH_OPENH264 "Build and enable OpenH264 codec support" ON)
option(WITH_CJSON "Build cJSON for Azure AD (AAD) support" ON)
option(WITH_OPUS "Build Opus audio codec" ON)
option(WITH_PNG "Build libpng image support" ON)
option(WITH_WEBP "Build libwebp image support" ON)
option(WITH_JPEG "Build libjpeg-turbo image support" ON)
option(WITH_URIPARSER "Build uriparser support" ON)

get_filename_component(_toolchain_abs "${CMAKE_TOOLCHAIN_FILE}" ABSOLUTE BASE_DIR "${CMAKE_BINARY_DIR}")

list(APPEND CMAKE_PREFIX_PATH ${DEPS_INSTALL_DIR} ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}
     ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/cmake ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
list(APPEND CMAKE_MODULE_PATH ${DEPS_INSTALL_DIR} ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}
     ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/cmake ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
list(APPEND CMAKE_FIND_ROOT_PATH ${DEPS_INSTALL_DIR} ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}
     ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/cmake ${DEPS_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/pkgconfig
)

string(REPLACE ";" "|" IOS_PREFIX_PATH "${CMAKE_PREFIX_PATH}")
string(REPLACE ";" "|" IOS_MODULE_PATH "${CMAKE_MODULE_PATH}")
string(REPLACE ";" "|" IOS_FIND_ROOT_PATH "${CMAKE_FIND_ROOT_PATH}")
set(IOS_CMAKE_ARGS
    -G${CMAKE_GENERATOR}
    -DCMAKE_TOOLCHAIN_FILE:FILEPATH=${_toolchain_abs}
    -DPLATFORM:STRING=${PLATFORM}
    -DDEPLOYMENT_TARGET=${DEPLOYMENT_TARGET}
    -DPLATFORM=${PLATFORM}
    -DCMAKE_IGNORE_PREFIX_PATH=${CMAKE_IGNORE_PREFIX_PATH}
    -DCMAKE_VERBOSE_MAKEFILE=${CMAKE_VERBOSE_MAKEFILE}
    -DCMAKE_INSTALL_NAME_DIR:STRING=@rpath
    -DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT}
    -DCMAKE_INSTALL_LIBDIR:STRING=${CMAKE_INSTALL_LIBDIR}
    -DCMAKE_INSTALL_BINDIR:STRING=${CMAKE_INSTALL_BINDIR}
    -DCMAKE_INSTALL_INCLUDEDIR:STRING=${CMAKE_INSTALL_INCLUDEDIR}
    -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=${CMAKE_FIND_ROOT_PATH_MODE_INCLUDE}
    -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=${CMAKE_FIND_ROOT_PATH_MODE_LIBRARY}
    -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=${CMAKE_FIND_ROOT_PATH_MODE_PACKAGE}
    -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
    -DBUILD_SHARED_LIBS:BOOL=ON
    -DCMAKE_INSTALL_PREFIX:PATH=${DEPS_INSTALL_DIR}
    -DCMAKE_SYSROOT=${CMAKE_SYSROOT}
    -DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}
    -DCMAKE_CONFIGURATION_TYPES=${CMAKE_CONFIGURATION_TYPES}
    -DCMAKE_PREFIX_PATH:STRING=${IOS_PREFIX_PATH}
    -DCMAKE_MODULE_PATH:STRING=${IOS_MODULE_PATH}
    -DCMAKE_FIND_ROOT_PATH:STRING=${IOS_FIND_ROOT_PATH}
    -DCMAKE_C_STANDARD=${CMAKE_C_STANDARD}
    -DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}
)

set(IOS_CMAKE_CACHE_ARGS)

if(DEFINED CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM)
  list(APPEND IOS_CMAKE_ARGS -DCMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM:STRING=${CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM})
endif()
if(DEFINED CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_STYLE)
  list(APPEND IOS_CMAKE_ARGS -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGN_STYLE:STRING=${CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_STYLE})
endif()
if(DEFINED CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY)
  list(APPEND IOS_CMAKE_ARGS
       -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY:STRING=${CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY}
  )
endif()

set(IOS_NATIVE_DEPS "")

if(WITH_OPENSSL)
  include(ExternalOpenSSL)
  list(APPEND IOS_NATIVE_DEPS openssl)
endif()

if(WITH_CJSON)
  include(ExternalCJSON)
  list(APPEND IOS_NATIVE_DEPS cjson)
endif()

if(WITH_OPENH264)
  include(ExternalOpenH264)
  list(APPEND IOS_NATIVE_DEPS openh264)
endif()

if(WITH_OPUS)
  include(ExternalOpus)
  list(APPEND IOS_NATIVE_DEPS opus)
endif()

if(WITH_PNG)
  include(ExternalLibPNG)
  list(APPEND IOS_NATIVE_DEPS libpng)
endif()

if(WITH_JPEG)
  include(ExternalJpeg)
  list(APPEND IOS_NATIVE_DEPS jpeg)
endif()

if(WITH_WEBP)
  include(ExternalWebP)
  list(APPEND IOS_NATIVE_DEPS webp)
endif()

set(IOS_FFMPEG_ARGS "")
if(WITH_FFMPEG)
  include(ExternalFFmpeg)
  list(APPEND IOS_NATIVE_DEPS ffmpeg)
endif()

if(WITH_URIPARSER)
  include(ExternalUriparser)
  list(APPEND IOS_NATIVE_DEPS uriparser)
endif()

include(ExternalFreeRDP)
