#!/bin/bash

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

for idx in `seq ${nodeMin} 1 ${nodeMax}`
do
  idx2=`printf "%03d\n" ${idx}`

  resul=`ssh -n node${idx2} 'taille=\`du -sh /scratch/$USER | cut -f1\`; rm -r /scratch/$USER/* 2>/dev/null && echo \${taille} || echo 0' 2>/dev/null`
  if [ $? -ne 0 ]; then
    echo "node${idx2} -> connection error"
  else
    if [ "${resul}" == "0" ] || [ "${resul}" == "" ]; then 
      echo "node${idx2} -> no scratch to be cleaned"
    else
      echo "node${idx2} -> scratch cleaned (${resul})"
    fi
  fi
done

exit 0

