#!/bin/sh
# CorvinOS bootstrapper (ADR-2215 amendment). Installs nothing itself: it only
# clones (or fast-forwards) the repository and hands over to the repo's own
# install.sh, so what runs is exactly what is checked out.
# Wrapped in main() so a truncated `curl | sh` download never runs half a script.
main() {
    repo_url="https://github.com/CorvinLabs/CorvinOS.git"
    dir="${CORVIN_DIR:-$HOME/CorvinOS}"

    if ! command -v git >/dev/null 2>&1; then
        echo "git is required. Install git first, then re-run this command." >&2
        return 1
    fi

    if [ -d "$dir/.git" ]; then
        echo "Updating existing checkout: $dir"
        git -C "$dir" pull --ff-only || {
            echo "Could not fast-forward $dir (local changes?). Resolve and re-run." >&2
            return 1
        }
    elif [ -e "$dir" ]; then
        echo "$dir exists but is not a CorvinOS checkout. Set CORVIN_DIR to another path." >&2
        return 1
    else
        echo "Cloning $repo_url -> $dir"
        git clone "$repo_url" "$dir" || return 1
    fi

    cd "$dir" || return 1
    # Under `curl | sh` stdin is the pipe; give the installer the terminal.
    if [ ! -t 0 ] && ( : </dev/tty ) 2>/dev/null; then
        ./install.sh "$@" </dev/tty
    else
        ./install.sh "$@"
    fi
}
main "$@"
