#!/usr/bin/env bash
# fake kubectl for phase-06-kubeconfig-gate.sh tests.
# Steered by env: CLUSTER_NAME_OUT SET_CLUSTER_NOOP NODE_NOTREADY POD_PHASE POD_STATE.
a1="${1:-}"; a2="${2:-}"; rest=" $* "
case "$a1 $a2" in
  "config view")
    echo "${CLUSTER_NAME_OUT:-k8s}" ;;
  "config set-cluster")
    srv=""
    for a in "$@"; do case "$a" in --server=*) srv="${a#--server=}";; esac; done
    if [ "${SET_CLUSTER_NOOP:-0}" != 1 ] && [ -n "${KUBECONFIG:-}" ] && [ -f "${KUBECONFIG:-}" ]; then
      sed -i -E "s#^([[:space:]]*server:).*#\1 $srv#" "$KUBECONFIG"
    fi
    echo "Cluster set." ;;
  "get nodes")
    st="Ready"; [ "${NODE_NOTREADY:-0}" = 1 ] && st="NotReady"
    echo "NAME           STATUS   ROLES                  AGE   VERSION"
    echo "capi-mgmt-v2   $st   control-plane,worker   12m   v1.32.13" ;;
  "get pod")
    if printf '%s' "$rest" | grep -q 'status.phase'; then
      echo "${POD_PHASE:-Succeeded}"
    elif printf '%s' "$rest" | grep -q 'containerStatuses'; then
      echo "${POD_STATE:-{\"terminated\":{\"reason\":\"Completed\",\"exitCode\":0}}}"
    fi ;;
  "delete pod")   exit 0 ;;
  "run egress-test") exit 0 ;;
esac
exit 0
