# 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.23.0
_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
  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=('fc067df40bfed774ce2152004ae466653455ca67f99c0364973e232978a28070fc38e599ff2b1923bebd2e761c360d472198c9fc899e4997b1598bdf0d1d178b')
b2sums=('a298a00b6f0c506bf198309493eaf2b0580cf906e10f5876130e5cdc9dcc10c5e736dcb45d4d55a66ac91ca8fc27e903cf1d4fd0f57b4a58dac77d4858d4252f')

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/"
}
