#!/bin/bash
# Crucible installer for Ubuntu/Debian. Installs Node + smartmontools +
# ipmitool, npm-installs @glassmkr/crucible, then delegates config/systemd
# setup to `glassmkr-crucible init` (Crucible 0.9.1+).
#
# Hosted at https://glassmkr.com/install.sh. Used by:
#
#   curl -sf https://glassmkr.com/install.sh | sudo bash -s -- --api-key <K>
#
# For standalone (no Dashboard) or telegram-enabled setups, see the README at
# https://github.com/glassmkr/crucible — hand-edit /etc/glassmkr/collector.yaml
# after the manual install.
set -euo pipefail

echo "=== Glassmkr Crucible Installer ==="

# OS check
if [ ! -f /etc/os-release ]; then
  echo "ERROR: cannot detect OS. Only Ubuntu and Debian are supported."
  exit 1
fi
. /etc/os-release
if [[ "$ID" != "ubuntu" && "$ID" != "debian" ]]; then
  echo "ERROR: only Ubuntu and Debian are supported. Detected: $ID"
  exit 1
fi
echo "Detected: $PRETTY_NAME"

if [ "$EUID" -ne 0 ]; then
  echo "ERROR: please run as root (sudo)"
  exit 1
fi

# Args. --dashboard-key kept as a back-compat alias for --api-key.
SERVER_NAME=""
API_KEY="${GLASSMKR_API_KEY:-}"
NO_START=""
while [[ $# -gt 0 ]]; do
  case $1 in
    --name) SERVER_NAME="$2"; shift 2 ;;
    --api-key) API_KEY="$2"; shift 2 ;;
    --dashboard-key) API_KEY="$2"; shift 2 ;;
    --no-start) NO_START="--no-start"; shift ;;
    *) echo "ignoring unknown arg: $1"; shift ;;
  esac
done

if [ -z "$API_KEY" ]; then
  if [ -t 0 ]; then
    read -p "Dashboard API key: " API_KEY
  fi
fi
if [ -z "$API_KEY" ]; then
  echo "ERROR: --api-key is required (or set GLASSMKR_API_KEY in the environment)."
  echo "Get one in the Dashboard dashboard: https://app.glassmkr.com/servers"
  exit 1
fi

# Node 22 (matches the npm-Trusted-Publishing setup in the publish workflow)
if ! command -v node >/dev/null 2>&1; then
  echo "Installing Node.js 22..."
  curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
  apt-get install -y nodejs
fi
echo "Node.js: $(node --version)"

if ! command -v smartctl >/dev/null 2>&1; then
  echo "Installing smartmontools..."
  apt-get install -y smartmontools
fi

if ! command -v ipmitool >/dev/null 2>&1; then
  echo "Installing ipmitool (best-effort; may be unavailable in containers)..."
  apt-get install -y ipmitool 2>/dev/null || echo "WARN: ipmitool not available. IPMI monitoring will be disabled."
fi

mkdir -p /var/lib/glassmkr
chmod 700 /var/lib/glassmkr

echo "Installing @glassmkr/crucible..."
npm install -g @glassmkr/crucible

# Hand off to init: validates the key, writes /etc/glassmkr/collector.yaml,
# writes the systemd unit with the dynamically-detected binary path, runs
# daemon-reload, and (unless --no-start) enables-and-starts the service.
NAME_FLAG=()
if [ -n "$SERVER_NAME" ]; then
  NAME_FLAG=(--name "$SERVER_NAME")
fi
glassmkr-crucible init --api-key "$API_KEY" "${NAME_FLAG[@]}" $NO_START

echo ""
echo "=== Installation complete ==="
echo "  systemctl status glassmkr-crucible --no-pager | head -20"
echo "  journalctl -u glassmkr-crucible -n 50 --no-pager"
echo "Config: /etc/glassmkr/collector.yaml"
