#!/usr/bin/env bash
# Top-level installer for the llvm816 project. Installs everything into
# ./tools/ so the tree is self-contained and deletable.
#
# Stages (5/5):
#   1. apt dependencies
#   2. llvm-mos clone + W65816 backend apply + cmake/ninja build of
#      clang/llc/llvm-mc + build link816/omfEmit + build runtime (.o)
#   3. MAME + apple2gs ROMs
#   4. Calypsi 5.16 (reference compiler — optional)
#   5. GNO/ME 2.0.6 + GS/OS 6.0.4 (shell-command runtime — optional;
#      cadius + .shk archives + system disks for runInGno.sh)
#
# Toolbox bindings (iigs/toolbox.h) are generated from NList.Data — a
# non-ORCA, non-OPUS toolbox database checked into tools/nlist/.  No
# Byte Works / ORCA / OPUS sources are required or installed.
#
# Usage:
#   ./setup.sh                    # install + build everything (~30-60 min)
#   ./setup.sh --skip-deps        # skip apt packages
#   ./setup.sh --skip-llvm        # skip stage 2 (clang/runtime/link816)
#   ./setup.sh --skip-mame        # skip MAME + ROM fetch
#   ./setup.sh --skip-calypsi     # skip Calypsi
#   ./setup.sh --skip-gno         # skip GNO/ME + GS/OS 6.0.4 fetch
#   ./setup.sh --skip-verify      # skip the post-install verification
#   ./setup.sh --verify-only      # run verification only
#   ./setup.sh --build-llvm       # deprecated alias for the default behavior

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/scripts/common.sh"

doDeps=1
doLlvm=1
doMame=1
doCalypsi=1
doGno=1
doVerify=1
buildLlvm=0
verifyOnly=0

for arg in "$@"; do
    case "$arg" in
        --skip-deps)     doDeps=0 ;;
        --skip-llvm)     doLlvm=0 ;;
        --skip-mame)     doMame=0 ;;
        --skip-calypsi)  doCalypsi=0 ;;
        --skip-gno)      doGno=0 ;;
        --skip-verify)   doVerify=0 ;;
        --build-llvm)    buildLlvm=1 ;;
        --verify-only)   verifyOnly=1 ;;
        -h|--help)
            sed -n '2,25p' "$0"
            exit 0
            ;;
        *) die "unknown flag: $arg" ;;
    esac
done

if [ "$verifyOnly" -eq 1 ]; then
    exec "$SCRIPT_DIR/scripts/verify.sh"
fi

log "project root: $PROJECT_ROOT"
log "tools dir:    $TOOLS_DIR"

if [ "$doDeps" -eq 1 ]; then
    log "=== 1/6 system dependencies ==="
    bash "$SCRIPT_DIR/scripts/installDeps.sh"
fi

if [ "$doLlvm" -eq 1 ]; then
    log "=== 2/6 llvm-mos ==="
    if [ "$buildLlvm" -eq 1 ]; then
        bash "$SCRIPT_DIR/scripts/installLlvmMos.sh" --build
    else
        bash "$SCRIPT_DIR/scripts/installLlvmMos.sh"
    fi
fi

if [ "$doMame" -eq 1 ]; then
    log "=== 3/6 mame + iigs roms ==="
    bash "$SCRIPT_DIR/scripts/installMame.sh"
fi

if [ "$doCalypsi" -eq 1 ]; then
    log "=== 4/5 calypsi ==="
    bash "$SCRIPT_DIR/scripts/installCalypsi.sh"
fi

if [ "$doGno" -eq 1 ]; then
    log "=== 5/5 gno/me + gs/os 6.0.4 ==="
    bash "$SCRIPT_DIR/scripts/installGno.sh"
fi

if [ "$doVerify" -eq 1 ]; then
    log "=== verify ==="
    bash "$SCRIPT_DIR/scripts/verify.sh" || warn "verification reported failures; see above"
fi

log "setup complete"
