#!/usr/bin/env bash
# Build a portable archive (no node_modules, no .git) for USB → Windows/Linux copy.
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
STAMP="$(date +%Y%m%d-%H%M%S)"
OUT_BASE="$(cd "$ROOT/.." && pwd)/Production Builds/Thermal Printer USB"
STAGE="$(mktemp -d)"
BUNDLE_NAME="siraya-thermal-printer-service"
ARCHIVE="siraya-thermal-printer-usb-${STAMP}.tar.gz"

mkdir -p "$OUT_BASE"

rsync -a \
  --exclude='.git' \
  --exclude='node_modules' \
  --exclude='printer.js.backup' \
  --exclude='.env' \
  --exclude='dist' \
  "$ROOT/" "$STAGE/$BUNDLE_NAME/"

# Ensure instructions are inside the bundle root
cp -f "$ROOT/COPY_TO_WINDOWS.txt" "$STAGE/$BUNDLE_NAME/COPY_TO_WINDOWS.txt"

tar -C "$STAGE" -czf "$OUT_BASE/$ARCHIVE" "$BUNDLE_NAME"
rm -rf "$STAGE"

echo "Created: $OUT_BASE/$ARCHIVE"
ls -lh "$OUT_BASE/$ARCHIVE"
