#!/usr/bin/env bash
# taler-monitoring — public URL / stack checks for a Taler domain
#
#   ./taler-monitoring.sh                          # local GOA (urls + inside + e2e)
#   ./taler-monitoring.sh -d taler.net urls         # public demo, no SSH
#   ./taler-monitoring.sh --domain taler-ops.ch     # public ops, no SSH
#   TALER_DOMAIN=demo.taler.net ./taler-monitoring.sh urls
#
# Tags: [OK] [INFO] [WARN] [ERROR] [BLOCKER]
# Exit 0 only if every selected phase exits 0.

set -euo pipefail
ROOT=$(cd "$(dirname "$0")" && pwd)

usage() {
  cat <<'EOF'
taler-monitoring — bank / exchange / merchant checks

Usage:
  ./taler-monitoring.sh [options] [phases...]

Phases:
  urls     public HTTPS (no SSH)          ← default with --domain
  inside   container status via SSH       (local stack only)
  versions taler packages vs deb.taler.net trixie + repo availability
  sanity   public + optional server
  server   server-side only (SSH)
  e2e      withdraw + pay (small amounts; remote aborts on login/KYC)
  all      urls + inside + versions + sanity + e2e  (SSH phases only on koopa)

Options:
  -d, --domain DOMAIN   target domain
                        koopa / hacktivism.ch  → local stack, SSH ok
                        everything else        → no SSH; e2e optional (tiny amounts)
                        presets: koopa | hacktivism.ch | taler.net | taler-ops.ch
                        generic: bank/exchange/backend.<domain>
  --bank URL            override bank base (https://…)
  --exchange URL        override exchange base
  --merchant URL        override merchant base
  --currency CODE       expected currency (GOA, KUDOS, CHF, …); empty = report only
  --no-probe            do not probe alternate merchant/bank hostnames
  -h, --help

Examples:
  ./taler-monitoring.sh -d taler.net
  ./taler-monitoring.sh -d taler-ops.ch urls
  ./taler-monitoring.sh -d demo.taler.net --currency KUDOS
  ./taler-monitoring.sh --exchange https://exchange.taler-ops.ch urls

Env (same meaning):
  TALER_DOMAIN  BANK_PUBLIC  EXCHANGE_PUBLIC  MERCHANT_PUBLIC  EXPECT_CURRENCY
  SKIP_SSH=1    NO_COLOR=1
EOF
}

# shellcheck source=lib.sh
source "$ROOT/lib.sh"

PHASES=()
DOMAIN_SET=0
BANK_OVERRIDE=""
EXCHANGE_OVERRIDE=""
MERCHANT_OVERRIDE=""
CURRENCY_OVERRIDE=""
NO_PROBE=0

while [ $# -gt 0 ]; do
  case "$1" in
    -h|--help) usage; exit 0 ;;
    -d|--domain)
      [ $# -ge 2 ] || { echo "missing arg for $1" >&2; exit 2; }
      apply_taler_domain "$2"
      DOMAIN_SET=1
      shift 2
      ;;
    --bank)
      [ $# -ge 2 ] || { echo "missing arg for $1" >&2; exit 2; }
      BANK_OVERRIDE="${2%/}"; shift 2
      ;;
    --exchange)
      [ $# -ge 2 ] || { echo "missing arg for $1" >&2; exit 2; }
      EXCHANGE_OVERRIDE="${2%/}"; shift 2
      ;;
    --merchant)
      [ $# -ge 2 ] || { echo "missing arg for $1" >&2; exit 2; }
      MERCHANT_OVERRIDE="${2%/}"; shift 2
      ;;
    --currency)
      [ $# -ge 2 ] || { echo "missing arg for $1" >&2; exit 2; }
      CURRENCY_OVERRIDE="$2"; shift 2
      ;;
    --no-probe) NO_PROBE=1; shift ;;
    urls|inside|versions|sanity|server|e2e|all) PHASES+=("$1"); shift ;;
    *)
      # bare domain shorthand: ./taler-monitoring.sh taler.net
      if [[ "$1" == *.* && "$1" != *://* && "$1" != -* ]]; then
        apply_taler_domain "$1"
        DOMAIN_SET=1
        shift
      else
        echo "Unknown: $1" >&2; usage >&2; exit 2
      fi
      ;;
  esac
done

if [ "$NO_PROBE" = "1" ]; then
  TALER_DOMAIN_PROBE=0
fi
if [ -n "$CURRENCY_OVERRIDE" ]; then
  EXPECT_CURRENCY="$CURRENCY_OVERRIDE"
fi
if [ -n "$BANK_OVERRIDE" ]; then BANK_PUBLIC="$BANK_OVERRIDE"; fi
if [ -n "$EXCHANGE_OVERRIDE" ]; then EXCHANGE_PUBLIC="$EXCHANGE_OVERRIDE"; fi
if [ -n "$MERCHANT_OVERRIDE" ]; then MERCHANT_PUBLIC="$MERCHANT_OVERRIDE"; fi

# Only koopa may use SSH. Remote domains: public + optional e2e (no SSH).
if [ "${LOCAL_STACK:-1}" != "1" ]; then
  SKIP_SSH=1
fi

# Remote: no SSH; ATM withdraw ladder set in check_e2e (smaller notes)
if [ "${LOCAL_STACK}" != "1" ]; then
  E2E_FAKE_INCOMING=0
  E2E_REMOTE=1
  : "${E2E_WITHDRAW_VALUES:=10 20 50}"
  : "${E2E_PAY_VALUES:=0.01 0.05 0.1 1}"
fi

# Export so check_*.sh (re-source lib) see the same targets via env
export TALER_DOMAIN BANK_PUBLIC EXCHANGE_PUBLIC MERCHANT_PUBLIC
export EXPECT_CURRENCY SKIP_SSH LOCAL_STACK TALER_DOMAIN_PROBE
export WITHDRAW_AMT PAY_AMT CREDIT_AMT MERCHANT_INSTANCE
export E2E_FAKE_INCOMING E2E_REMOTE E2E_VARIABLE E2E_ATM_MAX
export E2E_WITHDRAW_VALUES E2E_PAY_VALUES
export TALER_DOMAIN_APPLIED=1

# Default phases
if [ "${#PHASES[@]}" -eq 0 ]; then
  if [ "${LOCAL_STACK}" = "1" ]; then
    PHASES=(urls inside versions e2e)   # koopa: SSH ok
  else
    PHASES=(urls)              # remote default: public only; pass "e2e" to try
  fi
fi

OUT_PHASES=()
for p in "${PHASES[@]}"; do
  if [ "$p" = "all" ]; then
    if [ "${LOCAL_STACK}" = "1" ]; then
      OUT_PHASES+=(urls inside versions sanity e2e)
    else
      # remote: repo index check only (no installed pkgs without SSH)
      OUT_PHASES+=(urls versions e2e)
    fi
  else
    OUT_PHASES+=("$p")
  fi
done
PHASES=()
seen=" "
for p in "${OUT_PHASES[@]}"; do
  # Drop SSH-only phases for non-koopa (e2e is allowed — public path only)
  # versions still runs: outside-in deb.taler.net checks; container parts soft-skip if no SSH
  if [ "${LOCAL_STACK}" != "1" ] || [ "${SKIP_SSH}" = "1" ]; then
    case "$p" in inside|server)
      echo "[INFO]  skip phase '$p' (only koopa uses SSH)" >&2
      continue
      ;;
    esac
  fi
  case "$seen" in *" $p "*) ;; *) PHASES+=("$p"); seen="$seen$p " ;; esac
done

if [ "${#PHASES[@]}" -eq 0 ]; then
  PHASES=(urls)
fi

printf 'target domain=%s\n' "${TALER_DOMAIN}"
printf '  bank     %s\n' "$BANK_PUBLIC"
printf '  exchange %s\n' "$EXCHANGE_PUBLIC"
printf '  merchant %s\n' "$MERCHANT_PUBLIC"
printf '  currency expect=%s  skip_ssh=%s\n' "${EXPECT_CURRENCY:-any}" "$SKIP_SSH"

chmod +x "$ROOT"/check_*.sh 2>/dev/null || true

ec=0
for p in "${PHASES[@]}"; do
  case "$p" in
    urls)     "$ROOT/check_urls.sh"     || ec=1 ;;
    inside)   "$ROOT/check_inside.sh"   || ec=1 ;;
    versions) "$ROOT/check_versions.sh" || ec=1 ;;
    sanity)   "$ROOT/check_sanity.sh"   || ec=1 ;;
    server)   "$ROOT/check_server.sh"   || ec=1 ;;
    e2e)      "$ROOT/check_e2e.sh"      || ec=1 ;;
  esac
done
exit "$ec"
