#!/bin/sh
set -e

# Tilde CLI installer bootstrap
# Usage: curl -fsSL https://tilde.run/install | sh

# Check dependencies
command -v curl >/dev/null 2>&1 || { echo "Error: curl is required" >&2; exit 1; }

# Print the Tilde splash (brand-gradient ~ + welcome + subtitle) on capable
# terminals. Skipped when stdout is not a TTY, TERM is unset/dumb, or
# TILDE_NO_ANIM is set.
tilde_splash() {
    [ -t 1 ] || return 0
    case "${TERM-}" in ''|dumb) return 0 ;; esac
    [ -n "${TILDE_NO_ANIM-}" ] && return 0

    # Brand gradient: #ea580c (orange) -> #06d6a0 (teal green).
    # Paint each non-space character with a 256-color step picked by column.
    _tilde_gradient() {
        s="$1"
        gw="$2"
        col=0
        while [ -n "$s" ]; do
            c="${s%"${s#?}"}"
            s="${s#?}"
            if [ "$c" != " " ]; then
                idx=$((col * 10 / gw))
                [ "$idx" -gt 9 ] && idx=9
                case "$idx" in
                    0) color=202 ;;
                    1) color=208 ;;
                    2) color=214 ;;
                    3) color=215 ;;
                    4) color=222 ;;
                    5) color=150 ;;
                    6) color=114 ;;
                    7) color=79  ;;
                    8) color=43  ;;
                    *) color=42  ;;
                esac
                printf '\033[1;38;5;%dm%s\033[0m' "$color" "$c"
            else
                printf ' '
            fi
            col=$((col + 1))
        done
        printf '\n'
    }

    printf '\n\n'

    # Welcome ribbon, centered under the subtitle.
    printf '          '
    printf '  \033[1;38;5;215m\xe2\x9c\xa8\033[0m  '
    printf '\033[1;38;5;222mWelcome to \033[0m'
    printf '\033[1;38;5;43mtilde\033[0m'
    printf '\033[1;38;5;222m - let'\''s get you set up\033[0m'
    printf '  \033[1;38;5;43m\xe2\x9c\xa8\033[0m\n'

    printf '\n\n\n'

    # Big tilde: 8 rows, shape width 46, point-symmetric about the center.
    TW=46
    PAD='            '
    _tilde_gradient "${PAD}             ~~~~~~                           " "$TW"
    _tilde_gradient "${PAD}           ~~~~~~~~~~                         " "$TW"
    _tilde_gradient "${PAD}         ~~~~      ~~~~                       " "$TW"
    _tilde_gradient "${PAD}       ~~~~          ~~~~                     " "$TW"
    _tilde_gradient "${PAD}                     ~~~~          ~~~~       " "$TW"
    _tilde_gradient "${PAD}                       ~~~~      ~~~~         " "$TW"
    _tilde_gradient "${PAD}                         ~~~~~~~~~~           " "$TW"
    _tilde_gradient "${PAD}                           ~~~~~~             " "$TW"

    printf '\n\n\n'

    # Subtitle (the widest row).
    printf '  \033[1;38;5;215m\xe2\x9c\xa8\033[0m '
    printf '\033[1;38;5;222m  versioned storage  \033[0m'
    printf '\033[38;5;214m\xc2\xb7\033[0m'
    printf '\033[1;38;5;86m  sandboxed compute  \033[0m'
    printf '\033[38;5;79m\xc2\xb7\033[0m'
    printf '\033[1;38;5;43m  audited network \033[0m'
    printf ' \033[1;38;5;43m\xe2\x9c\xa8\033[0m\n'

    printf '\n\n'
}

tilde_splash || true

OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
ARCH="$(uname -m)"
case "$ARCH" in aarch64|arm64) ARCH="arm64" ;; x86_64|amd64) ARCH="amd64" ;; *) echo "Unsupported architecture: $ARCH" >&2; exit 1 ;; esac
case "$OS" in linux|darwin) ;; *) echo "Unsupported OS: $OS" >&2; exit 1 ;; esac

BASE_URL="${TILDE_API_URL:-https://tilde.run}"
INSTALLER_URL="${TILDE_INSTALLER_URL:-${BASE_URL}/releases/installer/${OS}/${ARCH}/tilde-installer}"

umask 077
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT

echo "Downloading tilde installer..."
curl -fsSL "$INSTALLER_URL" -o "$TMP_DIR/tilde-installer"
chmod +x "$TMP_DIR/tilde-installer"
"$TMP_DIR/tilde-installer" "$@"
