#!/bin/bash
#
# Vigilance Météo pour Svxlink
# Dépendances: curl, jq et une clé API MeteoFrance (valide 3 ans)
# F4GFZ Août 2023

dept='38'
active=0
output=''
apikey='clé'
url='https://public-api.meteofrance.fr/public/DPVigilance/v1/cartevigilance/encours'

html=$(curl -X GET ${url} -H  "accept: */*" -H  "apikey: ${apikey}" 2> '/dev/null')
declare -a vigi_name=(null vent pluie orage crues neige canicule grand_froid avalanches vague_submersion)
declare -a vigi_level_name=(null Vert Jaune Orange Rouge)

for (( i=1; i<10; i++ ));
do
  vigi_level=`echo ${html} | jq '.product | .periods[0].timelaps.domain_ids[] | select(.domain_id == "'$dept'") | .phenomenon_items[] | select(.phenomenon_id == "'${i}'") | .phenomenon_max_color_id'`
  if [[ $vigi_level -gt 1 ]];then
    output+=`printf "playMsg \"VigiMeteo\" \"%s\";\n" ${vigi_level_name[$vigi_level]}`
    output+=`printf "playMsg \"VigiMeteo\" \"pour\";\n"`
    output+=`printf "playMsg \"VigiMeteo\" \"%s\";\n" ${vigi_name[$i]}`
    ((active++))
  fi
done
if [[ $active != 0 ]]; then
  printf "playMsg \"VigiMeteo\" \"introvigi\";\n"
  echo ${output}
fi