# Maintainer: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: David Runge <dvzrv@archlinux.org>
# Contributor: Stijn Segers <francesco dot borromini at gmail dot com>

pkgname=freerdp
pkgver=3.19.1
_libver=${pkgver/.*/}
pkgrel=1
epoch=2
pkgdesc="Free implementation of the Remote Desktop Protocol (RDP)"
arch=(x86_64 aarch64)
url="https://www.freerdp.com/"
license=(Apache-2.0)
depends=(
  fuse3
  gcc-libs
  glibc
  jansson
  libcups
  libx11
  libxcursor
  libxext
  libxdamage
  libxfixes
  libxkbcommon
  libxi
  libxinerama
  libxkbfile
  libxrandr
  libxrender
  libxtst
  sdl3
  sdl3_ttf
  wayland
)
makedepends=(
  alsa-lib
  cmake
  ffmpeg
  git
  icu
  krb5
  libjpeg-turbo
  libp11
  libpng
  libpulse
  libusb
  libwebp
  ninja
  openssl
  pam
  pkcs11-helper
  xorgproto
  zlib
)
provides=(
  libfreerdp-client$_libver.so
  libfreerdp-server-proxy$_libver.so
  libfreerdp-server$_libver
  libfreerdp-shadow-subsystem$_libver.so
  libfreerdp-shadow$_libver.so
  libfreerdp$_libver.so
  libwinpr-tools$_libver.so
  libwinpr$_libver.so
)
source=(
  "git+https://github.com/$pkgname/$pkgname#tag=$pkgver"
)
sha512sums=('987d9337ccb8db11ab2308165b8deb56086795a17a042371983584e50f4846f3a7daf0435f94881c447d9fa3afb44f99c8d0112eaf76d2296265d600ebc0b450')
b2sums=('dce17e6838a7bd7064deba9a0845eabe6c6a881db3fb7447c22ef4b85e82ff3e37d1f69adba56f0059b8d23a71bf2e26aa33c208e4756acb837e10cbe73b9409')

prepare() {
  # Fix building downstream packages
  # https://github.com/FreeRDP/FreeRDP/pull/11876
  git -C $pkgname cherry-pick -n 74a3a7695cdd09cf020acdeb09480ac8ee92ccbe
}

build() {
  # gcc14 buildfix
  export CFLAGS+=" -Wno-incompatible-pointer-types"
  export CXXFLAGS+=" -Wno-incompatible-pointer-types"

  local cmake_options=(
    -B build
    -D CHANNEL_RDPECAM_CLIENT=ON
    -D CHANNEL_URBDRC_CLIENT=ON
    -D CMAKE_BUILD_TYPE=Release
    -D CMAKE_INSTALL_LIBDIR=lib
    -D CMAKE_INSTALL_PREFIX=/usr
    -D CMAKE_SKIP_INSTALL_RPATH=ON
    -D PROXY_PLUGINDIR=/usr/lib/$pkgname/server/proxy/plugins
    -D RDTK_FORCE_STATIC_BUILD=ON  # prevent file conflicts with freerdp2
    -D UWAC_FORCE_STATIC_BUILD=ON  # prevent file conflicts with freerdp2
    -D WINPR_UTILS_IMAGE_JPEG=ON
    -D WINPR_UTILS_IMAGE_PNG=ON
    -D WINPR_UTILS_IMAGE_WEBP=ON
    -D WITH_ALSA=ON
    -D WITH_BINARY_VERSIONING=ON  # prevent file conflicts with freerdp2
    -D WITH_CHANNELS=ON
    -D WITH_CLIENT_CHANNELS=ON
    -D WITH_CLIENT_SDL3=ON
    -D WITH_CLIENT_SDL2=OFF
    -D WITH_CUPS=ON
    -D WITH_DSP_FFMPEG=ON
    -D WITH_FFMPEG=ON
    -D WITH_FUSE=ON
    -D WITH_ICU=ON
    -D WITH_JPEG=ON
    -D WITH_PCSC=ON
    -D WITH_PULSE=ON
    -D WITH_SERVER=ON
    -D WITH_SERVER_CHANNELS=ON
    -D WITH_SWSCALE=ON
    -D WITH_SYSTEMD=ON
    -D WITH_VAAPI=ON
    -D WITH_VERBOSE_WINPR_ASSERT=OFF
    -D WITH_WAYLAND=ON
    -D WITH_WINPR_TOOLS=ON
    -D WITH_X11=ON
    -G Ninja
    -S $pkgname
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

check() {
  ctest --test-dir build --output-on-failure --stop-on-failure -j$(nproc)
}

package() {
  depends+=(
    alsa-lib libasound.so
    ffmpeg libavcodec.so libavutil.so libswresample.so libswscale.so
    icu libicuuc.so
    krb5 libk5crypto.so libkrb5.so
    libjpeg-turbo libjpeg.so
    libpng libpng16.so
    libpulse libpulse.so
    libusb libusb-1.0.so
    libwebp libwebp.so
    openssl libcrypto.so libssl.so
    pam libpam.so
    zlib libz.so
  )

  DESTDIR="$pkgdir" cmake --install build
  install -vDm 644 $pkgname/{ChangeLog,README.md} -t "$pkgdir/usr/share/doc/$pkgname/"
}
