# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=freerdp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.20.0
pkgrel=1
pkgdesc="Free implementation of the Remote Desktop Protocol (RDP) (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.freerdp.com/"
msys2_repository_url="https://github.com/FreeRDP/FreeRDP"
msys2_references=(
  'archlinux: freerdp'
  "cpe: cpe:/a:freerdp:freerdp"
  'gentoo: net-misc/freerdp'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-cjson"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-sdl3-ttf"
         "${MINGW_PACKAGE_PREFIX}-uriparser")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-icu"
             "${MINGW_PACKAGE_PREFIX}-iconv"
             "${MINGW_PACKAGE_PREFIX}-docbook-xsl")
source=("https://github.com/FreeRDP/FreeRDP/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-nameless-union.patch"
        "002-fix-missing-symbol.patch")
sha256sums=('7aef7cb22398ecdff0dd83071cbd17d20881751750b3879982af75b28042fd0d'
            '445e9ce79a7783345082d0f6276c3baea50c2aeed8047c18846a37c8bef995e6'
            '86adfa3e07eef9e2833559804ebcb97080140e9050df7ac5af8580310d30112a')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 --binary -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd FreeRDP-${pkgver}

  apply_patch_with_msg \
    001-nameless-union.patch \
    002-fix-missing-symbol.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _extra_config+=("-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=OFF")
  fi

  CFLAGS+=" -Wno-deprecated-declarations -Wno-incompatible-pointer-types"
  CFLAGS+=" -D__STDC_NO_THREADS__=1"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_LIBDIR='lib' \
    "${_extra_config[@]}" \
    -DPROXY_PLUGINDIR='${MINGW_PREFIX}/lib/freerdp2/server/proxy/plugins' \
    -DWITH_AAD=ON \
    -DWITH_CAIRO=ON \
    -DWITH_CHANNELS=ON \
    -DWITH_CLIENT_CHANNELS=ON \
    -DWITH_DSP_FFMPEG=ON \
    -DWITH_FFMPEG=ON \
    -DWITH_ICU=ON \
    -DWITH_JPEG=ON \
    -DWITH_MANPAGES=ON \
    -DWITH_MBEDTLS=ON \
    -DWITH_SERVER=ON \
    -DWITH_SERVER_CHANNELS=ON \
    -DWITH_SWSCALE=ON \
    -DWITH_ZLIB=ON \
    -DWITH_VERBOSE_WINPR_ASSERT=OFF \
    -DWITH_WEBVIEW=OFF \
    -DWITH_WINPR_TOOLS=ON \
    -DWITH_SHADOW=OFF \
    -DWITH_CLIENT_SDL2=OFF \
    -DWITH_MBEDTLS=OFF \
    -DWITH_CLIENT_INTERFACE=ON \
    -DWITH_WIN_CONSOLE=ON \
    -DCHANNEL_URBDRC_CLIENT=ON \
    -DUSE_UNWIND=OFF \
    -DBUILD_TESTING=OFF \
    "../FreeRDP-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .
  install -vDm0644 "${srcdir}/FreeRDP-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
