pkgname=open-orpheus
pkgver=0.16.2
pkgrel=2
pkgdesc="An open-source implementation of Netease Cloud Music's Orpheus browser host"
arch=('x86_64')
url="https://github.com/YUCLing/open-orpheus"
license=('MIT')
_electronver=43
depends=(
  'electron43'
  'gtk3'
  'libnotify'
  'nss'
  'xdg-utils'
  'at-spi2-core'
  'libdrm'
  'mesa'
  'libxcb'
)
optdepends=(
  'kde-cli-tools: KDE integration'
)
makedepends=(
  'nodejs>=24'
  'pnpm'
  'rust'
  'rust-wasm'
  'wasm-bindgen'
  'libarchive'
  'git'
)
source=(
  "$pkgname-$pkgver.tar.gz::https://github.com/YUCLing/open-orpheus/archive/refs/tags/v$pkgver.tar.gz"
  "open-orpheus.sh"
  "open-orpheus.desktop"
)
sha256sums=('b79d1d98f8bac00cc4a23d43e120d1e46711194db53fc8a00159e9e1f3a51ebb'
            '1af66959f8bd34896684c154d9169cdbad94b5ec08e107c4652ba5473e8d6ded'
            'de9b00ee7e7025cbcadda8cd16b63aa4092fb905803d0e6f4f570ab8461a5f5c')

prepare() {
  cd "$pkgname-$pkgver"

  # Read the exact Electron version the project pins. Only check that its
  # major version matches the system Electron we build against, so a minor
  # or patch bump in package.json does not fail the build.
  electron_npmver=$(node -p "require('./package.json').devDependencies.electron")
  electron_npmver="${electron_npmver#\^}"
  if [ "${electron_npmver%%.*}" != "$_electronver" ]; then
    echo "Electron major version mismatch in package.json (expected $_electronver.x, got $electron_npmver)" >&2
    exit 1
  fi

  # Skip Electron's postinstall, which would download Electron from GitHub;
  # we build and run against the system Electron instead.
  sed -i 's/electron: true/electron: false/' pnpm-workspace.yaml

  # Make electron-forge package from a local zip of the system Electron
  # instead of downloading it. ELECTRON_ZIP_DIR is read from the environment.
  sed -i 's|^  packagerConfig: {$|  packagerConfig: {\n    electronZipDir: process.env.ELECTRON_ZIP_DIR,|' forge.config.ts

  # Create the Electron zip that electron-packager will extract. Entry names
  # must not be prefixed with "./" (extract-zip rejects those).
  zipdir="$srcdir/electron-zip"
  mkdir -p "$zipdir"
  (
    cd "/usr/lib/electron$_electronver"
    find . -mindepth 1 -print | sed 's|^\./||' |
      bsdtar --format=zip -cf "$zipdir/electron-v$electron_npmver-linux-x64.zip" -T -
  )

  export ELECTRON_ZIP_DIR="$zipdir"
  export ELECTRON_OVERRIDE_DIST_PATH="/usr/lib/electron$_electronver"
  pnpm install --frozen-lockfile

  # Pre-seed the electron npm package so `require('electron')` resolves to
  # the system binary instead of trying to download it.
  mkdir -p node_modules/electron/dist
  printf '%s' "$electron_npmver" >node_modules/electron/dist/version
  printf 'electron' >node_modules/electron/path.txt
}

build() {
  cd "$pkgname-$pkgver"
  export ELECTRON_ZIP_DIR="$srcdir/electron-zip"
  export ELECTRON_OVERRIDE_DIST_PATH="/usr/lib/electron$_electronver"

  # The wasm32 rust-lld used by the audio-effect module rejects
  # `-Wl,--icf=safe`, which distro makepkg.conf files (e.g. CachyOS) inject
  # via RUSTFLAGS. Drop it for the native-module build.
  ORIGINAL_RUSTFLAGS="$RUSTFLAGS"
  RUSTFLAGS="${RUSTFLAGS//-C link-arg=-Wl,--icf=safe/}"
  RUSTFLAGS="${RUSTFLAGS//-Clink-arg=-Wl,--icf=safe/}"
  export RUSTFLAGS

  # Native modules (Rust napi + wasm) then the Forge package.
  pnpm run build:modules

  RUSTFLAGS="$ORIGINAL_RUSTFLAGS"
  export RUSTFLAGS
  pnpm run package
}

package() {
  cd "$pkgname-$pkgver"
  install -dm755 "$pkgdir/usr/lib/$pkgname/resources"
  cp -r out/"$pkgname"-linux-*/resources/app.asar "$pkgdir/usr/lib/$pkgname/resources/"
  cp -r out/"$pkgname"-linux-*/resources/app.asar.unpacked "$pkgdir/usr/lib/$pkgname/resources/"

  install -Dm755 "$srcdir/open-orpheus.sh" "$pkgdir/usr/bin/$pkgname"
  install -Dm644 "$srcdir/open-orpheus.desktop" "$pkgdir/usr/share/applications/$pkgname.desktop"
  install -Dm644 assets/icon_256.png "$pkgdir/usr/share/icons/hicolor/256x256/apps/$pkgname.png"
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
