#!/usr/bin/env bash

# ========================================
# This file must be sourced, NOT executed!
# ========================================

JOB_NAME="interactive_node"
CONF_FILE="$HOME/.internode_vars"

# Check if there is a config file with a JOB_ID that is still active.
JOB_ID=$(grep SLURM_JOB_ID $CONF_FILE)

if [[ -z $JOB_ID ]]; then
    echo "FAILED: 'JOB_ID' not found in config file.."
    exit
fi

# If there is a job, extract the ID.
JOB_ID=$(cut -d "=" -f2 <<< "$JOB_ID")
SLURM_CHECK=$(squeue --me -j "$JOB_ID" 2>&1 >/dev/null)

echo "JOB_ID DETECTED: $JOB_ID"

if [[ ! -z "$SLURM_CHECK" ]]; then
    echo "FAILED: Old stored JOB has finished, clearing config file.."
    rm $CONF_FILE
else
    echo "SUCCESS: Loading config file variables.."
    export $(grep -v '^#' $CONF_FILE | xargs -d '\n')
    srun --cpu_bind=none --nodes=1 --pty bash -i
fi

