#!/usr/bin/env bash
# Koinode · install the `koinode` command line, then let it look at the machine (specs/0019 §2.5).
#
#   curl -fsSL https://get.koinode.dev | bash                                   # install, then koinode check
#   curl -fsSL https://get.koinode.dev | bash -s -- --invite koi_... --hf       # install, then koinode join
#
#   --invite   go straight on to `koinode join` with this invitation
#   --hf       (with --invite) ask for a Hugging Face read token for the gated clip model
#   --name     (with --invite) how the owner will see this machine
#   --yes      do not ask before setting the machine up
#   --url      the owner's coordinator, when it is not wss://node.koinode.dev
#   --quiet    only install or update koinode; do not run anything after
#
# What it installs, in your home folder only (no administrator password): uv, Astral's tool for pinned Pythons, if it
# is not there yet (it brings its own Python, so the system's Python is never touched), and the koinode command in
# ~/.local/bin. `koinode uninstall` removes it again. Works with the macOS /bin/bash (3.2).
set -uo pipefail

CLI_URL="${KOINODE_CLI_URL:-https://get.koinode.dev/cli/koinode-0.1.0-py3-none-any.whl}"       # put in when the site serves this script
UV_VERSION="0.11.15"                            # the uv tried with this koinode
INVITE="" HF=0 NAME="" YES=0 QUIET=0 URL=""
while [ $# -gt 0 ]; do
    case "$1" in
        --invite) INVITE="$2"; shift 2 ;;
        --name) NAME="$2"; shift 2 ;;
        --hf) HF=1; shift ;;
        --yes|-y) YES=1; shift ;;
        --quiet) QUIET=1; shift ;;
        --url) URL="$2"; shift 2 ;;
        -h|--help) sed -n '2,18p' "$0" 2>/dev/null || true; exit 0 ;;
        *) echo "unknown option: $1 (see --help)" >&2; exit 2 ;;
    esac
done

if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
    KOI=$'\033[38;2;236;100;36m' OK=$'\033[38;2;127;184;134m' FNT=$'\033[38;2;143;138;129m' BAD=$'\033[38;2;255;122;135m'
    B=$'\033[1m' R=$'\033[0m'
else
    KOI="" OK="" FNT="" BAD="" B="" R=""
fi
say() { printf '%s\n' "$*"; }
ok() { printf '  %s✓%s %-12s %s\n' "$OK" "$R" "$1" "$2"; }
stop() { printf '\n  %s×%s %s\n\n' "$BAD" "$R" "$*" >&2; exit 1; }

say ""
say "  ${KOI}${B}><(((●>${R}   ${B}koinode${R}   ${FNT}installing the command line${R}"
say ""

case "$(uname -s)" in
    Darwin|Linux) ;;
    MINGW*|MSYS*|CYGWIN*) stop "on Windows, run this inside Ubuntu on WSL 2 (PowerShell as administrator, once: wsl --install -d Ubuntu)" ;;
    *) stop "koinode runs on macOS and Linux (Windows: inside WSL 2)" ;;
esac
case "$CLI_URL" in __*|"") stop "no download address for koinode: run it from https://get.koinode.dev" ;; esac
command -v curl >/dev/null 2>&1 || stop "curl is needed"

BIN="${UV_TOOL_BIN_DIR:-$HOME/.local/bin}"
export PATH="$BIN:$HOME/.local/bin:$HOME/.cargo/bin:$PATH"

# ------------------------------------------------------------------------------------------- uv
if command -v uv >/dev/null 2>&1; then
    ok uv "$(uv --version 2>/dev/null)"
else
    say "  ${FNT}↓ uv ${UV_VERSION} (Astral's tool for pinned Pythons) into ~/.local/bin…${R}"
    curl -LsSf "https://astral.sh/uv/${UV_VERSION}/install.sh" | env UV_PRINT_QUIET=1 sh >/dev/null 2>&1 \
        || stop "could not install uv: https://docs.astral.sh/uv/getting-started/installation/"
    command -v uv >/dev/null 2>&1 || stop "uv was installed but is not on the PATH: open a new terminal and run this again"
    ok uv "$(uv --version 2>/dev/null)"
fi

# ------------------------------------------------------------------------------------------- koinode
say "  ${FNT}↓ koinode, with its own Python…${R}"
uv tool install --force --quiet --python 3.12 "$CLI_URL" >/dev/null 2>&1 \
    || uv tool install --force --python 3.12 "$CLI_URL" \
    || stop "could not install koinode from $CLI_URL"
command -v koinode >/dev/null 2>&1 || stop "koinode was installed in $BIN, which is not on the PATH"
ok koinode "$(koinode --version 2>/dev/null) in $BIN"
[ -n "${UV_TOOL_BIN_DIR:-}" ] || uv tool update-shell >/dev/null 2>&1 || true   # ~/.local/bin on new terminals' PATH
say ""

# ------------------------------------------------------------------------------------------- then
[ "$QUIET" = 1 ] && exit 0
if [ -n "$INVITE" ]; then
    set -- join "$INVITE"
    [ -n "$URL" ] && set -- --url "$URL" "$@"
    [ "$HF" = 1 ] && set -- "$@" --hf
    [ -n "$NAME" ] && set -- "$@" --name "$NAME"
    [ "$YES" = 1 ] && set -- "$@" --yes
    if [ -r /dev/tty ]; then exec koinode "$@" </dev/tty; else exec koinode "$@"; fi
fi
if [ -n "$URL" ]; then koinode --url "$URL" check; else koinode check; fi
say "  ${FNT}koinode is installed. Open a new terminal and type ${R}${B}koinode${R}${FNT} any time.${R}"
say ""
