#!/usr/bin/env bash

: <<'END'
position
python script wrapper for position.py
uses maidenhead.py to calculate grid

Version 0.2

Steve de Bode - W0FFS - July 2026

Input:	none (GPS Device)
Output: Formatted Latitude, Longitude and 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
}

if [ "$DigiHubGPSport" = "nogps" ]; then
  err_section "GPS Not Configured"
  err_kv "Message" "No GPS device is configured for DigiHub."
  err_kv "Fix" "Run dhedit and use 'Manage GPS device' if this is incorrect."
  printf "\n" >&2
  exit 1
fi

source "$DigiHubvenv/bin/activate"
gpsposition=$(position.py)
IFS=',' read -r lat lon <<< "$gpsposition"
hamgrid=$(maidenhead.py "$lat" "$lon")
IFS=',' read -r grid <<< "$hamgrid"
deactivate

section "Current Location"
kv "Latitude"  "$lat"
kv "Longitude" "$lon"
kv "Grid"      "$grid"
printf "\n"
