#!/usr/bin/env bash
set -euo pipefail
X_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
MAIN_DIR="$X_DIR/apps/main"
say() { printf '\033[1;36m==>\033[0m %s\n' "$*"; }
die() { printf '\033[1;31merror:\033[0m %s\n' "$*" >&2; exit 1; }
command -v makepkg >/dev/null || die "makepkg not found — this script is for Arch Linux"
command -v pnpm >/dev/null || die "pnpm not found"
if pgrep -f "$MAIN_DIR/.package" >/dev/null 2>&1; then
echo "Processes are running from $MAIN_DIR/.package (probably the dev app):"
pgrep -af "$MAIN_DIR/.package" | head -5
read -r -p "Kill them and continue? [y/N] " reply || reply=n
[[ "$reply" == [yY]* ]] || die "quit the dev app first, then re-run"
pkill -f "$MAIN_DIR/.package" || true
sleep 2
fi
say "pnpm install"
(cd "$X_DIR" && pnpm install)
PACMAN_MAKER="$(cd "$MAIN_DIR" && node -p "require.resolve('./makers/maker-pacman.cjs')")"
say "electron-forge make (pacman only — this is the slow part)"
STAMP="$(mktemp)"
(cd "$MAIN_DIR" && npm run make -- --targets="$PACMAN_MAKER" < /dev/null)
PKG="$(find "$MAIN_DIR"/out/make/pacman -name '*.pkg.tar.zst' -newer "$STAMP" 2>/dev/null | head -1)"
rm -f "$STAMP"
[[ -n "$PKG" ]] || die "build finished but produced no fresh .pkg.tar.zst under $MAIN_DIR/out/make/pacman"
say "installing $(basename "$PKG")"
sudo pacman -U --noconfirm "$PKG"
say "done — restart the app to pick up the new version"
echo " (Spaces needs ROWBOAT_SPACES=1 in the environment, e.g. ~/.config/environment.d/rowboat.conf)"