#!/usr/bin/env bash
# fake openstack for the amphora-pipeline harness. Honors `-f value -c <col>...`.
#   image list --tag <t>  -> amphora row present iff $MARKER exists OR AMPH_PRESENT=1
#   image list --name <n> -> base row present iff BASE_PRESENT=1
#   image create ...      -> echoes a fake base id
cmd="${1:-}"; sub="${2:-}"; shift 2 2>/dev/null || true
if [ "$cmd" = "image" ] && [ "$sub" = "list" ]; then
  qtype=""; cols=()
  while [ $# -gt 0 ]; do
    case "$1" in
      --tag)  qtype="tag";  shift 2 ;;
      --name) qtype="name"; shift 2 ;;
      -c)     cols+=("$2"); shift 2 ;;
      -f)     shift 2 ;;
      *)      shift ;;
    esac
  done
  present=0
  if [ "$qtype" = "tag" ]; then
    { [ "${AMPH_PRESENT:-0}" = "1" ] || [ -f "${MARKER:-/nonexistent}" ]; } && present=1
    ID="c8f9f53a-amph"; NAME="amphora-haproxy-x86_64-ubuntu-22.04-test"; STATUS="active"
  else
    [ "${BASE_PRESENT:-0}" = "1" ] && present=1
    ID="6ed034f7-base"; NAME="jammy-amphora-base"; STATUS="active"
  fi
  [ "$present" = "1" ] || exit 0
  out=""
  for c in "${cols[@]}"; do
    case "$c" in ID) v="$ID" ;; Name) v="$NAME" ;; Status) v="$STATUS" ;; *) v="" ;; esac
    out="${out:+$out }$v"
  done
  printf '%s\n' "$out"
  exit 0
elif [ "$cmd" = "image" ] && [ "$sub" = "create" ]; then
  echo "6ed034f7-base"   # fake base id (the script captures -f value -c id)
  exit 0
fi
exit 0
