#!/usr/bin/env bash
# Install llvm-mos: clone source tree for backend development, plus
# download prebuilt SDK for reference/smoke-testing existing 6502 targets.
#
# Flags:
#   --build   also build the source tree with cmake/ninja (slow, ~30-60 min)

set -euo pipefail
source "$(dirname "$0")/common.sh"

doBuild=0
for arg in "$@"; do
    case "$arg" in
        --build) doBuild=1 ;;
        *) die "unknown flag: $arg" ;;
    esac
done

LLVM_SRC="$TOOLS_DIR/llvm-mos"
LLVM_BUILD="$TOOLS_DIR/llvm-mos-build"
LLVM_SDK="$TOOLS_DIR/llvm-mos-sdk"
SDK_URL="https://github.com/llvm-mos/llvm-mos-sdk/releases/latest/download/llvm-mos-linux.tar.xz"

# 1. Source tree for backend development.
#
# IMPORTANT: this clone is ephemeral scaffolding, not where we do work. Our
# own sources live in $PROJECT_ROOT/src/ and $PROJECT_ROOT/patches/ and are
# stitched into this tree by applyBackend.sh. That means a destructive
# reset here would stomp applied symlinks/patches — so we refuse to touch
# the clone if it looks modified. Use updateLlvmMos.sh to refresh safely.
if [ -d "$LLVM_SRC/.git" ]; then
    currentBranch="$(git -C "$LLVM_SRC" rev-parse --abbrev-ref HEAD)"
    if [ "$currentBranch" != "main" ]; then
        warn "llvm-mos clone is on branch '$currentBranch' (not main); leaving untouched"
    elif ! git -C "$LLVM_SRC" diff --quiet || ! git -C "$LLVM_SRC" diff --cached --quiet; then
        warn "llvm-mos clone has local modifications; leaving untouched. Use scripts/updateLlvmMos.sh to refresh."
    else
        log "llvm-mos source already cloned; fetching and fast-forwarding main"
        git -C "$LLVM_SRC" fetch --depth=1 origin main
        git -C "$LLVM_SRC" merge --ff-only FETCH_HEAD || warn "fast-forward failed; leaving clone as-is"
    fi
else
    log "cloning llvm-mos (shallow)"
    git clone --depth=1 https://github.com/llvm-mos/llvm-mos.git "$LLVM_SRC"
fi

# 2. Prebuilt SDK for testing/reference.
if [ -x "$LLVM_SDK/bin/mos-common-clang" ] || [ -x "$LLVM_SDK/bin/clang" ]; then
    log "llvm-mos-sdk already extracted"
else
    archive="$(fetchCached "$SDK_URL" "llvm-mos-linux.tar.xz")"
    log "extracting llvm-mos-sdk"
    install -d "$LLVM_SDK"
    tar -xJf "$archive" -C "$LLVM_SDK" --strip-components=1
fi

# 3. Optional source build.
if [ "$doBuild" -eq 1 ]; then
    needCmd cmake
    needCmd ninja
    log "configuring llvm-mos build (this takes a while)"
    install -d "$LLVM_BUILD"
    cmake -S "$LLVM_SRC/llvm" -B "$LLVM_BUILD" -G Ninja \
        -DCMAKE_BUILD_TYPE=Release \
        -DLLVM_TARGETS_TO_BUILD="" \
        -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="MOS" \
        -DLLVM_ENABLE_PROJECTS="clang;lld" \
        -DLLVM_PARALLEL_LINK_JOBS=1 \
        -DLLVM_USE_LINKER=lld \
        -DLLVM_INCLUDE_TESTS=OFF \
        -DLLVM_INCLUDE_EXAMPLES=OFF \
        -DLLVM_INCLUDE_BENCHMARKS=OFF
    log "building llvm-mos (background-friendly: use --build only when you have time)"
    ninja -C "$LLVM_BUILD"
    log "llvm-mos build complete: $LLVM_BUILD/bin/clang"
else
    log "skipped source build; rerun with --build when ready (cmake+ninja)"
fi

log "llvm-mos install done"
log "  source:    $LLVM_SRC"
log "  sdk:       $LLVM_SDK"
[ "$doBuild" -eq 1 ] && log "  build:     $LLVM_BUILD"
