#!/bin/bash

set +xv


date=${1}
day_end=$( date -d "${date:4:2}/1 + 1 month - 1 day" +"%d" )
date_end=${date:0:6}${day_end}
ym=${date:0:6}
day_nb=$(( ($(date -d "${date_end}H00 + 1 day" +%s) - $(date -d "${ym}01H00" +"%s") )/(60*60*24) ))

dir_out="/ec/res4/scratch/ar0s/ERA5/GRIB_N320/${ym}"; mkdir -p ${dir_out}
dir_req=$(dirname ${2}:-".")
file_req=$(basename ${2%*.req*})
req_type=${file_req/mars_} ; req_type=${req_type%%_*}
file_out="${req_type}_ERA5_L*_GLO_TLAUTO_IGNONE_GN320_ana01h_${date}.grib"

if [ ! -s ${dir_out}/${file_out} ]; then

  # EDIT MARS REQ
  if [ -f ${dir_req}/${file_req}.req.base ]; then
    sed -e "s~date_req~${date}~g; s~dir_out~${dir_out}~g" < ${dir_req}/${file_req}.req.base > ${dir_req}/JOBS/${file_req}_${date}.req
  else
    echo "MARS REQUEST NOT FOUND !"
    exit 1
  fi

  # SUBMIT MARS REQUEST
  while [ $(squeue -u $USER -h -t pending,running -r | wc -l) -eq 90 ]; do echo "sleeping..."; sleep 10; done 
  if [ ${date} -eq ${date_end} ]; then echo "waiting..."; w="--wait"; fi

  sbatch ${w} --job-name=mars_${req_type}_${date} <<EOF
#!/bin/bash
#SBATCH --job-name=marsreq
#SBATCH --output=JOBS/%x-%j.out 
#SBATCH --qos=nf
#SBATCH --time=00:30:00

set +xv

mars ${dir_req}/JOBS/${file_req}_${date}.req

exit 0

EOF

  if [ -n "${w}" ]; then
    file_nb=$( eval "ls ${dir_out}/${file_out/${date}/${ym}??} 2>/dev/null" | wc -l )
    if [ ${day_nb} -eq ${file_nb} ]; then echo "OK"; exit 0; else echo "ERROR"; exit 1; fi
  fi

fi
