#!/bin/sh

set -eu

VERSION="0.0.1-20250115"

help() {
  echo "Freeleaps Cluster Auth Retriever (Version: ${VERSION})"
  echo ""
  echo "FYI: This script is used to retrieve the auth of the infra services, just for internal and temporary use."
  echo ""
  echo "Usage: infra-auth-retriver <sub-commands>"
  echo ""
  echo "Sub Commands:"
  echo "  help,-h,--help        Show help"
  echo "  grafana               Retrieve Grafana Auth"
  echo "  argocd                Retrieve ArgoCD Auth"
}

main() {
  if [ "$#" -ne 1 ]; then
    echo "[ERROR] No sub-commands provided."
    echo "[TIP] Use 'infra-auth-retriver help' to get more information."
    exit 1
  fi

  sub_command="$1"

  case "${sub_command}" in
    help|-h|--help)
      help
      ;;
    grafana)
      echo "Grafana User: $(kubectl get secret kube-prometheus-stack-grafana -n freeleaps-monitoring-system -o jsonpath='{.data.admin-user}' | base64 -d)"
      echo "Grafana Password: $(kubectl get secret kube-prometheus-stack-grafana -n freeleaps-monitoring-system -o jsonpath='{.data.admin-password}' | base64 -d)"
      ;;
    argocd)
      echo "ArgoCD User: admin"
      echo "ArgoCD Auth: $(kubectl get secret argocd-initial-admin-secret -n freeleaps-devops-system -o jsonpath='{.data.password}' | base64 -d)"
      ;;
    *)
      help
      ;;
  esac
}

main "$@"