## https://unix.stackexchange.com/questions/554595/how-to-iterate-a-csv-file-in-bash
## https://unix.stackexchange.com/questions/198065/skip-the-first-6-lines-rows-in-a-text-file-with-awk

##lastquake static map api
##$img='https://maps.googleapis.com/maps/api/staticmap?center='.$lat.','.$lon.'&language=es&zoom=7&size=640x361&maptype=hybrid&markers=color:red%7C'.$lat.','.$lon.'&key=AIzaSyAZKwNKvH-tc6wj7QjHc2gxw6LwUjh78zE';

#awk -F, 'NR>1 { print "/Users/neavilag/opt/anaconda3/envs/telegram/bin/python /Users/neavilag/NA_files/devdrive/conda_envs/postmessageto_channel_group_privatechat_withbot/command_handler.py -d \"user(@neavilag)\" -m \"Tormenta detectada a", $4,"kms. de",$2,$3," (",$1, "flashes/15m)\""}' rayos.csv > msj.sh
#source msj.sh

### usage : bash loop_alerts.sh rayos
### usage : bash loop_alerts.sh goes16_13dif

. ./config.cfg  ##include configuration file

curl $alerts_url/$1.csv > alerts_data.csv
###. clean data
##/usr/bin/tr -d '"' <alerts_data.csv
cat alerts_data.csv


python_path="$gdal_path"
tele_path="/root/projects/telegram_bot"

while IFS=, read -r valor city country distance bearing lon lat elev; do 
  echo $valor $city $distance
  if [ $distance != "distance" ] ; then 
    image="https://maps.googleapis.com/maps/api/staticmap?center=$lat,$lon&language=es&zoom=11&size=512x512&maptype=hybrid&markers=color:red%7C$lat,$lon&key=AIzaSyAZKwNKvH-tc6wj7QjHc2gxw6LwUjh78zE"
    imagecy="https://us-central1-fbcapture-9109f.cloudfunctions.net/screenshot2?height=864&width=864&url=https%3A%2F%2Fclimaya.com%2Fappjs%2Fsat%2Fmap.html%3Fglm%3D1%26z%3D9%26lat%3D$lat%26lon%3D$lon%26pin%3D1"
    ## echo $image
    curl $imagecy > mapa.png
    cp mapa.png $alerts_path/

    ##dest="user(@neavilag)"
    dest="group(GroupTesting)"
    urlcy="https://climaya.com/rayos-en-tiempo-real/?param=$lat%7C$lon%7C10"
    echo $urlcy

    if [ "$country" = "CU" ] ; then 
      flag="🇨🇺"
    fi
    if [ "$country" = "MX" ] ; then 
      flag="🇲🇽"
    fi
    if [ "$country" = "BZ" ] ; then 
      flag="🇧🇿"
    fi
    if [ "$country" = "GT" ] ; then
      flag="🇬🇹"
    fi
     if [ "$country" = "SV" ] ; then
      flag="🇸🇻"
    fi
    if [ "$country" = "HN" ] ; then 
      flag="🇭🇳"
    fi
    if [ "$country" = "NI" ] ; then 
      flag="🇳🇮"
    fi
    if [ "$country" = "CR" ] ; then 
      flag="🇨🇷"
    fi
    if [ "$country" = "PA" ] ; then 
      flag="🇵🇦"
    fi
    if [ "$country" = "CO" ] ; then
      flag="🇨🇴"
    fi
    if [ "$country" = "VE" ] ; then 
      flag="🇻🇪"
    fi
    if [ "$country" = "DO" ] ; then 
      flag="🇩🇴"
    fi
    if [ "$country" = "BR" ] ; then 
      flag="🇧🇷"
    fi
    if [ "$country" = "AR" ] ; then 
      flag="🇦🇷"
    fi
    if [ "$country" = "UY" ] ; then 
      flag="🇺🇾"
    fi
    if [ "$country" = "CL" ] ; then 
      flag="🇨🇱"
    fi
    if [ "$country" = "PY" ] ; then 
      flag="🇵🇾"
    fi
    if [ "$country" = "PE" ] ; then 
      flag="🇵🇪"
    fi
    if [ "$country" = "EC" ] ; then 
      flag="🇪🇨"
    fi
    if [ "$country" = "BO" ] ; then 
      flag="🇧🇴"
    fi
    if [ "$country" = "HT" ] ; then 
      flag="🇭🇹"
    fi
    ###bearing
    echo "Bearing $bearing"
    bea="N/A"

    if [ "$bearing" -ge "0" ] && [ "$bearing" -lt "23" ] ; then
        bea="Norte"
    fi
    if [ "$bearing" -ge "23" ] && [ "$bearing" -lt "68" ] ; then
        bea="NorEste"
    fi
    if [ "$bearing" -ge "68" ] && [ "$bearing" -lt "113" ] ; then 
        bea="Este"
    fi
    if [ "$bearing" -ge "113" ] && [ "$bearing" -lt "158" ] ; then 
        bea="SurEste"
    fi
    if [ "$bearing" -ge "158" ] && [ "$bearing" -lt "203" ] ; then 
        bea="Sur"
    fi
    if [ "$bearing" -ge "203" ] && [ "$bearing" -lt "248" ] ; then 
        bea="SurOeste"
    fi
    if [ "$bearing" -ge "248" ] && [ "$bearing" -lt "293" ] ; then 
        bea="Oeste"
    fi
    if [ "$bearing" -ge "293" ] && [ "$bearing" -lt "338" ] ; then 
        bea="NorOeste"
    fi
    if [ "$bearing" -ge "338" ] && [ "$bearing" -le "360" ] ; then 
        bea="Norte"
    fi
  
    if [ "$1" = "rayos" ] ; then
    message="$flag Detectada TORMENTA ELÉCTRICA ⚡️ a $distance kms. al $bea de $city $country||Se registran $valor rayos en 10 min. Información EXPERIMENTAL|Mapa en tiempo real aquí: $urlcy||¡Mantente informado y seguro!"
    fi
    
    if [ "$1" = "goes16_13dif" ] ; then
    message="$flag ⚠️ Posible TIEMPO SEVERO a $distance kms. al $bea de $city $country||En los Próx. 30 mins. puede ocurrir rayos, vientos fuertes, lluvias intensas o granizo.||(EXPERIMENTAL $valor)|Ver mapa 👉 $urlcy||¡Mantente informado y seguro!"
    fi
    
    #  if [ "$1" = "pcpn_1h" ] ; then
    # message="$flag 🌧 Importante acumulación de lluvias última hora||Cerca de $city $country ($valor mm/1h)  (EXPERIMENTAL) | Precaución crecidas e inundaciones. Ver mapa tiempo real: $urlcy"
    # fi

    if [ "$1" = "pcpn_1h" ] && (( $valor < 1000 )); then
    message="$flag 🌧 Importante acumulación de lluvias última hora||Cerca de $city $country ($valor mm/1h)  (EXPERIMENTAL) | Precaución crecidas e inundaciones. Ver mapa tiempo real: $urlcy"
    fi


    echo $message
    #240321 disables temporary $python_path/python $tele_path/sender.py -d $dest -m "$1|$lat|$lon||$message" -i mapa.png

    if [ "$message" != "" ] ; then
       
        ##twitter
        echo "Twitter"
        curl -G --data-urlencode "a=CY" --data-urlencode "msg=$message" --data-urlencode "media=$alerts_url/mapa.png" https://climaya.com/twt/_twt.php
    fi
    
  fi
  done < alerts_data.csv
