#!/bin/bash

# Part of the cosima cluster
# Show how much disk space is still available 

# Return:
# Positive: number of available simulation slots


commandhelp() {
  echo ""
  echo "dmegalib-showdiskspace - check how much disk space is still available";
  echo "Copyright by Andreas Zoglauer"
  echo ""
  echo "Usage: dmegalib-showdiskspace"
  echo ""
  echo "Options:"
  echo "  --help: Show this help."
  echo ""
  echo "";
  echo "Example: "
  echo "  dmegalib-showdiskspace";
  echo "";
}


# Store command line as array
CMD=( "$@" )

# Check for help
for C in "${CMD[@]}"; do
  if [[ ${C} == *-h* ]]; then
    echo ""
    commandhelp
    exit 0
  fi
done

CFG=~/.dcosima.cfg

# Read configuration file and extract machines
MACHINES=( `cat ${CFG} | grep "^machine" | gawk '{ print $2 }'` )
REMOTEUSER=( )
REMOTEHOST=( )
REMOTEPORT=( )

#echo " "
#echo "Remote machines setup:"
for (( m=0; m<=$(( ${#MACHINES[*]} -1 )); m++ )); do
  REMOTEUSER[$m]=`echo "${MACHINES[$m]}" | awk -F"@" '{ print $1 }'` 
  REMOTEHOST[$m]=`echo "${MACHINES[$m]}" | awk -F"@" '{ print $2 }'  | awk -F":" '{ print $1 }'` 
  REMOTEPORT[$m]=`echo "${MACHINES[$m]}" | awk -F":" '{ print $2 }'`
  #echo " * Found remote machine: ${MACHINES[$m]} (user: ${REMOTEUSER[$m]}, address: ${REMOTEHOST[$m]}, port: ${REMOTEPORT[$m]})"
done

echo " "
echo " "
echo "Available disk space:"
ALLALLOWED="0"
ALLPOSSIBLE="0"
for (( m=0; m<=$(( ${#MACHINES[*]} -1 )); m++ )); do

  echo " "
  echo " * Machine ${REMOTEHOST[$m]}, port ${REMOTEPORT[$m]}, user ${REMOTEUSER[$m]}:"
  
  # Do a quick test if the machine is available:
  REMOTENAME=`ssh -i ${HOME}/.ssh/dmegalib_rsa -q -o ConnectTimeout=5 -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} "hostname"`
  if [ "$?" != "0" ]; then
    echo "   - NOT ACCESSIBLE"
    continue
  fi  

  # Check if we have a configuration file
  CONFIGFILE=`ssh -i ${HOME}/.ssh/dmegalib_rsa -q -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} "if [[ ! -f ~/.dcosima.cfg ]]; then echo \"NOT FOUND\"; fi; echo \"FOUND\";"`
  if [ "${CONFIGFILE}" == "NOT FOUND" ]; then
    echo "   - NO dcosima configuration file found"
    echo " "
    continue
  fi

  # Checking for remote global directory...
  REMOTECOMMAND='COUNT=`grep -c ^directory ~/.dcosima.cfg`; if [ ${COUNT} -eq 1 ]; then grep ^directory ~/.dcosima.cfg; fi;'
  MASTERDIR=$(ssh -i ${HOME}/.ssh/dmegalib_rsa -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} 'bash -s' <<< ${REMOTECOMMAND} )
  if [ "$?" != "0" ]; then
    echo "   - NO directory to store the simulated data is given in the .dcosima.cfg file"
    continue
  fi
  MASTERDIR=`echo ${MASTERDIR} | awk '{ print $2 }'`
  if [ "${MASTERDIR}" == "" ]; then
    echo "   - Cannot read the simulation directory in the .dcosima.cfg file"
    continue
  fi

  # Create the directory if it does not yet exist...
  ssh -i ${HOME}/.ssh/dmegalib_rsa -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} "if [[ ! -d ${MASTERDIR} ]]; then mkdir ${MASTERDIR}; fi"
  if [ "$?" != "0" ]; then
    echo "   - Unable to create the simulation directory"
    echo " "
    continue
  fi
  
  # Check the disk space
  FREEDISKSPACE=`ssh -i ${HOME}/.ssh/dmegalib_rsa -q -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} "cd ${MASTERDIR}; df -k . -P -T -BG | tail -n 1"`
  if [ "$?" != "0" ]; then
    echo "   - Unable to determine free disk space!"
    echo " "
    continue
  fi
  FREEDISKSPACE=$(echo ${FREEDISKSPACE}  | awk '{ print $5 }' | sed 's/.$//' )
  echo "   - Free disk space: ${FREEDISKSPACE} GByte"
  
  # Get the directories & the disk space used
  REMOTECOMMAND='DIR=$(grep ^directory ~/.dcosima.cfg | tr -s " " | cut -d " " -f2); if [[ ! -d ${DIR} ]]; then exit; fi; cd ${DIR}; for i in $(find . -maxdepth 1 -mindepth 1 -type d); do cd ${i}; DU=$(du -h -s); DU=${DU:0:${#DU}-1}; NEWESTFILE=0; for F in `find . -name "*" -type f`; do DATE=$(date -r ${F} +%s); if [[ ${DATE} -gt ${NEWESTFILE} ]]; then NEWESTFILE=${DATE}; fi; done; LATEST=$(date -d @${NEWESTFILE} +"%Y-%m-%d\xC2\xA0%H:%M"); echo "${i%%/}: last\xC2\xA0access:\xC2\xA0${LATEST}  disk\xC2\xA0space:\xC2\xA0${DU}\n"; cd ..; done;'
  SPACEANDDIR=$(ssh -i ${HOME}/.ssh/dmegalib_rsa -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} 'bash -s' <<< ${REMOTECOMMAND} )
  if [ "$?" != "0" ]; then
    echo "   - Unable to retrieve the directories!"
    echo " "
    continue
  fi
  
  if [[ ${SPACEANDDIR} != "" ]]; then 
    echo "   - Run directories:"
    OLDIFS=${IFS}; 
    IFS=$'\n'; 
    LINES=( $(echo -e ${SPACEANDDIR} | column -t -c 2) ); 
    IFS=${OLDIFS}; 
    for L in "${LINES[@]}"; do 
      echo -e "     ${L}"; 
    done
  else
    echo "   - No run directories found"  
  fi
done


echo " "

exit 0
