#!/usr/bin/env bash

##########################################
# country should be written with first letter as capital, or how appears in coords.conf
# date currently does not function and will return always today's date (or most recent available)
# only monarch accepts all variables, the rest get od550_dust and sconc_dust
# anim should only be called with true here, as the pngs are automatically deleted
# ./bin/gen_country_loop.sh monarch true sconc_dust 20220808 spain

# generate daily maps for specified couuntry
# usage: script.sh [model] [anim] [variable] [date] [country]

if [ "$1" != "" ]; then
    model="$1"
else
    model="all"
fi

if [ "$2" != "" ]; then
    anim="$2"
else
    anim="true"
fi

if [ "$3" != "" ]; then
    curvar="$3"
else
    curvar="all"
fi

if [ "$4" != "" ]; then
    curdate="$4"
else
    curdate=`date '+%y%m%d' -d '-1day'`
fi

if [ "$5" != "" ]; then
    country="$5"
else
    country="spain"
fi

curyear=`date '+%Y' -d ${curdate}`
curmon=`date '+%m' -d ${curdate}`
repodir='/data/daily_dashboard/comparison/'

for var in od550_dust sconc_dust dust_depd dust_depw dust_load dust_ext_sfc
do
	variable=$var
	echo "************ $model $anim $curvar $variable $curdate $country****************"
	if [ "$curvar" != "$variable" ] && [ "$curvar" != "all" ]; then
		continue
	fi
	tmpdir=${HOME}/tmp/${var}
	mkdir -p $tmpdir
	rm -rf $tmpdir/*
	if [ "$variable" != "od550_dust" ] && [ "$variable" != "sconc_dust" ]; then
	    model="monarch"
	    node js/create_country_loop.js $anim $model $curdate $variable $country
	    wait
	    sleep 1
	    convert -loop 0 -delay 25 ${tmpdir}/${curdate}_${model}_${country}_??.png ${tmpdir}/${curdate}_${model}_${country}_loop.gif
	    currepo=${repodir}/${model}/${variable}/${curyear}/${curmon}/${country}
	    mkdir -p $currepo
	    wait
	    mv ${tmpdir}/${curdate}_${model}_${country}_* $currepo
	    rm $currepo/${curdate}_${model}_${country}_??.png

	elif [ "$model" == "all" ]; then
	    for mod in `cat interactive-forecast-viewer/conf/models.json | grep '": {' | sed 's/^.*"\(.*\)".*$/\1/g'`
		do
		    node js/create_country_loop.js $anim $mod $curdate $variable $country
		    wait
	    	    sleep 1
		    convert -loop 0 -delay 25 ${tmpdir}/${curdate}_${mod}_${country}_??.png ${tmpdir}/${curdate}_${mod}_${country}_loop.gif
		    currepo=${repodir}/${mod}/${variable}/${curyear}/${curmon}/${country}
		    mkdir -p $currepo
		    wait
		    mv ${tmpdir}/${curdate}_${mod}_${country}_* $currepo
		    rm $currepo/${curdate}_${mod}_${country}_??.png
		done
	else
	    node js/create_country_loop.js $anim $model $curdate $variable $country
	    wait
	    sleep 1
	    convert -loop 0 -delay 25 ${tmpdir}/${curdate}_${model}_${country}_??.png ${tmpdir}/${curdate}_${model}_${country}_loop.gif
	    currepo=${repodir}/${model}/${variable}/${curyear}/${curmon}/${country}
	    mkdir -p $currepo
	    wait
	    mv ${tmpdir}/${curdate}_${model}_${country}_* $currepo
	    rm $currepo/${curdate}_${model}_${country}_??.png
	fi

	wait
	sleep 1
done
