#! /bin/bash -x

# Script to start up the processess related to the SMNAMonitoringApp (i.e., get the current date and update the dates within scripts).
#
# Note: make sure to run this script at times 01, 07, 13 and 19 UTC (otherwise it will choke at 00 UTC)
#
# @cfbastarz (March, 2024)

#inctime=${HOME}/bin/inctime

lpath=/lustre_xc50/carlos_bastarz/SMNAMonitoringApp/cron_scripts/anls_img

today=$(date '+%Y%m%d%H')
#today=2025102213
#today=2025022501
#today=2025101601
#today=2024031407
#today=2024031413
#today=2024031419
#today=2025120607

# Como os dados são pós-processados apenas às 00Z, então forçamos que a hora sinótica seja sempre 00
#if [ ${today:8:2} -le 10 ]
#then
#  todaym1H=${today:0:8}0$((${today:8:2}-1))
#else
#  todaym1H=${today:0:8}$((${today:8:2}-1))
#fi

todaym1H=${today:0:8}00

#yesterday=$(${inctime} ${todaym1H} -1d %y4%m2%d2%h2)
#aweekbefore=$(${inctime} ${todaym1H} -7d %y4%m2%d2%h2)

yesterday=$(date -u +%Y%m%d%H -d "${todaym1H:0:8} ${todaym1H:8:2} -24 hours")
aweekbefore=$(date -u +%Y%m%d%H -d "${todaym1H:0:8} ${todaym1H:8:2} -168 hours")

echo ${todaym1H} > ${lpath}/todaym1H.txt
echo ${aweekbefore} > ${lpath}/aweekbefore.txt

echo "Updating script ${lpath}/run_plot_map-smna.sh"
cat ${lpath}/run_plot_map-smna.sh-template | sed "s,#DATAI#,${aweekbefore},g" > ${lpath}/run_plot_map-smna.sh

echo "Updating script ${lpath}/run_plot_map-bam.sh"
cat ${lpath}/run_plot_map-bam.sh-template | sed "s,#DATAI#,${aweekbefore},g" > ${lpath}/run_plot_map-bam.sh

exit 0
