#!/usr/bin/bash

if [ "$1" == "--help" ]; then
  echo "======================================================="
  echo ""
  echo "  slist - a script that retrieves accounting           "
  echo "  and node information for a running or completed job. "
  echo ""
  echo "  Usage: slist [job_id] [optional_sacct_args]"
  echo ""
  echo "======================================================="

  exit
fi

if [ "$1" == "" ]; then
  echo "Please supply a job id."
  exit
fi

jid=$1
shift

# basic accounting information
echo "JobID        User      Partition       NNodes NCPUS  Start               Elapsed      State      Priority   "
echo "------------ --------- --------------- ------ ------ ------------------- ------------ ---------- -----------"
sacct $@ -j $jid --format=jobid%-12,user%-9,partition%-15,nnodes%-6,ncpus%-6,start%-19,elapsed%-12,state%-10,priority%-11 -X -n

part=`sacct $@ -j $jid --format=partition%-20 -X -n | awk '{ print $1 }'`
clust=`sacct $@ -j $jid --format=cluster%-20 -X -n | awk '{ print $1 }'`

#nodelist
echo " "
nodelist=`sacct $@ -j $jid --format=nodelist -P -X -n | nodeset -e | grep -v "None assigned" | tr ' ' '\n' `
j=0
for i in $nodelist; do
  if [ "$j" == "0" ]; then
    # echo "snodes $i ${clust}/${part}"
    snodes $i ${clust}/${part}
  else
    snodes $i ${clust}/${part} | tail -n1
  fi
  j=1
done

