#!/bin/bash
#
# Indices Solaires pour Svxlink
# Dépendances: curl, wget 
# F4GFZ Août 2022 - mod Avril 2024

url='http://dk0wcy.de/magnetogram/'
html=$( curl -# -L "${url}" 2> '/dev/null' )
wget -q -O /home/svxlink/wwv.txt https://services.swpc.noaa.gov/text/wwv.txt
isnumber='^[0-9]+$'

value=$(
  <<< "${html}" \
  grep -Po '(?<=b>)[^<][^ <]*'
)
declare -a wx_array=($value)

#########################
#    ${wx_array[@]}     #
#                       #
# 0 = Boulder A         #
# 1 = Kiel 3-hour k     #
# 2 = Solar Activity    #
# 3 = Kiel A            #
# 4 = Kiel current k    #
# 5 = Geomagnetic Field #
# 6 = Sunspot Number    #
# 7 = Aurora            #
# 8 = Solar Flux        #
#########################

# echo 'CW::play "vvv" 120 800 -4;'
echo 'playMsg "Condx" "intro";'
echo 'playSilence 200;'
if [[ ${wx_array[8]} =~ $isnumber ]] ; then
  echo 'playMsg "Condx" "flux";'
  echo 'playNumber '${wx_array[8]}';'
  echo 'playSilence 200;'
fi
if [[ ${wx_array[6]} =~ $isnumber ]] ; then
  echo 'playMsg "Condx" "sunspots";'
  echo 'playNumber '${wx_array[6]}';'
  echo 'playSilence 200;'
fi
if [[ ${wx_array[3]} =~ $isnumber ]] ; then
  echo 'playMsg "Condx" "indicea";'
  echo 'playNumber '${wx_array[3]}';'
  echo 'playSilence 200;'
fi
if [[ ${wx_array[1]} =~ $isnumber ]] ; then
  echo 'playMsg "Condx" "indicek";'
  echo 'playNumber '${wx_array[1]}';'
  echo 'playSilence 200;'
fi
echo 'playMsg "Condx" "activity";'
echo 'playMsg "Condx" "'${wx_array[2]}'";'
echo 'playSilence 200;'
# echo 'playMsg "Condx" "geofield";'
# echo 'playMsg "Condx" "'${wx_array[5]}'";'

# Geomagnetic Storms
geo=$( grep -q -E "(^Geomag.+?expected.$|^Geomag.+?likely.$)" /home/svxlink/wwv.txt ; case "$?" in "0") echo geo`grep -E "(^Geomag.+?expected.$|^Geomag.+?likely.$)" /home/svxlink/wwv.txt |awk '{print(substr($5, length($5), length($5)))}' |cut -f1 -d"."|tr '[a-z]' '[A-Z]'` ;; "1") echo "0" ;; *) echo "error: $?" ;; esac )
# Solar Radiation Storms
sol=$( grep -q -E "(^Solar.+?expected.$|^Solar.+?likely.$)" /home/svxlink/wwv.txt ; case "$?" in "0") echo sol`grep -E "(^Solar.+?expected.$|^Solar.+?likely.$)" /home/svxlink/wwv.txt |awk '{print(substr($6, length($6), length($6)))}' |cut -f1 -d"."|tr '[a-z]' '[A-Z]'` ;; "1") echo "0" ;; *) echo "error: $?" ;; esac )
# Radio Blackouts
rad=$( grep -q -E "(^Radio.+?expected.$|^Radio.+?likely.$)" /home/svxlink/wwv.txt ; case "$?" in "0") echo rad`grep -E "(^Radio.+?expected.$|^Radio.+?likely.$)" /home/svxlink/wwv.txt |awk '{print(substr($5, length($5), length($5)))}' |cut -f1 -d"."|tr '[a-z]' '[A-Z]'` ;; "1") echo "0" ;; *) echo "error: $?" ;; esac )

if [[ $geo =~ "geo" ]] ; then
  echo 'playMsg "Condx" "geofield";'
  echo 'playNumber '${geo: -1}';'
  echo 'playSilence 200;'
fi
if [[ $sol =~ "sol" ]] ; then
  echo 'playMsg "Condx" "radiation";'
  echo 'playNumber '${sol: -1}';'
  echo 'playSilence 200;'
fi
if [[ $rad =~ "rad" ]] ; then
  echo 'playMsg "Condx" "blackout";'
  echo 'playNumber '${rad: -1}';'
  echo 'playSilence 200;'
fi

rm /home/svxlink/wwv.txt