#!/usr/bin/env bash
# Package cross-built Windows exe + WebView2 loader for end-user portable install.
set -euo pipefail

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
VERSION="$(node -p "require('$REPO_ROOT/package.json').version")"
TARGET_DIR="$REPO_ROOT/src-tauri/target/x86_64-pc-windows-gnu/release"
EXE="$TARGET_DIR/vigthoria-workbench.exe"
DLL="$TARGET_DIR/WebView2Loader.dll"

if [[ ! -f "$EXE" ]]; then
  echo "Build first: scripts/release/build-windows-gnu.sh (or use GitHub Actions for NSIS setup.exe)"
  exit 1
fi

STAGE=$(mktemp -d)
trap 'rm -rf "$STAGE"' EXIT
INSTALL_NAME="Vigthoria Workbench"
cp -f "$EXE" "$STAGE/$INSTALL_NAME.exe"
[[ -f "$DLL" ]] && cp -f "$DLL" "$STAGE/"
ICON="$REPO_ROOT/src-tauri/icons/icon.ico"
[[ -f "$ICON" ]] && cp -f "$ICON" "$STAGE/icon.ico"

OUT="$REPO_ROOT/vigthoria-workbench-${VERSION}-win64-portable.zip"
rm -f "$OUT"
(cd "$STAGE" && zip -q -r "$OUT" .)
echo "Created $OUT ($(du -h "$OUT" | awk '{print $1}'))"
ls -la "$OUT"
