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

_realname=freerdp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.2
pkgrel=1
pkgdesc="Free implementation of the Remote Desktop Protocol (RDP) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.freerdp.com/"
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-iconv")
options=('strip' '!debug' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/${_realname}/${_realname}/archive/${pkgver}.tar.gz"
        "0001-fix-warnings-mingw.patch"
        "0002-fix-build-errors-mingw.patch"
        "0003-remove-declspec-import-export.patch"
        "0004-remove-conflict-ntml-type.patch"
        "0005-rename-interface-variable.patch"
        "0006-fix-manpage_formatting.patch"
        "0007-read-QuadPart-from-LARGE_INTEGER.patch")
sha256sums=('a1f52f0d9569b418a555ffe4d15a3782712198be47308e9514d20ca5af41a1b1'
            '9e71e16b23cb7e1285bc395370b78cc9ca72f897bb8650820d5b4d5e5fcb5176'
            '06d869d249484b57f8da868c8cd5498d96bc4f3bcf669139e2bc7da8bf48a9e6'
            '7b5b85a83af0d2243d334cc4b4cd80e95077ca00c161792cf27cee62c42eae0f'
            '923fd3cd86d6d5815b72f15b7fe7c226349b38ff09b7475b1143cb5d7c015738'
            '5b3fe9a7d4d5db2096b0786c88da30ff8c2819b1703aa995badd8f90550d56e1'
            'f091fcdf13a4693835ca1aa0a3d5ef2f15b97c77f91af5a937e3acfbded14678'
            '9c728341d0e7cd3896cee34b4dce78c18d5235a2cc336e2eae63e5fda354ac08')

prepare() {
  mv -v "FreeRDP-${pkgver}" "${_realname}-${pkgver}"
  cd "${_realname}-${pkgver}"

  # https://github.com/FreeRDP/FreeRDP/pull/6983
  patch -Np1 -i "${srcdir}/0001-fix-warnings-mingw.patch"

  # https://github.com/FreeRDP/FreeRDP/pull/7034
  patch -Np1 -i "${srcdir}/0002-fix-build-errors-mingw.patch"

  # Change to UTF-8 because patch can not. upstream #7034
  iconv -f UCS-2LE -t UTF-8 client/Windows/wfreerdp.rc  > client/Windows/wfreerdp.rc.mod
  sed -i '1s/^\xEF\xBB\xBF//' client/Windows/wfreerdp.rc.mod
  mv client/Windows/wfreerdp.rc.mod client/Windows/wfreerdp.rc

  # Duplicate macros
  patch -Np1 -i "${srcdir}/0003-remove-declspec-import-export.patch"

  # NTLM_AV_ID conflicts with MSV1_0_AVID
  patch -Np1 -i "${srcdir}/0004-remove-conflict-ntml-type.patch"

  # The variable name 'interface' assumed as keyword though it's C.
  patch -Np1 -i "${srcdir}/0005-rename-interface-variable.patch"

  # fix man page formatting:
  # https://bugs.archlinux.org/task/64814
  patch -Np1 -i "${srcdir}/0006-fix-manpage_formatting.patch"

  patch -Np1 -i "${srcdir}/0007-read-QuadPart-from-LARGE_INTEGER.patch"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  CFLAGS+=" -Wno-format" \
  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' \
    -DCMAKE_BUILD_TYPE='None' \
    -DPROXY_PLUGINDIR='${MINGW_PREFIX}/lib/freerdp2/server/proxy/plugins' \
    -DWITH_CAIRO=ON \
    -DWITH_ZLIB=ON \
    -DWITH_DSP_FFMPEG=ON \
    -DWITH_FFMPEG=ON \
    -DWITH_JPEG=ON \
    -DWITH_SERVER=ON \
    -DWITH_SWSCALE=ON \
    -DWITH_CHANNELS=ON \
    -DWITH_CLIENT_CHANNELS=ON \
    -DWITH_SERVER_CHANNELS=ON \
    -DCHANNEL_URBDRC_CLIENT=ON \
    "../${_realname}-${pkgver}"

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

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install
}
