#!/bin/bash
########################################################################
#                          RDRLX                                       #
# This shell script is for easy reading of the Davidson rlx_info file. #
# HDM 06/03                                                            #
########################################################################
#set -x
#  Read the options (if any).
while getopts ":hrdeslgf" opt; do
    case $opt in
    h  ) echo ' '
# Help text
   echo 'Purpose: Print some of the content of the  rlx_info file in a'
   echo '         more readable form. (Davidson relaxation only).'
   echo 'Usage  : "rdrlx" or "rdrlx file" or "rdrlx dir/".'
   echo 'NB.    : "rdrlx" is equivalent to "rdrlx rlx_info".'
   echo '         "rdrlx dir/" is equivalent to "rdrlx dir/rlx_info".'
   echo 'Options: '
         echo '  -h : Print this help text.'
         echo '  -e : Print only the "total-energy" part.'
         echo '  -r : Print only the "After Relaxation" part.'
         echo '  -d : Print only the "After Davidson" part.'
         echo '  -s : Print only the "Lost SPFs" part.'
         echo '  -l : Print only the "change of Q" part.'
         echo '  -g : Print only the "Funkphi" part.'
         echo '  -f : Print only the "Funkphi2" part.'
         echo ' '
         exit ;;
    r  ) relax=1
         all=1 ;;
    d  ) dav=1
         all=1 ;;
    e  ) en=1
         all=1 ;;
    s  ) spf=1
         all=1 ;;
    l  ) lock=1
         all=1 ;;
    g  ) funk=1
         all=1 ;;
    f  ) funk2=1
         all=1 ;;
    \? ) echo ' Unkown option!'
         echo ' -h provides a help text.'
         exit 1 ;;
  esac
done
shift $(($OPTIND - 1))



# Determine file name. "rlx_info" is default.
file='rlx_info'
if [ -n "$1" ] ; then file=$1 ; fi
if [ -z ${file##*/} ] ; then file=${file}rlx_info ; fi


# Search for sorting symbols and print.

# Check for  "" lines (Lost SPFs).
if [ -n "$spf" ] || [ -z "$all" ] ; then
   lost=`egrep ' ""$' $file | cut -c 77-80`
   if [ -n "$lost" ] ; then
      echo ' '
      echo ' Lost SPFs :'\
           '  dim - Sum[square of orbital overlap matrix elements]'
      echo '    time   s    m=1     m=2     m=3     m=4     m=5'\
           '   m=5     m=7     m=8'
      fgrep ' ""' $file | cut -c 1-77
   fi
fi

# Check for '^:: ' lines. funkphi
if [ -n "$funk" ] || [ -z "$all" ] ; then
   echo ' '
   echo 'Funkphi '
   egrep -a '^:: ' $file | cut -c 5-83
fi
# Check for '^.. ' lines. funkphi2
if [ -n "$funk2" ] || [ -z "$all" ] ; then
   echo ' '
   echo 'Funkphi2 '
   egrep -a '^\.\. ' $file | cut -c 5-83
fi
# Check for  After Davidson  lines.
if [ -n "$dav" ] || [ -z "$all" ] ; then
   echo ' '
   fgrep -a 'After Davidson' $file
   fgrep -a 'After  Lanczos' $file
fi
# Check for  After Relaxation  lines.
if [ -n "$relax" ] || [ -z "$all" ] ; then
   echo ' '
   fgrep -a 'After Relaxation' $file
fi
# Check for  ;  lines (change of Q, lock).
if [ -n "$lock" ] || [ -z "$all" ] ; then
   echo ' '
   fgrep -a ' ;' $file | cut -c 1-75
fi
# Check for  %  lines (total energies).
if [ -n "$en" ] || [ -z "$all" ] ; then
   echo ' '
   fgrep -a ' %' $file | cut -c 1-75
   echo ' '
   num=`fgrep ' %' $file | cut -c 13-15 | fgrep '*' | wc -l`
   if [ $num -gt 0 ] ; then
      echo " WARNING: Davidson did not converge for "$num" diagonalisations."
      echo ' '
   fi
fi

exit

