#!/usr/bin/env bash
# Stage dist/release for USB copy on Linux (includes linux-x64 payload + install.sh).
set -euo pipefail

VERSION="${1:-2.0.0}"
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
RELEASE_DIR="$ROOT/dist/release"

cd "$ROOT"
bash installer/linux/build-payload.sh "$VERSION" linux-x64

mkdir -p "$RELEASE_DIR"
cp installer/linux/install.sh "$RELEASE_DIR/install.sh"
cp installer/linux/install-from-folder.sh "$RELEASE_DIR/install-from-folder.sh"
chmod +x "$RELEASE_DIR/install.sh" "$RELEASE_DIR/install-from-folder.sh"
cp "$RELEASE_DIR/install.sh" "$RELEASE_DIR/setup.sh"

cat >"$RELEASE_DIR/manifest.json" <<EOF
{
  "version": "$VERSION",
  "arch": "linux-x64",
  "builtAt": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  "entry": {
    "linux": "install.sh",
    "linuxDeb": "dist/deb/siraya-printer_${VERSION}_amd64.deb",
    "windows": "Install.cmd"
  },
  "payload": "payload/linux-x64",
  "installTarget": "/opt/siraya/printer",
  "uiUrl": "http://127.0.0.1:5000"
}
EOF

echo "Release folder ready: $RELEASE_DIR"
echo "  USB install: sudo ./install.sh"
