#!/usr/bin/env bash

: <<'END'
maidenhead - python script wrapper for maidenhead.py

Genrate Maidenhead Grid Square from Latitude & Longitude

Version 1.0a

Steve de Bode - W0FFS - December 2025

Input:	latitude Longitude
Output: Formatted grid
END

UI_LIB="/usr/local/lib/digihub/dh.lib"
[ -r "$UI_LIB" ] && source "$UI_LIB" || {
  printf "\nERROR: Missing UI library: %s\n\n" "$UI_LIB" >&2
  exit 1
}

# Check Parameters
if [ "$#" -ne 2 ]; then
  err_section "Incorrect Syntax"
  err_kv "Syntax" "${0##*/} <Latitude> <Longitude>"
  printf "\n" >&2
  exit 2
fi

lat="$1"
lon="$2"

grid="$(maidenhead.py "$lat" "$lon" 2>/dev/null)"
rc=$?

case $rc in
  0)
    section "Maidenhead Grid"
    kv "Latitude"  "$lat"
    kv "Longitude" "$lon"
    kv "Grid"      "$grid"
    printf "\n"
    ;;
  1)
    err_section "Invalid Coordinates"
    err_kv "Latitude"  "$lat"
    err_kv "Longitude" "$lon"
    printf "\n" >&2
    exit 1
    ;;
  2)
    err_section "Invalid Arguments"
    err_kv "Latitude"  "$lat"
    err_kv "Longitude" "$lon"
    err_kv "Hint"      "Use decimal degrees (e.g., 28.1899 -82.50036)"
    printf "\n" >&2
    exit 2
    ;;
  *)
    err_section "Error"
    err_kv "Message" "Unexpected failure"
    printf "\n" >&2
    exit 1
    ;;
esac
