#!/usr/bin/env bash
#
# Default (customer) Redgold installer: the redgold terminal client + rac.
#
#   export REDGOLD_API_KEY=sk-rg-...
#   curl -fsSL https://redgold.ai/install.sh | bash
#
# This bootstrap is public. It requires the key before fetching the protected
# component installers, then sends the key on every downstream request.
# Internal/admin installs (heavy TUI as `redgold`, this client as
# `redgold-lite`, plus rac) use https://redgold.ai/install-admin.sh instead.
set -eu

INSTALL_DIR_DEFAULT="${HOME}/.redgold/bin"
INSTALL_DIR="${REDGOLD_INSTALL_DIR:-${INSTALL_DIR_DEFAULT}}"
KEY="${REDGOLD_API_KEY:-}"
INSTALL_ENGINE=1
INSTALL_LOCAL_STACK=1
INSTALL_RCI=1
INSTALL_AWS=1
TUI_LITE_INSTALLER_URL="${REDGOLD_TUI_LITE_INSTALLER_URL:-https://redgold.ai/install-lite.sh}"
RAC_INSTALLER_URL="${REDGOLD_RAC_INSTALLER_URL:-https://redgold.ai/dl/agent-bins/install.sh}"

print_help() {
    cat <<EOF
Redgold installer (terminal client + rac)

Usage:
  export REDGOLD_API_KEY=sk-rg-...
  curl -fsSL https://redgold.ai/install.sh | bash

Options:
  --key <sk-rg-...>   API key (equivalent to REDGOLD_API_KEY)
  --dir <path>        Install dir (default: ${INSTALL_DIR_DEFAULT})
  --without-rci       Install without the CI/automation companion
  --without-engine    Install without the local engine companion
  --without-local-stack
                       Install engine without executor and DTS companions
  --without-aws       Install without AWS CLI v2
  --help              This help

Env:
  REDGOLD_API_KEY                  Required API key
  REDGOLD_INSTALL_DIR              Equivalent to --dir
  REDGOLD_TUI_LITE_INSTALLER_URL   Override the terminal client installer URL
  REDGOLD_RAC_INSTALLER_URL        Override the rac installer URL
EOF
}

while [ $# -gt 0 ]; do
    case "$1" in
        --key) KEY="$2"; shift 2 ;;
        --dir) INSTALL_DIR="$2"; shift 2 ;;
        --without-rci) INSTALL_RCI=0; shift ;;
        --without-engine) INSTALL_ENGINE=0; INSTALL_LOCAL_STACK=0; shift ;;
        --without-local-stack) INSTALL_LOCAL_STACK=0; shift ;;
        --without-aws) INSTALL_AWS=0; shift ;;
        --help|-h) print_help; exit 0 ;;
        *) echo "unknown arg: $1" >&2; print_help; exit 2 ;;
    esac
done

if [ -z "$KEY" ]; then
    echo "REDGOLD_API_KEY or --key is required" >&2
    exit 1
fi
if ! command -v curl >/dev/null 2>&1; then
    echo "curl is required" >&2
    exit 1
fi

TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT

fetch_installer() {
    url="$1"
    output="$2"
    curl -fsSL \
        -H "Authorization: Bearer ${KEY}" \
        -H "x-api-key: ${KEY}" \
        "$url" -o "$output"
}

echo "==> Downloading component installers"
fetch_installer "$TUI_LITE_INSTALLER_URL" "$TMP/install-lite.sh"
fetch_installer "$RAC_INSTALLER_URL" "$TMP/install-rac.sh"

echo "==> Installing TUI Lite"
REDGOLD_API_KEY="$KEY" bash "$TMP/install-lite.sh" --key "$KEY" --dir "$INSTALL_DIR"

echo "==> Installing rac"
RAC_ARGS=(--key "$KEY" --dir "$INSTALL_DIR")
[ "$INSTALL_RCI" = 1 ] || RAC_ARGS+=(--without-rci)
[ "$INSTALL_ENGINE" = 1 ] || RAC_ARGS+=(--without-engine)
[ "$INSTALL_LOCAL_STACK" = 1 ] || RAC_ARGS+=(--without-local-stack)
[ "$INSTALL_AWS" = 1 ] || RAC_ARGS+=(--without-aws)
REDGOLD_API_KEY="$KEY" bash "$TMP/install-rac.sh" "${RAC_ARGS[@]}"

echo
echo "Done. Installed redgold, rac, and selected companions in $INSTALL_DIR"
