#!/usr/bin/env bash
#############################################
# THOR — bootstrap installer
# Adds the signed THOR package repo, installs THOR, and runs first-time config.
# Usage:  curl -fsSL https://thor.ops.truehorizonai.com/install.sh | bash
#         curl -fsSL https://thor.ops.truehorizonai.com/install.sh | bash -s -- --channel staging
#
# Updates after install are just:  sudo apt upgrade thor   /   sudo dnf upgrade thor
#############################################
set -euo pipefail

# ---- Repo endpoints (must match your published repos) --------------------
APT_URL="${THOR_APT_URL:-https://thor.ops.truehorizonai.com/deb}"
YUM_URL="${THOR_YUM_URL:-https://thor.ops.truehorizonai.com/rpm}"
PUBKEY_URL="${THOR_PUBKEY_URL:-https://thor.ops.truehorizonai.com/thor-repo-pubkey.asc}"
CHANNEL="latest"
# --------------------------------------------------------------------------

RED='\033[0;31m'; GREEN='\033[0;32m'; BLUE='\033[0;34m'; NC='\033[0m'
info(){ echo -e "${BLUE}[INFO]${NC} $1"; }
ok(){ echo -e "${GREEN}[OK]${NC} $1"; }
err(){ echo -e "${RED}[ERROR]${NC} $1"; }

while [[ $# -gt 0 ]]; do
    case "$1" in
        --channel) CHANNEL="$2"; shift 2 ;;
        *) err "Unknown option: $1"; exit 1 ;;
    esac
done

[ "$(id -u)" -eq 0 ] && SUDO="" || SUDO="sudo"

command -v systemctl >/dev/null || { err "systemd is required."; exit 1; }
[ -r /etc/os-release ] || { err "Cannot detect OS (/etc/os-release missing)."; exit 1; }
. /etc/os-release

install_deb() {
    info "Configuring APT repo (channel: ${CHANNEL})"
    ${SUDO} install -m 0755 -d /usr/share/keyrings
    curl -fsSL "${PUBKEY_URL}" | gpg --dearmor | ${SUDO} tee /usr/share/keyrings/thor.gpg >/dev/null
    echo "deb [signed-by=/usr/share/keyrings/thor.gpg] ${APT_URL} ${CHANNEL} main" \
        | ${SUDO} tee /etc/apt/sources.list.d/thor.list >/dev/null
    ${SUDO} apt-get update
    ${SUDO} apt-get install -y thor
}

install_rpm() {
    info "Configuring YUM/DNF repo (channel: ${CHANNEL})"
    ${SUDO} tee /etc/yum.repos.d/thor.repo >/dev/null <<EOF
[thor-${CHANNEL}]
name=THOR (${CHANNEL})
baseurl=${YUM_URL}/${CHANNEL}/\$basearch
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=${PUBKEY_URL}
EOF
    if command -v dnf >/dev/null; then ${SUDO} dnf install -y thor
    else ${SUDO} yum install -y thor; fi
}

case "${ID}:${ID_LIKE:-}" in
    debian*|ubuntu*|*:*debian*|*:*ubuntu*) install_deb ;;
    fedora*|rhel*|centos*|*:*rhel*|*:*fedora*) install_rpm ;;
    *)
        # Fall back on packaging tooling present.
        if command -v apt-get >/dev/null; then install_deb
        elif command -v dnf >/dev/null || command -v yum >/dev/null; then install_rpm
        else err "Unsupported distro '${ID}' (no apt or dnf/yum)."; exit 1; fi
        ;;
esac

ok "THOR installed ($(thor version | head -1))"

# First-time configuration, if not already present. Needs a real terminal.
if [ ! -f /etc/thor/config.yaml ]; then
    if [ -e /dev/tty ]; then
        info "Starting first-time configuration..."
        ${SUDO} thor configure </dev/tty
        ${SUDO} systemctl enable --now thor
        ok "THOR service enabled and started."
    else
        info "No TTY available. Finish setup with:"
        echo "    sudo thor configure && sudo systemctl enable --now thor"
    fi
else
    ok "Existing /etc/thor/config.yaml kept. Manage with: sudo systemctl restart thor"
fi
