#!/bin/bash

#-----------------------------------------------------------------------
#  Definitions :
#-----------------------------------------------------------------------
if [ -d /tmpa ] && [ -w /tmpa ] ; then
   tmp="/tmpa"
elif [ -d /work ] && [ -w /work ] ; then
   tmp="/work"
elif [ -d /tmp ] && [ -w /tmp ] ; then
   tmp="/tmp"
elif [ -d "$HOME" ] && [ -w "$HOME" ] ; then
   tmp="$HOME"
else
   tmp="."
fi

gnu_rlx=$tmp/elist
gnu_rlx1=$tmp/elist1
opt1='-G -a 0.'
diff=0

purp="Purpose: Plot the energy of a block-relaxation run (Dav) vs time."
usage='Usage:   plbrlx [options]  no [dir]'
help0='-h     : print this help text.'
help1='-D     : Show energy difference wrt t=0.'
helpa='-E     : Show energy difference wrt last point.'
helpb='-V val : Show energy difference wrt val.'
help2='-a val : set lower range of x to val.'
help3='-x val : set upper range of x to val.'
help4='-y val : set upper range of y to val.'
help5='-z val : set lower range of y to val.'
hlp6a='-l     : use logarithmic y-scale.'
hlp6b='-m     : use logarithmic x-scale.'
hlp6c='-n     : no titles.'
hlp6d='-t tit : title.'
hlp6e='-c     : write data to screen.'
help7='-p     : prompt for printing the plot at default printer lpr.'
help8='-P printer : specify alternative printer'
help9=' (e.g. -P "| lpr -Pps2"  or  -P file).'
hlp10="no is the number of the eigenvalue (counting from 0) to be plotted."
hlp11="dir is the path to the directory which contains the rlx_info file."

#-----------------------------------------------------------------------
#  Read the options (if any).
#-----------------------------------------------------------------------

while getopts ":DEhlmncV:a:x:y:z:t:pP:" opt; do
    case $opt in
    h  ) echo "$purp"
         echo "$usage $usag1"
         echo "Options:"
         echo " $help0"
         echo " $help1"
         echo " $helpa"
         echo " $helpb"
         echo " $help2"
         echo " $help3"
         echo " $help4"
         echo " $help5"
         echo " $hlp6a"
         echo " $hlp6b"
         echo " $hlp6c"
         echo " $hlp6d"
         echo " $hlp6e"
         echo " $help7"
         echo " $help8 $help9"
         echo " $hlp10"
         echo " $hlp11"
         echo ' '
         exit ;;
    a  ) opt1="$opt1 -a $OPTARG" ;;
    x  ) opt1="$opt1 -x $OPTARG" ;;
    y  ) opt1="$opt1 -y $OPTARG" ;;
    z  ) opt1="$opt1 -z $OPTARG" ;;
    t  ) opt1="$opt1 -t $OPTARG" ;;
    l  ) opt1="$opt1 -l" ;;
    m  ) opt1="$opt1 -m" ;;
    n  ) opt1="$opt1 -n" ;;
    p  ) opt1="$opt1 -p" ;;
    P  ) opt1="$opt1 -P $OPTARG" ;;
    D  ) diff=1 ;;
    E  ) diff=2 ;;
    V  ) e0="$OPTARG" ; diff=3 ;;
    c  ) cat=1 ;;
    \? ) echo ' Unkown option!'
         echo $usage
         echo ' -h provides a help text.'
         exit 1 ;;
  esac
done
shift $(($OPTIND - 1))
no=$1
shift
dir=${1##-*} ; dir=${dir%/} ; dir=${dir:=.}
u3='-u 1:3 '
if [ -z "$no" ] ; then
   echo "One must give the number of the eigenvalue to be plotted"
   echo 'See "plbrlx -h" '
   exit
fi
rdrlx -e $dir/ | cut -c 2-9,17-20,39-58 | sed -e 's/ [0-9][0-9][0-9]/:&/g' \
   -e 's/: //g' -e /^' '*$/d -e /[a-zA-Z]/d | fgrep " $no "> $gnu_rlx

if [ "$diff" = "0" ] ; then
   if [ -n "$cat" ] ; then cat $gnu_rlx ; fi
   cd $dir
   plgen $u3$opt1 $gnu_rlx -L ' ' "-" -d -L 'Energy (improved relaxation)'
   rm -f $gnu_rlx

elif [ "$diff" = "1" ] ; then
   i=0
   while [ $i -lt 9 ] && read LINE
     do
       i=$(($i+1))
       tim=`echo $LINE | cut -d' ' -f1`
       echo "tim=$tim"
       if [ "$tim" = "0.000" ] ; then
         e0=`echo $LINE | cut -d' ' -f3`
       fi
     done < $gnu_rlx
     echo "e0=$e0"

   i=0
   while : && read LINE
     do
       i=$(($i+1))
       if [ $i -gt 1 ] ; then
          tim=`echo $LINE | cut -d' ' -f1`
          ee=`echo $LINE | cut -d' ' -f3`
          en=`echo "$ee - $e0" | bc `
          echo " $tim $en" >> $gnu_rlx1
       fi
     done < $gnu_rlx

   if [ -n "$cat" ] ; then cat $gnu_rlx1 ; fi
   cd $dir
   plgen $opt1 $gnu_rlx1 -L ' ' "-" -d -L 'Delta-Energy (impr relax)'
   rm -f $gnu_rlx
   rm -f $gnu_rlx1

elif [ "$diff" = "2" ] ; then
   i=0
   while read LINE
     do
       i=$(($i+1))
       tim=`echo $LINE | cut -d' ' -f1`
       e0=`echo $LINE | cut -d' ' -f3`
     done < $gnu_rlx
     if [ -z "$cat" ]; then echo "e0 = $e0" ; fi

   i=0
   while : && read LINE
     do
       i=$(($i+1))
       if [ $i -gt 1 ] ; then
          tim=`echo $LINE | cut -d' ' -f1`
          ee=`echo $LINE | cut -d' ' -f3`
          en=`echo "$ee - $e0" | bc `
          echo " $tim $en" >> $gnu_rlx1
       fi
     done < $gnu_rlx

   if [ -n "$cat" ] ; then cat $gnu_rlx1 ; fi
   cd $dir
   plgen $opt1 $gnu_rlx1 -L ' ' "-" -d -L 'Energy - E_min (impr relax)'
   rm -f $gnu_rlx
   rm -f $gnu_rlx1

elif [ "$diff" = "3" ] ; then
   i=0
   while : && read LINE
     do
       i=$(($i+1))
       if [ $i -gt 1 ] || [ -n "$cat" ]; then
          tim=`echo $LINE | cut -d' ' -f1`
          ee=`echo $LINE | cut -d' ' -f3`
          en=`echo "$ee - $e0" | bc `
          echo " $tim $en" >> $gnu_rlx1
       fi
     done < $gnu_rlx


   if [ -n "$cat" ] ; then cat $gnu_rlx1 ; fi
   cd $dir
   plgen $opt1 $gnu_rlx1 -L ' ' "-" -d -L "Energy - $e0 (impr relax)"
   rm -f $gnu_rlx
   rm -f $gnu_rlx1
fi
exit

