#!/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"
  echo "  kafka                 Retrieve Kafka password"
}

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 "Note: The 'admin' account has been disabled."
      echo "Please choose 'Sign in with MATHMAST AD' to log in."
      echo "Grafana URL: https://grafana.mathmast.com/"
      ;;
    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)"
      ;;
    kafka)
      echo "Kafka Username: freeleaps"
      echo "Kafka Password: $(kubectl get secret kafka-user-passwords --namespace freeleaps-data-platform -o jsonpath='{.data.client-passwords}' | base64 -d | cut -d , -f 1)"
      ;;
    *)
      help
      ;;
  esac
}

main "$@"