#!/usr/bin/env bash
# fake ssh for phase-06-capi-stack.sh tests. Reads the remote block from stdin,
# identifies the sub-step by a distinctive token, appends it to $ORDER_FILE (so
# tests can assert ORC-before-init), and emits canned output + exit code.
# Steered by env: A_FAIL B_FAIL C_FAIL D_FAIL E_FAIL F_FAIL.
body="$(cat 2>/dev/null || true)"
log() { [ -n "${ORDER_FILE:-}" ] && printf '%s\n' "$1" >> "$ORDER_FILE"; }

if printf '%s' "$body" | grep -q 'dependencies.json'; then
  log a
  [ "${A_FAIL:-0}" = 1 ] && { echo "PIN-FAIL: CAPO=null"; exit 1; }
  echo "== pins =="; echo "CAPI=v1.13.2"; echo "== tooling =="; echo "clusterctl v1.13.2"; exit 0
elif printf '%s' "$body" | grep -q 'jetstack/cert-manager'; then
  log b
  [ "${B_FAIL:-0}" = 1 ] && { echo "Error: timed out waiting for the condition"; exit 1; }
  echo "cert-manager deployed"; exit 0
elif printf '%s' "$body" | grep -q 'server-side'; then
  log c
  [ "${C_FAIL:-0}" = 1 ] && { echo "error: no matches for kind Image"; exit 1; }
  echo "images.openstack.k-orc.cloud"; exit 0
elif printf '%s' "$body" | grep -q 'clusterctl init'; then
  log d
  [ "${D_FAIL:-0}" = 1 ] && { echo "capo-system deploy not Available"; exit 1; }
  echo "Your management cluster has been initialized successfully!"; exit 0
elif printf '%s' "$body" | grep -q 'cluster-api-addon-provider'; then
  log e
  [ "${E_FAIL:-0}" = 1 ] && { echo "Error: helm timeout"; exit 1; }
  echo "addon + janitor Running"; exit 0
elif printf '%s' "$body" | grep -q 'STACK: OK'; then
  log f
  [ "${F_FAIL:-0}" = 1 ] && { echo "NOT-RUNNING:"; echo "capo-system/capo-controller CrashLoopBackOff"; exit 1; }
  echo "STACK: OK"; exit 0
fi
echo "fake-ssh: unrecognized block" >&2
exit 0
