# !/bin/bash

printf "\033c"
DIR=`pwd`

NAME=$(hostname -f | cut -d '.' -f 2)

if [ $NAME = "bullx" ]; then
        NAME="ANSELM"
else
        NAME="SALOMON"
fi

printf "Preparing data for analyse ...\n"

ml --show_hidden -t av 2>&1 > /dev/null| awk '{print $1 }' | grep -v "/apps/*" > $DIR/ml_av

find /apps/easybuild/easyconfigs-it4i/ -type f -name "*.eb" | cut -d '/' -f 6,7 | sed 's/.eb//' > $DIR/eb-it4i_tmp
echo -n "" > "$DIR/results/$NAME-not_installed.txt"
echo -n "" > "$DIR/results/$NAME-warning.txt"

while read i; do a=$(echo $i | cut -d '/' -f 1); b=$(echo $i | sed -rn "s/$a\/$a[-]+(.*)/\1/p"); echo "$a/$b" ;done < <(cat $DIR/eb-it4i_tmp) > $DIR/eb-it4i

INSTALL="FALSE"
format="%-80s %30s\n"

red=$'\e[1;31m'
grn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
end=$'\e[0m'

while read x; do 
   while read y; do 
      if [ "$x" = "$y" ]; then 
         INSTALL="OK"
         break
      fi
   done < <(cat $DIR/ml_av) 

   if [ ! "$INSTALL" = "OK" ]; then
      if [ -d "/apps/all/$x" ]; then
         INSTALL="WARNING"
      fi
   fi  

   if [ "$INSTALL" = "OK" ]; then
      printf "$format" "$x" "${grn}installed${end}"
      INSTALL="FALSE"
   elif [ "$INSTALL" = "WARNING" ]; then
      printf "$format" "$x" "${yel}warning${end}"
      echo "$x" >> "$DIR/results/$NAME-warning.txt"
      INSTALL="FALSE"
   else
      printf "$format" "$x" "${red}not installed${end}"
      echo "$x" >> "$DIR/results/$NAME-not_installed.txt"
      INSTALL="FALSE"
   fi

done < <(cat $DIR/eb-it4i)

rm $DIR/eb-it4i $DIR/ml_av $DIR/eb-it4i_tmp