#!/bin/bash

nodeMin=$1
if [ $# -eq 1 ]; then
   nodeMax=$1
else
   nodeMax=$2
   if [ $# -eq 0 ]; then
     echo -e "Usage :\n  cleanscratch [NodeNumber]\n  cleanscratch [FirstNodeNumber] [LastNodeNumber]"
     echo -e "Usage :\n  or cleanscratch [FirstNodeNumber] [LastNodeNumber] [username]"
     exit 1
   fi
fi

NAME=$USER
if [ $# -eq 3 ]; then NAME=$3 ; fi

for idx in `seq ${nodeMin} 1 ${nodeMax}`
do
  idx2=`printf "%03d\n" ${idx}`
  #taille=0
  ztot=$(ssh node${idx2} "df -h | grep -E "%[[:space:]]/scratch"" | awk '{printf $2 " " $5 }')
  total=$(echo $ztot | awk '{printf $1}')
  percent=$(echo $ztot | awk '{printf $2}')
  resul=$(ssh -n node${idx2} "if [ -d /scratch/$NAME ]; then taille=\`du -sh /scratch/$NAME | cut -f1\`; else taille=-99 ; fi ;  echo \${taille} || echo 0" 2>/dev/null)
  if [ $? -ne 0 ]; then
    echo "node${idx2} -> connection error"
    echo " "
  else
    echo "node${idx2} -> $NAME scratch contains : ${resul}"
    echo "node${idx2} -> total scratch contains : ${total} and ${percent} used"
    echo " "
  fi
done

exit 0

