# Edit on github: https://github.com/LizardByte/Sunshine/tree/nightly/packaging/linux/aur/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=('GPL3')

depends=('avahi' 'boost-libs' 'curl' 'libappindicator-gtk3' 'libevdev' 'libmfx' 'libpulse' 'libva' 'libvdpau' 'libx11' 'libxcb' 'libxfixes' 'libxrandr' 'libxtst' 'numactl' 'openssl' 'opus' 'udev')
makedepends=('boost' 'cmake' 'git' 'make' 'nodejs' 'npm')
optdepends=('cuda: NvFBC capture support'
            'libcap'
            'libdrm')

provides=('sunshine')

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

prepare() {
    cd "$pkgname"
    # Skip submodules that we don't want
    if [[ $CARCH == "x86_64" ]]; then
        git -c submodule."ffmpeg-macos-x86_64".update=none \
            -c submodule."ffmpeg-windows-x86_64".update=none \
            -c submodule."ffmpeg-linux-aarch64".update=none \
            -c submodule."ffmpeg-macos-aarch64".update=none \
            submodule update --recursive --init
    elif [[ $CARCH == "aarch64" ]]; then
        git -c submodule."ffmpeg-macos-x86_64".update=none \
            -c submodule."ffmpeg-windows-x86_64".update=none \
            -c submodule."ffmpeg-linux-x86_64".update=none \
            -c submodule."ffmpeg-macos-aarch64".update=none \
            submodule update --recursive --init

    # It's unlikely that someone could get this far on a system with an incorrect arch, but we should handle it anyway
    # Pull linux ffmpeg submodules
    else
        git -c submodule."ffmpeg-macos-x86_64".update=none \
            -c submodule."ffmpeg-windows-x86_64".update=none \
            -c submodule."ffmpeg-macos-aarch64".update=none \
            submodule update --recursive --init
    fi
}

build() {
    pushd "$pkgname"
    npm install
    popd

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

    cmake \
        -S "$pkgname" \
        -B build \
        -Wno-dev \
        -D CMAKE_INSTALL_PREFIX=/usr \
        -D SUNSHINE_EXECUTABLE_PATH=/usr/bin/sunshine \
        -D SUNSHINE_ASSETS_DIR="share/sunshine"

    make -C build
}

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