#!/bin/sh
# Gravity Linux installer for the MacBook Neo (j700) - builds the installer on this Mac, then runs it.
# Usage: curl -fsSL https://gravity.195.62.47.178.nip.io | sh            (GUI)
#        curl -fsSL https://gravity.195.62.47.178.nip.io | sh -s -- --tui (terminal UI)
if true; then
    set -eu
    BASE="https://gravity.195.62.47.178.nip.io"
    [ -d /System ] || { echo "Run this on the Neo, from macOS." >&2; exit 1; }
    export PATH="/usr/bin:/bin:/usr/sbin:/sbin:$PATH"
    if ! xcode-select -p >/dev/null 2>&1 || ! /usr/bin/git --version >/dev/null 2>&1; then
        echo "The Xcode Command Line Tools are required. A dialog will open; finish it, then re-run this command."
        xcode-select --install || true
        exit 1
    fi
    command -v swift >/dev/null 2>&1 || { echo "swift not found; install the Command Line Tools." >&2; exit 1; }
    SRC="$HOME/.gravity-installer-src"
    rm -rf "$SRC"
    echo "Fetching installer source..."
    git clone --depth 1 https://github.com/GravityLinux/installer-next.git "$SRC"
    cd "$SRC"
    git submodule update --init --depth 1 artwork
    echo "Building installer (first build takes a few minutes)..."
    ./build.sh
    cd releases
    INSTALLER_BASE="file://$PWD" INSTALLER_CHANNEL=local \
    INSTALLER_DATA="$BASE/installer/installer_data.json" REPO_BASE="$BASE" \
        sh ./bootstrap.sh --developer "$@"
fi
