# Edit on github: https://github.com/LizardByte/Sunshine/blob/master/packaging/linux/Arch/PKGBUILD
# Reference: https://wiki.archlinux.org/title/PKGBUILD

pkgname='sunshine'
pkgver=@PROJECT_VERSION@@SUNSHINE_SUB_VERSION@
pkgrel=1
pkgdesc="@PROJECT_DESCRIPTION@"
arch=('x86_64' 'aarch64')
url=@PROJECT_HOMEPAGE_URL@
license=('GPL-3.0-only')
install=sunshine.install

_gcc_version=13

depends=(
  'avahi'
  'curl'
  'libayatana-appindicator'
  'libcap'
  'libdrm'
  'libevdev'
  'libmfx'
  'libnotify'
  'libpulse'
  'libva'
  'libvdpau'
  'libx11'
  'libxcb'
  'libxfixes'
  'libxrandr'
  'libxtst'
  'miniupnpc'
  'numactl'
  'openssl'
  'opus'
  'udev'
)

makedepends=(
  'cmake'
  'cuda'
  "gcc${_gcc_version}"
  'git'
  'make'
  'nodejs'
  'npm'
)

optdepends=(
  'cuda: Nvidia GPU encoding support'
  'libva-mesa-driver: AMD GPU encoding support'
  'xorg-server-xvfb: Virtual X server for headless testing'
)

provides=()
conflicts=()

source=("$pkgname::git+@GITHUB_CLONE_URL@#commit=@GITHUB_COMMIT@")
sha256sums=('SKIP')

prepare() {
    cd "$pkgname"
    git submodule update --recursive --init
}

build() {
    export BRANCH="@GITHUB_BRANCH@"
    export BUILD_VERSION="@GITHUB_BUILD_VERSION@"
    export COMMIT="@GITHUB_COMMIT@"

    export CC="gcc-${_gcc_version}"
    export CXX="g++-${_gcc_version}"

    export CFLAGS="${CFLAGS/-Werror=format-security/}"
    export CXXFLAGS="${CXXFLAGS/-Werror=format-security/}"

    cmake \
        -S "$pkgname" \
        -B build \
        -Wno-dev \
        -D BUILD_DOCS=OFF \
        -D BUILD_WERROR=ON \
        -D CMAKE_INSTALL_PREFIX=/usr \
        -D SUNSHINE_EXECUTABLE_PATH=/usr/bin/sunshine \
        -D SUNSHINE_ASSETS_DIR="share/sunshine" \
        -D SUNSHINE_PUBLISHER_NAME='LizardByte' \
        -D SUNSHINE_PUBLISHER_WEBSITE='https://app.lizardbyte.dev' \
        -D SUNSHINE_PUBLISHER_ISSUE_URL='https://app.lizardbyte.dev/support'

    make -C build
}

check() {
    export CC="gcc-${_gcc_version}"
    export CXX="g++-${_gcc_version}"

    cd "${srcdir}/build/tests"
    ./test_sunshine --gtest_color=yes
}

package() {
    make -C build install DESTDIR="$pkgdir"
}
