#!/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 d'installation ChessCubing pour Proxmox.

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

Usage en une ligne :
  bash -c "$(curl -fsSL https://git.jeannerot.fr/christophe/chesscubing/raw/branch/main/install-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_LXC_IP
  CHESSCUBING_LXC_GATEWAY
  CHESSCUBING_LXC_BRIDGE
  CHESSCUBING_LXC_CORES
  CHESSCUBING_LXC_MEMORY
  CHESSCUBING_LXC_SWAP
  CHESSCUBING_LXC_DISK_GB
  CHESSCUBING_TEMPLATE_STORAGE
  CHESSCUBING_ROOTFS_STORAGE
  CHESSCUBING_LXC_PASSWORD
  CHESSCUBING_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}"
LXC_IP="${CHESSCUBING_LXC_IP:-dhcp}"
LXC_GATEWAY="${CHESSCUBING_LXC_GATEWAY:-}"
LXC_BRIDGE="${CHESSCUBING_LXC_BRIDGE:-vmbr0}"
LXC_CORES="${CHESSCUBING_LXC_CORES:-2}"
LXC_MEMORY="${CHESSCUBING_LXC_MEMORY:-1024}"
LXC_SWAP="${CHESSCUBING_LXC_SWAP:-512}"
LXC_DISK_GB="${CHESSCUBING_LXC_DISK_GB:-6}"
TEMPLATE_STORAGE="${CHESSCUBING_TEMPLATE_STORAGE:-}"
ROOTFS_STORAGE="${CHESSCUBING_ROOTFS_STORAGE:-}"
LXC_PASSWORD="${CHESSCUBING_LXC_PASSWORD:-}"

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

prompt_default CTID "CTID Proxmox (laisser vide pour auto)" ""
prompt_default LXC_HOSTNAME "Nom du LXC" "chesscubing-web"
prompt_default LXC_IP "IP du LXC ou dhcp" "dhcp"
if [[ "$LXC_IP" != "dhcp" ]]; then
  prompt_default LXC_GATEWAY "Passerelle du LXC" ""
fi
prompt_default LXC_BRIDGE "Bridge réseau Proxmox" "vmbr0"

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 d’installation ChessCubing...\n'
curl -fsSL "${RAW_BASE_URL}/scripts/install-proxmox-lxc.sh" -o "$TMP_DIR/install-proxmox-lxc.sh"
chmod +x "$TMP_DIR/install-proxmox-lxc.sh"

cmd=(
  "$TMP_DIR/install-proxmox-lxc.sh"
  --hostname "$LXC_HOSTNAME"
  --lxc-ip "$LXC_IP"
  --bridge "$LXC_BRIDGE"
  --cores "$LXC_CORES"
  --memory "$LXC_MEMORY"
  --swap "$LXC_SWAP"
  --disk-gb "$LXC_DISK_GB"
  --branch "$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")
fi

if [[ -n "$LXC_GATEWAY" ]]; then
  cmd+=(--gateway "$LXC_GATEWAY")
fi

if [[ -n "$TEMPLATE_STORAGE" ]]; then
  cmd+=(--template-storage "$TEMPLATE_STORAGE")
fi

if [[ -n "$ROOTFS_STORAGE" ]]; then
  cmd+=(--rootfs-storage "$ROOTFS_STORAGE")
fi

if [[ -n "$LXC_PASSWORD" ]]; then
  cmd+=(--lxc-password "$LXC_PASSWORD")
fi

printf 'Lancement de l’installation LXC ChessCubing...\n'
"${cmd[@]}"
