#!/bin/bash

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

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 "final: $final"
echo "finalize: $finalize"
echo "cluster_cores: $cluster_cores"
echo "job ID: $SLURM_JOB_ID"

export CAL_NOTEBOOK_NAME=$notebook

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

echo "Starting influx feeder"
./cal_influx_feeder.sh $notebook $detector $caltype > /dev/null 2>&1  &

# make sure we use agg backend
export MPLBACKEND=AGG

# 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"
${python_path} -m princess ${nb_path} --save --on-error-resume-next
${python_path} -m nbconvert --to rst --TemplateExporter.exclude_input=True ${nb_path}

# 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

if [ "${final}" == "FINAL" ]
then
   ${python_path} ${finalize} $SLURM_JOB_ID
fi
killall -9 cal_influx_feeder.sh || true
