#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# This file acts as a stepping stone to launch ecTrans on an interactive node.
# ------------------------------------------------------------------------------

# Load helpers for color printing.
source helpers.sh

# Load directory structure for installation paths.
source dirs.sh

# $SLURM_JOBID is either passed by ENV or set because user is in shell.
[ -z "$SLURM_JOB_ID" ] && fatal "Not inside SLURM session, or SLURM_JOB_ID not \
passed through ENV."

# Check if SLURM_JOB_ID is expired.
ACTIVE_JOBS=$(squeue -u $USER -t R -o "%i" -h | tr '\n' ' ')

if [[ ! " ${ACTIVE_JOBS[*]} " =~ [[:space:]]${SLURM_JOB_ID}[[:space:]] ]]; then
    fatal "\$SLURM_JOB_ID \"$SLURM_JOB_ID\" is not of active job."
fi

# Load nodelist from job_id if user is not in shell.
if [ -z "$SLURM_NODELIST" ]; then
    SLURM_NODELIST=$(squeue -u $USER -j $SLURM_JOB_ID -O Nodelist -h)
fi

info "SLURM_JOB_ID=$SLURM_JOB_ID Nodelist=$SLURM_NODELIST"

# Specify where to store results.
RESULTS="$RESULTS_DIR/interactive"
rm -rf $RESULTS
mkdir -p $RESULTS

srun --overlap --jobid=$SLURM_JOB_ID --nodelist $SLURM_NODELIST --cpu-bind=no \
    --gpus-per-node=8 --ntasks-per-node=8 --output=$RESULTS/out.%j.%t \
    --error=$RESULTS/err.%j.%t --input=none "./ectrans_lumi-g.sh"

# Output succesfull run.
success "Finished the interactive run of ecTrans."
