#!/bin/sh
# =============================================================================
#  Eve Host Manager — bootstrap (served at https://get.evehm.com/)
#
#      curl -fsSL https://get.evehm.com | sh
#
#  or fully hands-off, with the license key appended (verified BEFORE anything
#  is installed; the domains chosen at order time are fetched automatically):
#
#      curl -fsSL https://get.evehm.com | sh -s -- EVE-XXXX-XXXX-XXXX-XXXX
#
#  Strictly POSIX so it runs under dash/ash/bash alike. It only:
#    1. makes sure we can become root (direct or via sudo),
#    2. makes sure bash + curl exist (installs them if not),
#    3. downloads the real installer to a private temp file,
#    4. runs it, then deletes the temp file — nothing is left behind.
#
#  The real installer (https://get.evehm.com/install.sh) is bash and handles
#  everything else: Docker, prompts, license check, services, the `evehm` CLI.
# =============================================================================
set -eu

URL="${EVEHM_INSTALLER_URL:-https://get.evehm.com/install.sh}"

# ── Arguments: an EVE-… license key can be appended after `sh -s --` ─────────
KEY="${EVEHM_LICENSE_KEY:-}"
for a in "$@"; do
  case "$a" in
    [Ee][Vv][Ee]-*) KEY="$(printf '%s' "$a" | tr '[:lower:]' '[:upper:]')" ;;
    *) printf "Note: ignoring unrecognised argument '%s'.\n" "$a" ;;
  esac
done

say()  { printf '%s\n' "$*"; }
fail() { printf 'Error: %s\n' "$*" >&2; exit 1; }

[ "$(uname -s 2>/dev/null)" = "Linux" ] || fail "Eve Host Manager installs on Linux servers only."

# ── Privileges: run as root, or re-run everything through sudo ───────────────
SUDO=""
if [ "$(id -u)" -ne 0 ]; then
  command -v sudo >/dev/null 2>&1 \
    || fail "Please run as root (or install sudo):  curl -fsSL https://get.evehm.com | sh"
  SUDO="sudo"
  if ! sudo -n true 2>/dev/null; then
    [ -e /dev/tty ] || fail "Root is required and sudo needs a password but no terminal is available."
    say "Root privileges are required — you may be asked for your sudo password."
    sudo -v </dev/tty || fail "Could not obtain sudo privileges."
  fi
fi

pkg() {
  if   command -v apt-get >/dev/null 2>&1; then $SUDO env DEBIAN_FRONTEND=noninteractive apt-get update -qq && $SUDO env DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@"
  elif command -v dnf     >/dev/null 2>&1; then $SUDO dnf install -y "$@"
  elif command -v yum     >/dev/null 2>&1; then $SUDO yum install -y "$@"
  elif command -v zypper  >/dev/null 2>&1; then $SUDO zypper -n install "$@"
  elif command -v pacman  >/dev/null 2>&1; then $SUDO pacman -Sy --noconfirm "$@"
  elif command -v apk     >/dev/null 2>&1; then $SUDO apk add --no-cache "$@"
  else return 1; fi
}

command -v curl >/dev/null 2>&1 || pkg ca-certificates curl || fail "curl is required and couldn't be installed."
command -v bash >/dev/null 2>&1 || pkg bash               || fail "bash is required and couldn't be installed."

# ── Fetch → run → remove ─────────────────────────────────────────────────────
TMP="$(mktemp /tmp/.evehm-install.XXXXXX)" || fail "mktemp failed."
trap 'rm -f "$TMP"' EXIT INT TERM
chmod 600 "$TMP"

say "Downloading the Eve Host Manager installer…"
curl -fsSL "$URL" -o "$TMP" || fail "Could not download $URL — check your network and try again."

# EVEHM_KEEP_INSTALLER=1 stops the installer deleting $TMP itself; our trap does it,
# so the temp copy is removed exactly once, whatever happens.
[ -n "$KEY" ] && say "License key detected — it will be verified before anything is installed."
if [ -n "$SUDO" ]; then
  if [ -n "$KEY" ]; then
    sudo -E env EVEHM_KEEP_INSTALLER=1 EVEHM_LICENSE_KEY="$KEY" bash "$TMP" install
  else
    sudo -E env EVEHM_KEEP_INSTALLER=1 bash "$TMP" install
  fi
else
  if [ -n "$KEY" ]; then
    EVEHM_KEEP_INSTALLER=1 EVEHM_LICENSE_KEY="$KEY" bash "$TMP" install
  else
    EVEHM_KEEP_INSTALLER=1 bash "$TMP" install
  fi
fi
