# Maintainer: Ryan Hughes <ryan@omarchy.org>
# Contributor: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Contributor: Giancarlo Razzolini <grazzolini@archlinux.org>
# Contributor: Christian Heusel <gromit@archlinux.org>
#
# Arch builds steam for x86_64 only; this is the aarch64 build. It installs
# the same Valve launcher files (desktop entry, icons, udev rules) but swaps
# the x86 bootstrap for Valve's native arm64 client, which then updates itself
# from Valve's linuxarm64 client channel. _client is the client build the
# bootstrap was taken from; it only matters for the first launch, so it moves
# with the launcher rather than with every client release.

pkgbase=steam
pkgname=(steam steam-devices)
pkgver=1.0.0.87
pkgrel=1
_client=1788652215
_client_zip=bins_linuxarm64_linuxarm64.zip.dc817d33f8308815bf4fde6a3cb61fd4529728c8
pkgdesc="Valve's digital software delivery system"
url='https://steampowered.com/'
arch=('aarch64')
license=('LicenseRef-steam-subscriber-agreement')
# the beta directory is a superset of stable and also contains the stable releases
source=(
  "https://repo.steampowered.com/$pkgbase/archive/beta/${pkgbase}_$pkgver.tar.gz"
  "$pkgbase-client-$_client.zip::https://client-update.steamstatic.com/$_client_zip"
  steam.sh
)
noextract=("$pkgbase-client-$_client.zip")
# The client checksum is the sha2 Valve's steam_client_linuxarm64 manifest
# publishes for that zip.
sha256sums=('649375d2f9377f8009aaf3e2ff09978041eb9114897d9c5a3886f1d82e27ba1f'
            'c2d7c0ecb8de7e209504cd657b785127cd1ac3d905668c296e87c360b2b54163'
            '6f8d705e7d615b2debf8449898c51ad687136cd8f6605ef3c7573b4aa768e342')

build() {
  # The arm64 counterpart of Valve's bootstraplinux_ubuntu12_32.tar.xz. The
  # client binary links only glibc and downloads the rest of Steam itself.
  mkdir -p bootstrap
  bsdtar -xf "$pkgbase-client-$_client.zip" -C bootstrap steamrtarm64/steam
  chmod 755 bootstrap/steamrtarm64 bootstrap/steamrtarm64/steam
  tar --sort=name --mtime="@$SOURCE_DATE_EPOCH" --owner=0 --group=0 --numeric-owner \
    -cJf bootstraplinux_linuxarm64.tar.xz -C bootstrap steamrtarm64
}

package_steam() {
  # Libraries the arm64 client links against from the host, plus lsof, which
  # the client runs to find its own IPC ports.
  depends=(
    alsa-lib
    at-spi2-core
    bash
    cairo
    coreutils
    dbus
    fontconfig
    freetype2
    gdk-pixbuf2
    glib2
    glibc
    gtk2
    hicolor-icon-theme
    libcups
    libdrm
    libgcc
    libglvnd
    libibus
    libnm
    libpipewire
    libpulse
    libsm
    libstdc++
    libva
    libvdpau
    libx11
    libxcomposite
    libxdamage
    libxext
    libxfixes
    libxi
    libxinerama
    libxkbcommon
    libxrandr
    libxrender
    libxtst
    lsof
    mesa
    nss
    openal
    pango
    sdl2-compat
    systemd-libs
    tar
    ttf-font
    vulkan-driver
    vulkan-icd-loader
    xdg-user-dirs
    xz
  )
  optdepends=(
    'ntsync-autoload: for NTSync support'
    'polkit: to setup SteamVR without root access'
    'xdg-desktop-portal-impl: file & folder picker'
    'xorg-fonts-misc: for non-latin locales'
  )
  depends+=(steam-devices)

  cd "$pkgbase-launcher"
  make DESTDIR="$pkgdir" install-docs install-icons install-desktop install-appdata

  install -Dm755 "$srcdir/steam.sh" "$pkgdir/usr/bin/steam"
  install -Dm644 "$srcdir/bootstraplinux_linuxarm64.tar.xz" -t "$pkgdir/usr/lib/steam"

  install -d "$pkgdir/usr/share/licenses/$pkgname"
  ln -s "../../doc/$pkgname/steam_subscriber_agreement.txt" "$pkgdir/usr/share/licenses/$pkgname/"
  install -Dm644 debian/changelog -t "$pkgdir/usr/share/doc/$pkgname"
}

package_steam-devices() {
  pkgdesc="List of devices Steam and SteamVR will want read/write permissions on"
  license=(MIT)
  depends=(udev)

  cd "$srcdir/$pkgbase-launcher/subprojects/steam-devices"

  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -Dm644 60-steam-input.rules "$pkgdir/usr/lib/udev/rules.d/60-steam-input.rules"
  install -Dm644 60-steam-vr.rules "$pkgdir/usr/lib/udev/rules.d/60-steam-vr.rules"
}
