#!/usr/bin/env bash
# fake juju for phase-07-conductor-graft.sh tests.
# Logs every call to $JUJU_LOG; keeps decoded-file state in $JUJU_STATE so the
# 7.2 sha256 both-sides gate passes legitimately. Dispatches `juju ssh` remote
# commands by substring. Steered by env:
#   DOMAIN_SETUP_FAIL TCP_FAIL SHA_MISMATCH DRIVER_MISSING NOTACTIVE
#   SHOWARGS_NODIR PS_NODIR NODRIVER_ENABLED
: "${JUJU_LOG:=/dev/null}"
: "${JUJU_STATE:=/tmp}"
printf 'juju %s\n' "$*" >> "$JUJU_LOG"

sub="${1:-}"; shift || true

if [ "$sub" = "run" ]; then
  # juju run magnum/leader domain-setup
  [ "${DOMAIN_SETUP_FAIL:-0}" = 1 ] && exit 1
  echo "Running domain-setup on magnum/leader"; exit 0
fi

if [ "$sub" != "ssh" ]; then exit 0; fi

# strip: -m MODEL UNIT ; the remainder is the remote command (1+ args)
while [ "${1:-}" = "-m" ]; do shift 2; done
shift || true          # drop the UNIT
REMOTE="$*"

emit_kubeconfig_sha() {
  if [ "${SHA_MISMATCH:-0}" = 1 ]; then
    echo "0000000000000000000000000000000000000000000000000000000000000000  /etc/magnum/kubeconfig"
  elif [ -f "$JUJU_STATE/kubeconfig" ]; then
    sha256sum "$JUJU_STATE/kubeconfig" | awk '{print $1"  /etc/magnum/kubeconfig"}'
  else
    echo "(no kubeconfig)"; return 1
  fi
}

case "$REMOTE" in
  *"base64 -d > /etc/magnum/kubeconfig"*)     # 7.2 write -- stdin is base64 payload (must precede the getent match)
    base64 -d > "$JUJU_STATE/kubeconfig" 2>/dev/null || true; exit 0 ;;
  *"echo TCP-OK"*)
    if [ "${TCP_FAIL:-0}" = 1 ]; then echo "TCP-FAIL"; else echo "TCP-OK"; fi ;;
  *"systemctl show magnum-conductor -p User"*)
    echo "magnum" ;;
  *"[m]agnum-conductor"*)          # ps fallback owner probe
    echo "magnum" ;;
  *"getent passwd magnum"*)
    exit 0 ;;
  *"sha256sum /etc/magnum/kubeconfig"*)
    emit_kubeconfig_sha ;;
  *"curl "*pypi*|*"pypi:"*)
    echo "pypi:200"; echo "helm:200" ;;
  *"WANT="*"get.helm.sh"*)          # helm install block
    echo "[OK] installed v3.17.3+ge4da497" ;;
  *"command -v helm"*)              # restricted-PATH gate
    echo "/usr/bin/helm"; echo "v3.17.3+ge4da497" ;;
  *"pip install"*"magnum-capi-helm"*)
    echo "Successfully installed magnum-capi-helm-1.4.0" ;;
  *"pip show magnum-capi-helm"*)
    echo "Version: 1.4.0" ;;
  *"entry_points"*)
    if [ "${DRIVER_MISSING:-0}" = 1 ]; then echo "['k8s_fedora_coreos_v1']"; else echo "['k8s_capi_helm_v1', 'k8s_fedora_coreos_v1']"; fi ;;
  *"helm --kubeconfig /etc/magnum/kubeconfig list -A"*)   # auth proof
    echo "NAME          NAMESPACE     REVISION  STATUS    CHART"
    echo "cert-manager  cert-manager  1         deployed  cert-manager-v1.20.2"
    echo "ck-network    kube-system   1         deployed  cilium-1.17.12" ;;
  *"install -d"*"magnum.conf.d"*)
    mkdir -p "$JUJU_STATE/conf.d"; exit 0 ;;
  *"tee /etc/magnum/magnum.conf.d/00-capi-helm.conf"*)    # 7.6 write -- stdin payload
    mkdir -p "$JUJU_STATE/conf.d"; cat > "$JUJU_STATE/conf.d/00-capi-helm.conf"; exit 0 ;;
  *"chmod 0644 /etc/magnum/magnum.conf.d/00-capi-helm.conf"*)
    exit 0 ;;
  *"grep -q '^default_helm_chart_version = "*)
    grep -q '^default_helm_chart_version = 0.25.1$' "$JUJU_STATE/conf.d/00-capi-helm.conf"; exit $? ;;
  *"grep -nP '[^\\x00-\\x7F]' /etc/magnum/magnum.conf.d/00-capi-helm.conf"*)
    LC_ALL=C grep -nP '[^\x00-\x7F]' "$JUJU_STATE/conf.d/00-capi-helm.conf"; exit $? ;;   # empty -> exit 1 (no non-ascii)
  *"tee /etc/default/magnum-conductor"*)
    cat > "$JUJU_STATE/default-conductor"; exit 0 ;;
  *"show-args"*)
    if [ "${SHOWARGS_NODIR:-0}" = 1 ]; then
      echo "/usr/bin/magnum-conductor --config-file=/etc/magnum/magnum.conf --log-file=/var/log/magnum/magnum-conductor.log"
    else
      echo "/usr/bin/magnum-conductor --config-file=/etc/magnum/magnum.conf --config-dir /etc/magnum/magnum.conf.d --log-file=/var/log/magnum/magnum-conductor.log"
    fi ;;
  *"sudo bash -s"*)                 # 7.7b heredoc -- consume + simulate success
    cat >/dev/null; exit 0 ;;
  *"grep -c '^auth_version = v3"*)
    echo "2" ;;
  *"grep -c -- '--config-dir /etc/magnum/magnum.conf.d' /etc/default/magnum-api"*)
    echo "1" ;;
  *"systemctl restart magnum-conductor magnum-api"*)
    if [ "${NOTACTIVE:-0}" = 1 ]; then echo "active"; echo "failed"; else echo "active"; echo "active"; fi ;;
  *"ps -ww -C magnum-conductor"*|*"ps -ww -C magnum-api"*)
    if [ "${PS_NODIR:-0}" = 1 ]; then
      echo "/usr/bin/python3 /usr/bin/magnum-x --config-file=/etc/magnum/magnum.conf --log-file=/var/log/magnum/x.log"
    else
      echo "/usr/bin/python3 /usr/bin/magnum-x --config-file=/etc/magnum/magnum.conf --config-dir /etc/magnum/magnum.conf.d --log-file=/var/log/magnum/x.log"
    fi ;;
  *"magnum-driver-manage list-drivers"*)
    if [ "${NODRIVER_ENABLED:-0}" = 1 ]; then echo "| k8s_fedora_coreos_v1 |"; else echo "| k8s_capi_helm_v1 |"; echo "| k8s_fedora_coreos_v1 |"; fi ;;
  *)
    exit 0 ;;
esac
exit 0
