#!/bin/bash
#
# bb_stop_stage_out
# Terminate any file staging and completely release burst buffer resources
#
# NOTE: Sample script used for burst buffer management. Used for development
# purposes and is not intended for production use.
#
# File system formats:
# I. BB_DIRECTORY (environment variable): Defines head of burst buffer file
#    system
#    A. .slurm_state, child of BB_DIRECTORY, contains state information, file
#       format is "TotalSize=#"
#    B. Slurm job ID number, child directories of BB_DIRECTORY, one per job
#       1. .slurm_state, child of Slurm job ID directory, contains state
#          information, file format is
#          "UserID=# JobID=# Name=name State=name Size=#"
#          (NOTE: "UserID" must be first)
#          (NOTE: Either the JobID or Name should be specified, but not both)
#

# Validate execute line
valid=0
if [[ $# -ge 2 && $1 = "stop_stage_in" ]]; then
	job_id=$2
	valid=1
fi
if [[ $# -ge 2 && $1 = "stop_stage_out" ]]; then
	job_id=$2
	valid=1
fi
if [[ $valid -eq 0 ]]; then
	echo "Usage: $0 stop_stage_out|stop_stage_in <jobid> <userid> <size>"
	exit 1
fi

# Default BB_DIRECTORY for testing is $HOME/slurm_bb
if [[ -z "$BB_DIRECTORY" ]] ; then
	BB_DIRECTORY=$HOME/slurm_bb
fi

while [[ -d $BB_DIRECTORY/$job_id ]]; do
	rm -rf $BB_DIRECTORY/$job_id
	if [[ -d $BB_DIRECTORY/$job_id ]] ; then
		sleep 2
	fi
done
exit 0
