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

## options
: "${_run_unit_tests:=false}" # if set to true; unit tests will be executed post build; useful in CI
: "${_support_headless_testing:=false}"
: "${_use_cuda:=detect}" # nvenc

: "${_commit:=86188d47a7463b0f73b35de18a628353adeaa20e}"

pkgname='sunshine'
pkgver=2025.924.154138
pkgrel=3
pkgdesc="Self-hosted game stream host for Moonlight"
arch=('x86_64' 'aarch64')
url=https://app.lizardbyte.dev/Sunshine
license=('GPL-3.0-only')
install=sunshine.install

# this variable remains for future cases where we need an older version of gcc for cuda compatibility
_gcc_version=15
_versioned_gcc=false  # set to true if we need a versioned gcc, e.g. gcc14

_gcc_dep_suffix=""
_gcc_env_suffix=""
if [ "${_versioned_gcc}" == true ]; then
  _gcc_dep_suffix="${_gcc_version}"
  _gcc_env_suffix="-${_gcc_version}"
fi

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

makedepends=(
  'appstream'
  'appstream-glib'
  'cmake'
  'desktop-file-utils'
  "gcc${_gcc_dep_suffix}"
  'git'
  'make'
  'nodejs'
  'npm'
)

optdepends=(
  'libva-mesa-driver: AMD GPU encoding support'
)

provides=()
conflicts=()

source=("$pkgname::git+https://github.com/LizardByte/Sunshine.git#commit=${_commit}")
sha256sums=('SKIP')

# Options Handling
if [[ "${_use_cuda::1}" == "d" ]] && pacman -Qi cuda &> /dev/null; then
  _use_cuda=true
fi

if [[ "${_use_cuda::1}" == "t" ]]; then
  optdepends+=(
    'cuda: Nvidia GPU encoding support'
  )
fi

if [[ "${_support_headless_testing::1}" == "t" ]]; then
  optdepends+=(
    'xorg-server-xvfb: Virtual X server for headless testing'
  )
fi

# Ensure makedepends, checkdepends, optdepends are sorted
if [ -n "${makedepends+x}" ]; then
  mapfile -t tmp_array < <(printf '%s\n' "${makedepends[@]}" | sort)
  makedepends=("${tmp_array[@]}")
  unset tmp_array
fi

if [ -n "${optdepends+x}" ]; then
  mapfile -t tmp_array < <(printf '%s\n' "${optdepends[@]}" | sort)
  optdepends=("${tmp_array[@]}")
  unset tmp_array
fi

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

build() {
    export BRANCH="master"
    export BUILD_VERSION="2025.924.154138"
    export COMMIT="${_commit}"

    export CC="gcc${_gcc_env_suffix}"
    export CXX="g++${_gcc_env_suffix}"

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

    export MAKEFLAGS="${MAKEFLAGS:--j$(nproc)}"

    local _cmake_options=(
      -S "$pkgname"
      -B build
      -Wno-dev
      -D BUILD_DOCS=OFF
      -D BUILD_WERROR=ON
      -D CMAKE_INSTALL_PREFIX=/usr
      -D SUNSHINE_COMPILE_OPTIONS="-Wno-uninitialized"
      -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'
    )

    if [[ "${_use_cuda::1}" != "t" ]]; then
      _cmake_options+=(-DSUNSHINE_ENABLE_CUDA=OFF -DCUDA_FAIL_ON_MISSING=OFF)
    else
      # If cuda has just been installed, its variables will not be available in the environment
      # therefore, set them manually to the expected values on Arch Linux
      if [ -z "${CUDA_PATH:-}" ] && pacman -Qi cuda &> /dev/null; then
        local _cuda_gcc_version
        _cuda_gcc_version="$(LC_ALL=C pacman -Si cuda | grep -Pom1 '^Depends On\s*:.*\bgcc\K[0-9]+\b')"

        export CUDA_PATH=/opt/cuda
        export NVCC_CCBIN="/usr/bin/g++-${_cuda_gcc_version}"
      fi
    fi

    if [[ "${_run_unit_tests::1}" != "t" ]]; then
      _cmake_options+=(-DBUILD_TESTS=OFF)
    fi

    cmake "${_cmake_options[@]}"

    appstreamcli validate "build/dev.lizardbyte.app.Sunshine.metainfo.xml"
    appstream-util validate "build/dev.lizardbyte.app.Sunshine.metainfo.xml"
    desktop-file-validate "build/dev.lizardbyte.app.Sunshine.desktop"
    desktop-file-validate "build/dev.lizardbyte.app.Sunshine.terminal.desktop"

    make -C build
}

check() {
    cd "${srcdir}/build"
    ./sunshine --version

    if [[ "${_run_unit_tests::1}" == "t" ]]; then
      export CC="gcc${_gcc_env_suffix}"
      export CXX="g++${_gcc_env_suffix}"

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

package() {
    export MAKEFLAGS="${MAKEFLAGS:--j$(nproc)}"
    make -C build install DESTDIR="$pkgdir"
}
