#!/bin/bash
# This script is used to run a parameterised copy of a Jupyter notebook. It is
# typically run through Slurm with the sbatch command.

set -euo pipefail

# set paths to use
nb_path=$1
python_path=$2
ipcluster_profile=$3
notebook=$4
detector=$5
caltype=$6
cluster_cores=$7

echo "Running with the following parameters:"
echo "Notebook path: $nb_path"
echo "Python path: $python_path"
echo "IP-Cluster profile: $ipcluster_profile"
echo "notebook: $notebook"
echo "detector: $detector"
echo "caltype: $caltype"
echo "cluster_cores: $cluster_cores"
echo "job ID: ${SLURM_JOB_ID:-none}"

export CAL_NOTEBOOK_NAME="$notebook"

# set-up enviroment
source /etc/profile.d/modules.sh
module load anaconda/3

# make sure we use agg backend
export MPLBACKEND=AGG

# Ensure Python uses UTF-8 for files by default
export LANG=en_US.UTF-8

# start an ip cluster if requested
if [ "${ipcluster_profile}" != "NO_CLUSTER" ]
then
    ${python_path} -m IPython profile create ${ipcluster_profile} --parallel
    ${python_path} -m ipyparallel.cluster start --n=${cluster_cores} --profile=${ipcluster_profile} --daemonize &
    sleep 15
fi

echo "Running notebook"
if [ "$caltype" == "CORRECT" ]
then
  # calparrot stores and repeats calcat queries
  ${python_path} -m calparrot -- ${python_path} -m princess ${nb_path} --save
else
  ${python_path} -m princess ${nb_path} --save
fi

# stop the cluster if requested
if [ "${ipcluster_profile}" != "NO_CLUSTER" ]
then
    ${python_path} -m ipyparallel.cluster stop --profile=${ipcluster_profile}
    profile_path=$(${python_path} -m IPython locate profile ${ipcluster_profile})
    echo "Removing cluster profile from: $profile_path"
    rm -rf $profile_path
fi