# 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.24.2
_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
  libgcc
  libstdc++
  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=('2828ec1c5c14e0ef8eece34949b33339bf5312d0c6eb93da0d6e7c45a098641108e57ce9cb7866391bb8aad39e31a3677ab32af4e0f7e74b80f3507f68ab3c5f')
b2sums=('497a4025f89bee005d0cda3b99bf9923b5171b17838479058d213f183c2fbd4a229e1210046a0cd5c7cc2c8e731a6c588888d11f38b156e5c756bea048741fb8')

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