#!/usr/bin/env bash
a1="${1:-}"; a2="${2:-}"; a3="${3:-}"; rest=" $* "
FIPVAL="${FIPVAL:-10.12.5.103}"; TENANT="${TENANT:-10.20.0.107}"
case "$a1 $a2" in
  "token issue")    [ "${SCOPE_FAIL:-0}" = 1 ] && exit 1; exit 0 ;;
  "keypair show")   [ "${KP_VIS:-1}" = 1 ] && exit 0 || exit 1 ;;
  "security group") [ "${SG_VIS:-1}" = 1 ] && { echo sg-id; exit 0; } || exit 1 ;;   # 'security group show'
  "network show")   [ "${NET_VIS:-1}" = 1 ] && { echo net-id; exit 0; } || exit 1 ;;
  "server show")
    { [ "${VM_PRESENT:-0}" = 1 ] || [ -f "${MK_VM:-/x}" ]; } || exit 1
    if printf '%s' "$rest" | grep -q -- '-f json'; then
      if [ "${ADDR_ONLY_FIP:-0}" = 1 ]; then echo "{\"addresses\":{\"capi-mgmt-net\":[\"$FIPVAL\"]}}"
      else echo "{\"addresses\":{\"capi-mgmt-net\":[\"$TENANT\",\"$FIPVAL\"]}}"; fi
    elif printf '%s' "$rest" | grep -q -- '-c status'; then
      [ -f "${MK_RECREATED:-/x}" ] && echo ACTIVE || echo "${VM_STATUS:-ACTIVE}"
    else
      echo vm-id
    fi
    exit 0 ;;
  "server create") [ -f "${MK_DELETED:-/x}" ] && : > "${MK_RECREATED:?}"; : > "${MK_VM:?}"; exit 0 ;;
  "server delete") : > "${MK_DELETED:?}"; rm -f "${MK_VM:-/x}"; exit 0 ;;
  "server add")    exit 0 ;;
  "port list")     echo port-id; exit 0 ;;
  "floating ip")
    case "$a3" in list) [ "${FIP_PRESENT:-0}" = 1 ] && echo "$FIPVAL"; exit 0 ;; create) echo "$FIPVAL"; exit 0 ;; esac ;;
esac
exit 0
