#!/bin/bash

# 3000km MUF(D) F2 script pour Svxlink
# Dépendances: curl
# F4GFZ Octobre 2023

# source: https://giro.uml.edu/didbase/
# Dourbes, Belgique = DB049

ID="DB049"
RESULT="$(curl -s "https://lgdc.uml.edu/common/DIDBGetValues?ursiCode=$ID&charName=MUFD&DMUF=3000&fromDate=`date +%Y%%2F%m`%2F01+01%3A00%3A00&toDate=`date +%Y`%2F12%2F31+03%3A00%3A00" |tail -n 1|awk '{print "3000km F2 MUF(D) = " $3 " MHz ["$1"]"}')"
MUF="$(echo $RESULT |awk '{print $5}')"
pattern=".+html.+"

if [[ ! $RESULT =~ $pattern ]]
then
 TIME="$(echo $RESULT |awk -F"T" '{print $2}' |awk -F"." '{print $1}')"
 D="$(date -u -d @$(( $(date +%s) - $(date -u -d $TIME +%s) )) +%T |awk -F":" '{print $1}')"
 if [[ $D -gt 0 ]]
 then
  DELTA="$(date -u -d @$(( $(date +%s) - $(date -u -d $TIME +%s) )) +%T |awk -F":" '{print $1" "$2}')"
 else
  # DELTA="$(date -u -d @$(( $(date +%s) - $(date -u -d $TIME +%s) )) +%T |awk -F":" '{print "00 "$2}')" # heure+min
  DELTA="$(date -u -d @$(( $(date +%s) - $(date -u -d $TIME +%s) )) +%T |awk -F":" '{print $2}')"
 fi
 echo 'playSilence 400;'
 echo "playMsg \"Condx\" \"muftime\""
 echo "playNumber" `echo ${DELTA}`
 echo "playMsg \"Condx\" \"minutes\""
 echo 'playSilence 200;'
 echo "playMsg \"Condx\" \"muf\""
 echo "playNumber" `echo ${MUF}`
 echo "playMsg \"Condx\" \"MHz\""
else
 echo ""
fi