# Maintainer: Felitendo
# This PKGBUILD is updated automatically:
# https://git.felo.gg/Felitendo/PKGBUILDS

pkgname=faugus-launcher-bin
pkgver=2.4.2
pkgrel=1
pkgdesc="Simple and lightweight app for running Windows games using UMU-Launcher (upstream binary)"
arch=('any')
url="https://github.com/Faugus/faugus-launcher"
license=('MIT')
depends=('gtk4' 'libadwaita' 'libmanette' 'icoextract' 'python'
         'python-gobject' 'python-requests' 'python-pillow' 'python-vdf'
         'python-psutil' 'python-dbus' 'umu-launcher')
optdepends=('mangohud: performance overlay'
            'gamemode: feral gamemode integration')
provides=('faugus-launcher')
conflicts=('faugus-launcher')
# The Debian revision in the asset name does not follow from pkgver, so the
# exact name is tracked here and refreshed automatically.
_asset="faugus-launcher_2.4.2-1_all.deb"
source=("${pkgname}-${pkgver}.deb::${url}/releases/download/${pkgver}/${_asset}")
noextract=("${pkgname}-${pkgver}.deb")
sha256sums=('06d41869f42c2078a36a366b38ee62b7aa6dc98e08bb4a1346d7eb193ff8f145')

package() {
  local _deb="$srcdir/deb"

  install -d "$_deb"
  bsdtar -xOf "$srcdir/${pkgname}-${pkgver}.deb" 'data.tar.*' \
    | bsdtar -xpf - -C "$_deb" ./usr

  # Debian puts the modules in dist-packages, which is not on Arch's
  # sys.path. A fixed private directory also keeps the package working
  # across Python minor bumps, since it is only rebuilt for new releases.
  install -d "$pkgdir/usr/lib/faugus-launcher"
  cp -a "$_deb/usr/lib/python3/dist-packages/faugus" "$pkgdir/usr/lib/faugus-launcher/"
  # .pyc compiled for Debian's Python, useless here
  find "$pkgdir/usr/lib/faugus-launcher" -type d -name __pycache__ -exec rm -rf {} +

  cp -a "$_deb/usr/share" "$pkgdir/usr/"
  # generated by pacman hooks; shipping them would cause file conflicts
  rm -f "$pkgdir/usr/share/applications/mimeinfo.cache" \
        "$pkgdir/usr/share/icons/hicolor/icon-theme.cache"

  # upstream's wrapper relies on Debian's default sys.path, ours points at
  # the directory above; the rest matches upstream verbatim
  install -d "$pkgdir/usr/bin"
  cat > "$pkgdir/usr/bin/faugus-launcher" << 'EOF'
#!/bin/sh

export PYTHONPATH="/usr/lib/faugus-launcher${PYTHONPATH:+:$PYTHONPATH}"
export GTK_IM_MODULE="${GTK_IM_MODULE:-simple}"

case "$1" in
	--shortcut) exec /usr/bin/python3 -m faugus.shortcut "$2";;
	--game)     exec /usr/bin/python3 -m faugus.runner --game "$2";;
	--run)      msg="$2"; shift 2; exec /usr/bin/python3 -m faugus.runner "$msg" "$@";;
	*)          exec /usr/bin/python3 -m faugus.launcher "$@";;
esac
EOF
  chmod 755 "$pkgdir/usr/bin/faugus-launcher"

  mv "$pkgdir/usr/share/licenses/faugus-launcher" \
     "$pkgdir/usr/share/licenses/$pkgname"
}
