#!/bin/bash
# a quick and dirty script to rerun the postprocessing in case something failed...
# rhand@|giub.unibe.ch, 2019-11-26

set -ex

yearlist=$*

expname=$(pwd | sed -e "s&.*/&&")

for year in ${yearlist}; do
   
   sed -e "s&#DPATH&DPATH=$(pwd)/work&" -e "s/#EXP/EXP=${expname}/" -e "s/#DAY/DAY=01/" -e "s/#MONTH/MONTH=12/" -e "s/#YEAR/YEAR=${year}/"  -e "s&\$SUBJOB2&rerun.subjob2.${year}&" -e "s&\${SUBJOB2}&rerun.subjob2.${year}&"  ${expname}.job1 > work/rerun.subjob1.${year}
   sed -e "s&#DPATH&DPATH=$(pwd)/work&" -e "s/#EXP/EXP=${expname}/" -e "s/#DAY/DAY=01/" -e "s/#MONTH/MONTH=12/" -e "s/#YEAR/YEAR=${year}/"  ${expname}.job2 > work/rerun.subjob2.${year}

   cd work && previousjob=$(sbatch ${dependency} rerun.subjob1.${year} | sed -e "s/Submitted batch job //") && cd -

   dependency="--dependency=afterany:${previousjob}"

done
