#!/bin/bash
#
# make_release.sh — pack a redistributable Sprinter C Compiler release tarball.
#
# Whitelist-based: only files explicitly listed below go into the tarball.
# Anything else (internal dev tools, research docs, build artefacts, mac
# xattr sidecars, .git, .claude memory, etc.) is excluded by construction.
#
# Output: build/sprinter-c-v<VERSION>-<host>.tar.gz with:
#   - bin/sprinter-cc                  : compiler driver
#   - toolchain/mkexe/                 : .ihx → .exe utility (prebuilt for host)
#   - toolchain/check_banks.py         : bank size checker used by sprinter-cc
#   - runtime/                         : crt0 + bank/heap support
#   - libc/include/                    : public headers
#   - libc/{io,stdio,mem,gfx}/         : libc sources (assembled into sprinter.lib)
#   - lib/Makefile + lib/sprinter.lib  : prebuilt library + recipe to rebuild
#   - examples/                        : 27 ready-to-build programs
#   - third_party/sdcc/                : vendored SDCC 4.5
#   - release_docs/  →  docs/          : user-facing documentation
#   - examples.mk-style root Makefile, README, RELEASE_NOTES, LICENSE
#
# NOT included (developer-only):
#   - docs/ (research notes, original PetersPlus reference material)
#   - third_party/solid-c/ (reference for Solid-C compat target)
#   - mame/ (large emulator + Sprinter HDD/CD images)
#   - .git/, .claude/, .DS_Store, ._* (macOS xattrs)
#   - this script itself (toolchain/make_release.sh)
#   - any build artefacts (.sprinter-cc-*, *.rel, *.ihx, *.lk, *.map, ...)
#
# Usage: ./toolchain/make_release.sh [VERSION]

set -euo pipefail

VERSION="${1:-1.0}"
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
HOST_ARCH="$(uname -s | tr '[:upper:]' '[:lower:]')-$(uname -m)"
NAME="sprinter-c-v${VERSION}-${HOST_ARCH}"
STAGE="$ROOT/build/$NAME"
TARBALL="$ROOT/build/$NAME.tar.gz"

cd "$ROOT"

echo ">> verifying build is clean and current"
make all >/dev/null 2>&1

echo ">> staging $STAGE"
rm -rf "$STAGE"
mkdir -p "$STAGE"

# --- WHITELIST: only these directories/files end up in the tarball ---

# Top-level metadata
cp README.md RELEASE_NOTES.md LICENSE Makefile "$STAGE/"

# Toolchain
cp -R bin runtime libc examples "$STAGE/"
# toolchain/ — only mkexe (prebuilt + sources) and check_banks.py
mkdir -p "$STAGE/toolchain/mkexe"
cp toolchain/mkexe/mkexe          "$STAGE/toolchain/mkexe/"
cp toolchain/mkexe/mkexe.c        "$STAGE/toolchain/mkexe/"
cp toolchain/mkexe/Makefile       "$STAGE/toolchain/mkexe/"
[[ -f toolchain/mkexe/check.sh ]] && cp toolchain/mkexe/check.sh "$STAGE/toolchain/mkexe/"
cp toolchain/check_banks.py       "$STAGE/toolchain/"

# Lib — keep Makefile and the prebuilt archive only; sources are in libc/
mkdir -p "$STAGE/lib"
cp lib/Makefile        "$STAGE/lib/"
cp lib/sprinter.lib    "$STAGE/lib/"

# Vendored SDCC — follow symlinks so the real content lands in the tarball
mkdir -p "$STAGE/third_party"
[[ -d third_party/sdcc ]]            && cp -RL third_party/sdcc            "$STAGE/third_party/"
[[ -f third_party/setup-sdcc.sh ]]   && cp     third_party/setup-sdcc.sh   "$STAGE/third_party/"

# User-facing docs.  Source: release_docs/{en,ru}/  →  docs/{en,ru}/ in tarball.
# The internal docs/ tree (research notes, original Peters Plus material) is
# NOT shipped.
[[ -d release_docs ]] && cp -R release_docs "$STAGE/docs"

# --- Strip build artefacts that crept in via examples/ ---
echo ">> stripping build artefacts and macOS metadata"
find "$STAGE" \( \
        -name '.sprinter-cc-*' -o \
        -name '*.rel' -o -name '*.lst' -o -name '*.sym' -o -name '*.asm' -o \
        -name '*.ihx' -o -name '*.lk' -o -name '*.map' -o -name '*.noi' -o \
        -name '*.exe' -o \
        -name '.DS_Store' -o -name '._*' \
    \) -print0 | xargs -0 rm -rf

# Bonus: clean any *_test or scratch files I might have left in libc dirs
find "$STAGE/libc" -name 'build' -type d -exec rm -rf {} + 2>/dev/null || true

# --- Tarball ---
echo ">> packaging $TARBALL"
cd "$ROOT/build"
COPYFILE_DISABLE=1 tar --no-mac-metadata -czf "$TARBALL" "$NAME" 2>/dev/null || \
    COPYFILE_DISABLE=1 tar -czf "$TARBALL" "$NAME"
rm -rf "$NAME"

ls -lh "$TARBALL"
echo
echo "Release ready: $TARBALL"
echo
echo "Try it:"
echo "  tar xzf $(basename "$TARBALL")"
echo "  cd $NAME"
echo "  make all"
echo "  bin/sprinter-cc -o hello.exe examples/hello/hello.c"
