#!/usr/bin/env bash
# bin/bgpx — Unified operator CLI for BGP.Exchange installer tooling.

set -Eeuo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

usage() {
  cat <<EOF
Usage: bgpx <command> [args]

Commands:
  install [...]     Run install.sh
  repair  [...]     Run repair.sh
  update  [...]     Run update.sh
  uninstall [...]   Run uninstall.sh
  health  [...]     Run bgpx-health
  bird|gortr|irrd|birdlg|nginx|zerotier <action>
                    Service module (install|remove|start|stop|restart|reload|status|health)
  roles             List available roles
  version           Print installer version
EOF
}

cmd="${1:-}"
[[ -n "${cmd}" ]] || { usage; exit 2; }
shift || true

case "${cmd}" in
  install) exec "${ROOT}/install.sh" "$@" ;;
  repair) exec "${ROOT}/repair.sh" "$@" ;;
  update) exec "${ROOT}/update.sh" "$@" ;;
  uninstall) exec "${ROOT}/uninstall.sh" "$@" ;;
  health) exec "${ROOT}/bin/bgpx-health" "$@" ;;
  bird|gortr|irrd|birdlg|nginx|zerotier)
    exec "${ROOT}/services/${cmd}.sh" "$@"
    ;;
  roles)
    # shellcheck source=/dev/null
    source "${ROOT}/lib/common.sh"
    roles_list
    ;;
  version)
    tr -d '[:space:]' <"${ROOT}/VERSION"
    echo
    ;;
  -h|--help|help) usage ;;
  *) echo "Unknown command: ${cmd}" >&2; usage; exit 2 ;;
esac
