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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

usage() {
  cat <<'USAGE'
Usage:
  scripts/ops/cluster_power_console.sh [--repo-dir <path>] [--delegate-host <host>] <shutdown|startup> [recovery-script-options...]

Purpose:
  Friendly manual entrypoint for running Ananke from a remote console.
  Canonical control host is titan-db by default so bundle/state handling stays in one place.

Defaults:
  --repo-dir       $HOME/Development/ananke (fallback: $HOME/Development/titan-iac)
  --delegate-host  titan-db

Examples:
  scripts/ops/cluster_power_console.sh shutdown --execute
  scripts/ops/cluster_power_console.sh startup --execute --force-flux-branch main
  scripts/ops/cluster_power_console.sh --delegate-host titan-24 shutdown --execute
USAGE
}

if [[ -d "${HOME}/Development/ananke" ]]; then
  REPO_DIR="${HOME}/Development/ananke"
else
  REPO_DIR="${HOME}/Development/titan-iac"
fi
DELEGATE_HOST="titan-db"
REMOTE_REPO_DIR="${ANANKE_REMOTE_REPO_DIR:-}"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --repo-dir)
      REPO_DIR="${2:-}"
      shift 2
      ;;
    --delegate-host)
      DELEGATE_HOST="${2:-}"
      shift 2
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      break
      ;;
  esac
done

if [[ $# -lt 1 ]]; then
  usage
  exit 1
fi

SIBLING_SCRIPT="${SCRIPT_DIR}/cluster_power_recovery.sh"
REPO_SCRIPT="${REPO_DIR}/scripts/ops/cluster_power_recovery.sh"
LOCAL_SCRIPT=""

if [[ -x "${SIBLING_SCRIPT}" ]]; then
  LOCAL_SCRIPT="${SIBLING_SCRIPT}"
elif [[ -x "${REPO_SCRIPT}" ]]; then
  LOCAL_SCRIPT="${REPO_SCRIPT}"
fi

if [[ -n "${LOCAL_SCRIPT}" ]] && command -v kubectl >/dev/null 2>&1; then
  exec "${LOCAL_SCRIPT}" "$@"
fi

if [[ -z "${DELEGATE_HOST}" ]]; then
  echo "cluster-power-console: no usable local recovery script found and no delegate host configured" >&2
  exit 1
fi

quoted_args="$(printf '%q ' "$@")"
quoted_repo_dir="$(printf '%q' "${REPO_DIR}")"

remote_cmd=""
if [[ -n "${REMOTE_REPO_DIR}" ]]; then
  remote_cmd+="ANANKE_REPO_DIR=$(printf '%q' "${REMOTE_REPO_DIR}") "
fi
remote_cmd+="if [ -x ~/ananke-tools/cluster_power_recovery.sh ]; then ~/ananke-tools/cluster_power_recovery.sh ${quoted_args}; elif [ -x ${quoted_repo_dir}/scripts/ops/cluster_power_recovery.sh ]; then ${quoted_repo_dir}/scripts/ops/cluster_power_recovery.sh ${quoted_args}; elif [ -x ${quoted_repo_dir}/scripts/cluster_power_recovery.sh ]; then ${quoted_repo_dir}/scripts/cluster_power_recovery.sh ${quoted_args}; else echo 'cluster-power-console: remote recovery script not found' >&2; exit 1; fi"

exec ssh -o BatchMode=yes -o ConnectTimeout=8 "${DELEGATE_HOST}" "${remote_cmd}"
