#!/bin/sh set -Eeuo pipefail source data/weather-schema FIELD_COUNT=$(echo $SCHEMA | grep -o ";" | wc -l) URL=https://ssl.smn.gob.ar/dpd/zipopendata.php?dato=tiepre TMPDIR=/tmp/weather OUT=$HOME/.weather CITY="La Plata" print=false help() { cat <&2 help exit 1 ;; esac done # Create dir if not exists if [ ! -d /tmp/weather ]; then mkdir -p $TMPDIR fi # Get the dataset curl -s "$URL" --output $TMPDIR/weather.zip unzip -q $TMPDIR/weather.zip -d $TMPDIR # Sanitize iconv -f latin1 -t utf8 $TMPDIR/*.txt | tr -d ' ' > $TMPDIR/final.txt # Cut city data data=$(cat $TMPDIR/final.txt | grep -i "$CITY" || echo "notfound") if [ "$data" = "notfound" ]; then echo "Error: City/Weather station not found" cleanup exit 1 fi # Process string processed="" for (( i = 0; i <= $FIELD_COUNT; i++ )); do field_number=$(($i +1)) field=$(echo $SCHEMA | cut -d ";" -f $field_number) value=$(echo $data | cut -d ";" -f $field_number) line="${field}=$(echo ${value/\//} | sed -e 's/^[ ]*//')" if [ ! $i = $FIELD_COUNT ]; then processed="${processed}${line}\n" else processed="${processed}${line}" fi done # Print or save? if [ "$print" = true ]; then echo -e "$processed" else if [ -f $OUT ]; then rm $OUT fi echo -e "$processed" > $OUT fi # Cleanup cleanup