#!/bin/bash

################################################################################
## Launcher script for Christmas Trees!
################################################################################
##
## It reads a Tree recipe recipe and splits it into atomic recipes.
## Then each atomic recipe is launched as an independent job with SLURM.
##
## Merry Christmas!
##
################################################################################

# Usage statement

function usage
{
  echo "Usage: $0 <path_to_recipe>"
  echo " "
  echo "  <path_to_recipe>: Path to the Christmas Tree recipe."
  echo " "
}

if [[ ( $@ == "--help") || $@ == "-h" ]]; then
  usage
  exit 0
fi

# Assign arguments
recipe=$1

umask 002

# Check recipe
if [ ! -f "$recipe" ]; then
  echo "Could not find the recipe file: $recipe"
  usage
  exit 1
fi

# Define tmp file to store necessary information
tmpfile=$(mktemp ${TMPDIR-/tmp}/XMAS.XXXXXX)

# Divide recipes and get layout
module load R/4.1.2-foss-2019b
echo "Counting the number of Christmas trees..."
Rscript read_ChristmasRecipe.R ${recipe} ${tmpfile}
nrows=$( head -1 $tmpfile | tail -1 )
ncols=$( head -2 $tmpfile | tail -1 )
outdir=$( head -3 $tmpfile | tail -1 )
outfile=$( head -4 $tmpfile | tail -1 )

# Create directory for slurm output
logdir=${outdir}/slurm_logs/
mkdir -p $logdir
echo "Christmas tree logs will be stored in $logdir"

# Launch one job per atomic recipe
job_number=0
# Create empty array to store all the job IDs for the verification jobs
tree_job_list=()
echo "Sending tree requests to Santa's workshop..."

# Loop over atomic recipes
for atomic_recipe in ${outdir}/santas_workshop/atomic_recipe_??.yml; do
  job_number=$(($job_number + 1))
  job_name=$(basename $outdir)_$(printf %02d $job_number)
  outlog=${logdir}/create_tree-${job_name}.out
  errlog=${logdir}/create_tree-${job_name}.err
  # Send batch job and capture job ID
  job_ID=$(sbatch --parsable --output=$outlog --error=$errlog --time=00:10:00 plot_ChristmasTree.sh ${atomic_recipe})
  # Add job ID to array
  tree_job_list+=($job_ID)
  echo "Submitted tree request $job_ID"
done
  
# Submit layout job with dependency on atomic tree jobs, passed as a 
# comma-separated string. The layout job will not run until all the 
# verification jobs have finished successfully.
# If any of the jobs fail, it will be canceled.
echo "Elves will build the Christmas card when all the trees are done..."
outlog=${logdir}/create_card.out
errlog=${logdir}/create_card.err
card_ID=$(sbatch --parsable --dependency=afterok:$(IFS=,; echo "${tree_job_list[*]}") --output=$outlog --error=$errlog --time=00:10:00 create_layout.sh $nrows $ncols $outfile)
echo "Submitted card request $cardID"

# Clean temporary files
rm $tmpfile
# rm ${outdir}/santas_workshop/*
umask 022
