# Maintainer: Duncan Tourolle <duncan@tourolle.paris>
#
# BikeControl — indoor cycling trainer control (Tauri + Svelte + Rust).
#
# Tauri's bundler has no pacman target, so this PKGBUILD builds a real
# .pkg.tar.zst with makepkg. It builds from the *local working tree* (pointed at
# by $BIKECONTROL_SRC), which is what scripts/build-arch.sh sets up. For AUR
# distribution, replace this with a source=() release tarball / VCS URL.

pkgname=bikecontrol
# scripts/build-arch.sh exports BIKECONTROL_PKGVER, derived from git: the tag on
# master/main, the branch name on a feature branch. The fallback below only
# applies when makepkg is run by hand outside that script.
pkgver="${BIKECONTROL_PKGVER:-0.1.0}"
pkgrel=1
pkgdesc="Indoor cycling trainer control: ride gradient profiles over BLE FTMS and record to FIT"
arch=('x86_64')
url="https://github.com/dtourolle/BikeControl"
license=('MIT')
# Runtime: webkit2gtk for the webview, gtk3 for the shell window.
depends=('webkit2gtk-4.1' 'gtk3')
makedepends=('rust' 'npm' 'nodejs' 'pkgconf')
optdepends=('bluez: talk to a real smart trainer over BLE'
            'bluez-utils: bluetoothctl, for pairing and troubleshooting')
# !lto: the release profile already enables Rust LTO; makepkg's C LTO flags only
# risk mismatches with cc-rs-built deps. !strip: keep it consistent with what
# `cargo build --release` produced.
options=('!strip' '!lto')

# Populated from the working tree by scripts/build-arch.sh.
_srcdir="${BIKECONTROL_SRC:-$startdir/../..}"

build() {
  cd "$_srcdir"
  export CARGO_HOME="${CARGO_HOME:-$srcdir/cargo-home}"

  # The frontend must exist before cargo builds: tauri.conf.json points
  # frontendDist at ../ui/dist and tauri-build embeds it into the binary.
  # `cargo build` does not run beforeBuildCommand, so build the UI here.
  npm --prefix ui ci || npm --prefix ui install
  npm --prefix ui run build

  # Only the raw binary is needed; the Arch filesystem layout is done in
  # package() below rather than via tauri-bundler.
  cargo build --release --locked -p bikecontrol-app
}

package() {
  cd "$_srcdir"

  # Cargo names the binary after the crate (bikecontrol-app); install it under
  # the shorter user-facing name the .desktop entry execs.
  install -Dm755 "target/release/bikecontrol-app" \
    "$pkgdir/usr/bin/bikecontrol"

  install -Dm644 "packaging/arch/bikecontrol.desktop" \
    "$pkgdir/usr/share/applications/bikecontrol.desktop"

  # Icons (hicolor)
  install -Dm644 "src-tauri/icons/32x32.png" \
    "$pkgdir/usr/share/icons/hicolor/32x32/apps/bikecontrol.png"
  install -Dm644 "src-tauri/icons/128x128.png" \
    "$pkgdir/usr/share/icons/hicolor/128x128/apps/bikecontrol.png"
  install -Dm644 "src-tauri/icons/128x128@2x.png" \
    "$pkgdir/usr/share/icons/hicolor/256x256/apps/bikecontrol.png"

  # Example ride profiles, so a fresh install has something to load.
  for _p in profiles/*.yaml; do
    install -Dm644 "$_p" "$pkgdir/usr/share/bikecontrol/profiles/$(basename "$_p")"
  done

  if [ -f LICENSE ]; then
    install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  fi
}
