#!/usr/bin/env bash
# xone-ops: one entry point for the operator tools in this directory.
#   xone-ops list                 the tool names
#   xone-ops NAME [ARGS...]       run ops/tools/xone-NAME.py (or .sh) with ARGS, exit status unchanged
# NAME is the file name without "xone-" and the extension: preflight, ports, instance, diag, datadir, ...
# A tool run without arguments prints its own usage. This script adds no behaviour of its own.
set -euo pipefail
here=$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" && pwd)
names() { for f in "$here"/xone-*.py "$here"/xone-*.sh; do [ -e "$f" ] || continue; b=${f##*/xone-}; echo "${b%.*}"; done | sort; }
case "${1:-}" in
  ""|-h|--help|help) echo "usage: xone-ops list | xone-ops NAME [ARGS...]" >&2; names >&2; exit 2 ;;
  list) names; exit 0 ;;
esac
name=$1; shift
case "$name" in *[!a-z0-9-]*) echo "xone-ops: bad tool name" >&2; exit 2 ;; esac
for ext in py sh; do
  f="$here/xone-$name.$ext"
  [ -e "$f" ] || continue
  if [ "$ext" = py ]; then exec python3 -I "$f" "$@"; else exec bash "$f" "$@"; fi
done
echo "xone-ops: no tool named $name; try: xone-ops list" >&2
exit 2
