#!/usr/bin/env bash
set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
RELEASE="${SOURCE_RELEASE:-2026-09-04}"
NAME="toshinsamuels-com-source-${RELEASE}.zip"
OUT_DIR="$ROOT/public/source"
TMP="${TMPDIR:-/tmp}/$NAME"

mkdir -p "$OUT_DIR"
rm -f "$OUT_DIR/$NAME" "$OUT_DIR/${NAME%.zip}.sha256" "$TMP"

cd "$ROOT"
zip -qr "$TMP" . \
  -x 'node_modules/*' '.next/*' '.git/*' '.vercel/*' \
     '.env' '.env.*' '*.DS_Store' \
     "public/source/$NAME" \
     "public/source/${NAME%.zip}.sha256"

cp "$TMP" "$OUT_DIR/$NAME"
if command -v sha256sum >/dev/null 2>&1; then
  HASH="$(sha256sum "$TMP" | awk '{print $1}')"
else
  HASH="$(shasum -a 256 "$TMP" | awk '{print $1}')"
fi
printf '%s  %s\n' "$HASH" "$NAME" > "$OUT_DIR/${NAME%.zip}.sha256"

printf 'Created %s\nSHA-256 %s\n' "$OUT_DIR/$NAME" "$HASH"
