#!/bin/bash

# Part of the cosima cluster
# Check the MEGAlib version in the remote machine



commandhelp() {
  echo ""
  echo "dmegalib-checkmegalib - check the MEGAlib version on the remote machines";
  echo "Copyright by Andreas Zoglauer"
  echo ""
  echo "Usage: dmegalib-checkmegalib";
  echo ""
  echo "Options:"
  echo "  --help                    : Show this help"
  echo ""
  echo "";
  echo "Example: "
  echo "  dmegalib-updatemegalib";
  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


# Default options:
CFG=~/.dcosima.cfg
if [ ! -f ${CFG} ]; then
  echo "ERROR: No dmegalib configuration file present. Please run dmegalib-setup first."
  exit 1
fi


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

echo " "
echo "Checking MEGAlib on all machines..."
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 }'`
 
  # Do a quick test if the machine is available:
  REMOTENAME[$m]=`ssh -i ${HOME}/.ssh/dmegalib_rsa -q -o ConnectTimeout=5 -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} "hostname"`
  if [ "$?" != "0" ]; then
    echo " * Machine ${REMOTEHOST[$m]}, port ${REMOTEPORT[$m]}, user ${REMOTEUSER}: NOT ACCESSIBLE"
    MACHINES[$m]=""
    GITVERSION[$m]=""
    continue
  fi  

  # Check if .bash_local exists
  ssh -i ${HOME}/.ssh/dmegalib_rsa -q -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} 'if [[ -f .bash_local ]]; then exit 0; else exit 1; fi'
  if [ "$?" != "0" ]; then
    echo " * Machine ${REMOTENAME[$m]}: MEGAlib has never been installed"
    MACHINES[$m]=""
    continue
  fi 

  # Check if MEGAlib path exists
  ssh -i ${HOME}/.ssh/dmegalib_rsa -q -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} '. .bash_local; if [[ ${MEGALIB} != "" ]]; then exit 0; else exit 1; fi'
  if [ "$?" != "0" ]; then
    echo " * Machine ${REMOTENAME[$m]}: MEGAlib has never been correctly installed"
    MACHINES[$m]=""
    continue
  fi 
  
  # Check that cosima is installed
  #GITVERSION[$m]=`ssh -i ${HOME}/.ssh/dmegalib_rsa -q -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} '. .bash_local; cd ${MEGALIB}; git rev-parse HEAD'`
  GITVERSION[$m]=`ssh -i ${HOME}/.ssh/dmegalib_rsa -q -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} '. .bash_local; cd ${MEGALIB}; git log -1 --pretty=format:"%H %cd" --date=short'`
  if [ "$?" != "0" ]; then
    echo " * Machine ${REMOTENAME[$m]}: Unable to retrieve git version!"
    MACHINES[$m]=""
    continue
  fi 
      
  # Check that cosima is installed
  AVAILABLE=`ssh -i ${HOME}/.ssh/dmegalib_rsa -q -p ${REMOTEPORT[$m]} ${REMOTEUSER[$m]}@${REMOTEHOST[$m]} '. .bash_local; type cosima > /dev/null' 2>&1`
  if [ "${AVAILABLE}" != "" ]; then
    echo " * Machine ${REMOTENAME[$m]}: Cosima not found!"
    GITVERSION[$m]=""
    continue
  fi 

done
echo " "

# Check if all MEGAlib's are accessible
OUTPUT=""
for (( m=0; m<=$(( ${#MACHINES[*]} -1 )); m++ )); do
  if [[ "${MACHINES[$m]}" == "" ]]; then
    if [[ ${REMOTENAME[$m]} != "" ]]; then
      OUTPUT="${OUTPUT} * ${REMOTENAME[$m]} (${REMOTEUSER[$m]}@${REMOTEHOST[$m]}:${REMOTEPORT[$m]})\n"
    else 
      OUTPUT="${OUTPUT} * ${REMOTEUSER[$m]}@${REMOTEHOST[$m]}:${REMOTEPORT[$m]}\n"
    fi
    continue;
  fi
done
  
if [[ ${OUTPUT} != "" ]]; then
  echo "The following machines have no running MEGAlib version installed:"
  echo -e "${OUTPUT}"
  exit 1
else
  VERSIONLOG="${REMOTENAME[0]}: ${GITVERSION[0]}\n"
  INITIALGITVERSION=${GITVERSION[0]}
  IDENTICAL="yes"
  for (( m=1; m<=$(( ${#GITVERSION[*]} -1 )); m++ )); do
    VERSIONLOG+="${REMOTENAME[$m]}: ${GITVERSION[$m]}\n"
    if [[ ${INITIALGITVERSION} != ${GITVERSION[$m]} ]]; then
      IDENTICAL="no"
    fi
  done
  
  echo -e ${VERSIONLOG} | column -t -c 2 -s' '
  echo " "
  if [[ ${IDENTICAL} == "no" ]]; then
    echo "Not all machines run the same version of MEGAlib. Update MEGAlib!"
    echo " "
    exit 1
  else 
    echo "GOOD: All machines are accessible and run the same version of MEGAlib!"
    echo " "
  fi
fi
  
  
exit 0
