#!/bin/bash -e

  export COMPSS_PYTHON_VERSION=3.12.1
  # module load COMPSs/3.4.post2607
  module use /apps/GPP/modulefiles/applications/COMPSs/.custom
  module load TrunkRSP
  module load BackTrackBB

  export PATH=/home/bsc19/bsc019057/.local/bin/:/apps/COMPSs/3.4.post2606/Runtime/scripts/utils:$PATH
  #module load gcc/7.2.0 graphviz/2.40.1
  #export PYTHONPATH=/home/bsc19/bsc19234/.local/lib/python3.6/site-packages:$PYTHONPATH

  # Retrieve script arguments
  job_dependency=${1:-None}
  num_nodes=${2:-5}
  execution_time=${3:-20}
  tracing=${4:-false}
  exec_file="$(pwd)/../../backtrackbb/scripts/btbb_continuous.py"

  export COMPUTING_UNITS="56"

  # Define script variables
  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  WORK_DIR=${SCRIPT_DIR}/
  APP_CLASSPATH=${SCRIPT_DIR}/../../
  APP_PYTHONPATH=${SCRIPT_DIR}/../../

  # Define application variables
  graph=$tracing
  log_level="off"
  qos_flag="--qos=gp_bsccs"
  project_name="bsc19"
  workers_flag=""
  constraints=""

  # Create workers sandbox
  # mkdir -p "${WORK_DIR}/COMPSs_Sandbox"
  # --master_working_dir="${WORK_DIR}" \
  # --worker_working_dir="${WORK_DIR}/COMPSs_Sandbox" \

  CPUS_PER_NODE=112
  WORKER_IN_MASTER=112

  if [ $# -eq 0 ]
  then
    echo "No arguments passed. Using default:"
    echo "Job dependency : ${job_dependency}"
    echo "Number of nodes: ${num_nodes}"
    echo "Execution time : ${execution_time}"
    echo "Tracing        : ${tracing}"
    echo "File to execute: ${exec_file}"
  else
    shift 4
  fi

  # Those are evaluated at submit time, not at start time...
  COMPSS_VERSION=`module load whatis COMPSs 2>&1 >/dev/null | awk '{print $1 ; exit}'`

  # Enqueue job
  enqueue_compss \
    --job_name=PROV_BackTrackBB_PyCOMPSs_continuous \
    --job_dependency="${job_dependency}" \
    --project_name="${project_name}" \
    --exec_time="${execution_time}" \
    --num_nodes="${num_nodes}" \
    -g --keep_workingdir \
    --cpus_per_node="${CPUS_PER_NODE}" \
    --worker_in_master_cpus="${WORKER_IN_MASTER}" \
    \
    "${workers_flag}" \
    \
    --job_execution_dir="${WORK_DIR}" \
    --worker_working_dir="${WORK_DIR}" \
    \
    --log_dir="${WORK_DIR}" \
    \
    --tracing="${tracing}" \
    --summary \
    --log_level="${log_level}" \
    "${qos_flag}" \
    \
    --constraints=${constraints} \
    --provenance=BackTrackBB.yaml \
    --provenance_folder=BACKTRACKBB_NO_RUNTIMELOGS \
    --zip_provenance \
    \
    --classpath=${APP_CLASSPATH}:${CLASSPATH} \
    --pythonpath=${APP_PYTHONPATH}:${PYTHONPATH} \
    \
    --lang=python \
    \
    "$exec_file" $@

#--constraints=${constraints} \
#--provenance \


# Enqueue tests example:
# mkdir -p /gpfs/scratch/bsc19/bsc019234/BackTrackBB/backtrackbb-master/scripts/cluster/out_synthetic_continous_np/2222/01
# ./3_launch_btbb_pycompss_synthetic_continous_provenance.sh None 5 20 false \
#    synthetic \
#    /gpfs/scratch/bsc44/MN4/bsc44/bsc44755/scratch_pr1ejg00_pr1ejg19/pr1ejg19/data/2222/01 \
#    /gpfs/scratch/bsc19/bsc019234/BackTrackBB/backtrackbb-master/scripts/cluster/out_synthetic_continous_np/2222/01 \
#    /gpfs/scratch/bsc19/bsc019234/BackTrackBB/backtrackbb-master/scripts/examples/mn5/Synthetic/BT_SyntheticExample_MN_base.conf \
#    2222 \
#    01 \
#    1 \
#    2 \
#    0 \
#    12 \
#    100


# OUTPUTS:
# - compss-XX.out : Job output file
# - compss-XX.err : Job error file
# - ~/.COMPSs/JOB_ID/ : COMPSs files
