#!/bin/bash

# run an mmapply policy across the cluster via slurm

# gather info to map mmapplypolicy to runtime configuration
# arguments passed via job env and runtime context

filesystem=${FILESYSTEM:-scratch}
policyfile=$POLICYFILE
tmpglobal=$DIR/slurm-tmp-${SLURM_JOBID}
tmpscratch=$DIR/slurm-tmp-${SLURM_JOBID}
mkdir -p $tmpglobal

nodes=`scontrol show hostnames "${SLURM_JOB_NODELIST}" | tr '\n' ',' | sed -e 's/,$//'`
cores="${SLURM_CPUS_PER_TASK}"

DATESTR=`date +'%Y-%m-%d-%H:%M:%S'`

policy=`basename $policyfile`
filetag="${policy}_slurm-${SLURM_JOBID}_${DATESTR}"

cmd="mmapplypolicy ${filesystem} -I defer \
  -P $policyfile \
  -g $tmpglobal \
  -s $tmpscratch \
  -f ${DIR}/list-${SLURM_JOBID} \
  -M FILEPATH=${filesystem} \
  -M JOBID=${SLURM_JOBID} \
  -M LIST_OUTPUT_FILE=${OUTFILE:-/tmp/gpfs-list-policy}
  -N ${nodes} -n ${cores} -m ${cores}"

# report final command in job log
echo $cmd

# run policy command
$cmd

rcode=$?

if [ $rcode -ne 0 ]
then
  echo error: mmapplypoicy failed: code $rcode
  exit $rcode
fi

# tag output file with run metadata
outfile=`ls -t $tmpglobal | head -1`
if [[ "$outfile" != "" ]]
then
   mv -n $tmpglobal/$outfile $tmpglobal/../${outfile}_$filetag
fi
rmdir $tmpglobal
