#!/usr/bin/env bash
set -euo pipefail

REPO_BRANCH="${CHESSCUBING_GIT_BRANCH:-main}"
RAW_BASE_URL="https://git.jeannerot.fr/christophe/chesscubing/raw/branch/${REPO_BRANCH}"

usage() {
  cat <<'EOF'
Bootstrap de mise à jour ChessCubing pour Proxmox.

Usage local :
  ./update-chesscubing-proxmox.sh

Usage en une ligne :
  bash -c "$(curl -fsSL https://git.jeannerot.fr/christophe/chesscubing/raw/branch/main/update-chesscubing-proxmox.sh)"

Variables d'environnement reconnues :
  CHESSCUBING_LOCAL
  CHESSCUBING_PROXMOX_HOST
  CHESSCUBING_PROXMOX_USER
  CHESSCUBING_PROXMOX_PASSWORD
  CHESSCUBING_SSH_PORT
  CHESSCUBING_CTID
  CHESSCUBING_LXC_HOSTNAME
  CHESSCUBING_GIT_BRANCH
  CHESSCUBING_ETHAN_REPO_URL
  CHESSCUBING_ETHAN_GIT_BRANCH
  CHESSCUBING_BRICE_REPO_URL
  CHESSCUBING_BRICE_GIT_BRANCH
EOF
}

die() {
  printf 'Erreur: %s\n' "$*" >&2
  exit 1
}

have_cmd() {
  command -v "$1" >/dev/null 2>&1
}

need_cmd() {
  have_cmd "$1" || die "La commande '$1' est requise."
}

prompt_default() {
  local var_name="$1"
  local prompt_label="$2"
  local default_value="${3:-}"
  local current_value="${!var_name:-}"
  local input=""

  if [[ -n "$current_value" ]]; then
    return 0
  fi

  if [[ -n "$default_value" ]]; then
    read -r -p "$prompt_label [$default_value]: " input
    printf -v "$var_name" '%s' "${input:-$default_value}"
  else
    read -r -p "$prompt_label: " input
    printf -v "$var_name" '%s' "$input"
  fi
}

prompt_secret() {
  local var_name="$1"
  local prompt_label="$2"
  local current_value="${!var_name:-}"

  if [[ -n "$current_value" ]]; then
    return 0
  fi

  read -rsp "$prompt_label: " current_value
  echo
  printf -v "$var_name" '%s' "$current_value"
}

if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
  usage
  exit 0
fi

need_cmd curl

LOCAL_MODE="${CHESSCUBING_LOCAL:-}"
PROXMOX_HOST="${CHESSCUBING_PROXMOX_HOST:-}"
PROXMOX_USER="${CHESSCUBING_PROXMOX_USER:-}"
PROXMOX_PASSWORD="${CHESSCUBING_PROXMOX_PASSWORD:-}"
SSH_PORT="${CHESSCUBING_SSH_PORT:-22}"
CTID="${CHESSCUBING_CTID:-}"
LXC_HOSTNAME="${CHESSCUBING_LXC_HOSTNAME:-chesscubing-web}"
ETHAN_REPO_URL="${CHESSCUBING_ETHAN_REPO_URL:-https://git.jeannerot.fr/Mineloulou/Chesscubing.git}"
ETHAN_REPO_BRANCH="${CHESSCUBING_ETHAN_GIT_BRANCH:-main}"
BRICE_REPO_URL="${CHESSCUBING_BRICE_REPO_URL:-https://git.jeannerot.fr/Lescratcheur/ChessCubing.git}"
BRICE_REPO_BRANCH="${CHESSCUBING_BRICE_GIT_BRANCH:-main}"

if [[ -z "$LOCAL_MODE" && -z "$PROXMOX_HOST" ]]; then
  if have_cmd pct && have_cmd pveam; then
    LOCAL_MODE="1"
  else
    LOCAL_MODE="0"
  fi
fi

if [[ "$LOCAL_MODE" != "1" ]]; then
  need_cmd ssh
  need_cmd sshpass
  prompt_default PROXMOX_HOST "IP ou nom du serveur Proxmox" ""
  prompt_default PROXMOX_USER "Utilisateur SSH Proxmox" "root@pam"
  prompt_secret PROXMOX_PASSWORD "Mot de passe SSH Proxmox"
  prompt_default SSH_PORT "Port SSH Proxmox" "22"
fi

if [[ "$LOCAL_MODE" != "1" ]]; then
  [[ -n "$PROXMOX_HOST" ]] || die "Le serveur Proxmox est obligatoire."
  [[ -n "$PROXMOX_USER" ]] || die "L'utilisateur Proxmox est obligatoire."
  [[ -n "$PROXMOX_PASSWORD" ]] || die "Le mot de passe Proxmox est obligatoire."
fi

TMP_DIR="$(mktemp -d)"
cleanup() {
  rm -rf "$TMP_DIR"
}
trap cleanup EXIT

printf 'Téléchargement du script de mise à jour ChessCubing...\n'
curl -fsSL "${RAW_BASE_URL}/scripts/update-proxmox-lxc.sh" -o "$TMP_DIR/update-proxmox-lxc.sh"
chmod +x "$TMP_DIR/update-proxmox-lxc.sh"

cmd=(
  "$TMP_DIR/update-proxmox-lxc.sh"
  --branch "$REPO_BRANCH"
  --ethan-repo-url "$ETHAN_REPO_URL"
  --ethan-branch "$ETHAN_REPO_BRANCH"
  --brice-repo-url "$BRICE_REPO_URL"
  --brice-branch "$BRICE_REPO_BRANCH"
)

if [[ "$LOCAL_MODE" == "1" ]]; then
  cmd+=(--local)
else
  cmd+=(
    --proxmox-host "$PROXMOX_HOST"
    --proxmox-user "$PROXMOX_USER"
    --proxmox-password "$PROXMOX_PASSWORD"
    --ssh-port "$SSH_PORT"
  )
fi

if [[ -n "$CTID" ]]; then
  cmd+=(--ctid "$CTID")
else
  cmd+=(--hostname "$LXC_HOSTNAME")
fi

printf 'Lancement de la mise à jour ChessCubing...\n'
"${cmd[@]}"
