#!/bin/bash
####################################################################
#                  PLENERD                                         #
# This shell script plots the enerd file using gnuplot.            #
# HDM 08/03    (This is a copy of pladwkb)                         #
####################################################################

#-----------------------------------------------------------------------
#  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

gnuop="$tmp/gnu_options.${LOGNAME}_$$"
gnupr="$tmp/gnu_print.${LOGNAME}_$$"
enerd=enerd
eshift='0'
uselog=''
ar="*"
xr="*"
yr="*"
zr="*"
lpr="no"
G=0
for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done

purp="Purpose: Plot of the content of the enerd file."
usage="Usage: plenerd [-f -e -d -t -E -l -a -x -y -z -h] [-p | -P printer]"
printhelp() {
  echo $purp
  echo $usage
  cat <<EOF
 -h      : print this help text.
 -f FILE : Use FILE rather than ./enerd as input file.
 -d FIL2 : Use FIL2 as input file for a comparison data set.
 -s FIL2 : Use FIL2 as input file for a comparison with FIL1 plotted with points.
 -e FIL3 : similar to -d. Thus at most three data sets may be shown.
 -G      : draw grid lines.
 -t      : Plot the transl. mean-filed rather than Delta(E).
 -E val  : shift energies by val (in eV) or 'auto' for automatic shift.
 -l      : use logarithmic y-scale.
 -a val  : set lower range of x to val.
 -x val  : set upper range of x to val.
 -y val  : set upper range of y to val.
 -z val  : set lower range of y to val.
 -p      : prompt for printing the plot at default printer lpr.
 -P printer : specify alternative printer (e.g. -P "| lpr -Pps2"  or  -P file).
EOF
}

#-----------------------------------------------------------------------
# Define function rdtenerd and rdeenerd and rdeshift.
#-----------------------------------------------------------------------

rdtenerd() {            # Reading TMF
   read cnt
   i=1
   ( while [ $i -le $cnt ] ; do
        read LINE
        echo $LINE
        i=$(($i+1))
     done ) \
   | awk '{ print $1 "  " $2+'"$eshift"'}' >> $gnuop
}

rdeenerd () {           # Reading Delta(E)
   read cnt
   i=1
   while [ $i -le $cnt ] ; do
      read LINE
      i=$(($i+1))
   done
   ( while read LINE ; do
        echo $LINE
     done ) \
   | awk '{ print $1+'"$eshift"' "  " $2 }' >> $gnuop
}

rdeshift () { # Read last TMF value == automatic energy shift
   read cnt
   i=2
   while [ $i -le $cnt ] ; do
      read LINE
      i=$(($i+1))
   done
   read VAL1 VAL2
   # change sign
   case $VAL2 in
     -*) eshift=${VAL2#-} ;;
     *) eshift=-${VAL2} ;;
   esac
   echo "energy shift = ${eshift} eV" >&2
}

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

while getopts ":htld:s:e:E:f:a:x:y:z:GrpP:" opt; do
    case $opt in
    h  ) printhelp
         exit ;;
    G  ) G=1 ;;
    d  ) d_enerd=$OPTARG ;;
    s  ) x_enerd=$OPTARG ;;
    e  ) e_enerd=$OPTARG ;;
    f  ) enerd=$OPTARG ;;
    t  ) tmf=1 ;;
    E  ) eshift=$OPTARG ;;
    l  ) uselog='true' ;;
    a  ) ar=$OPTARG ;;
    x  ) xr=$OPTARG ;;
    y  ) yr=`echo $OPTARG | tr 'd' 'e'`;;
    z  ) zr=`echo $OPTARG | tr 'd' 'e'`;;
    p  ) lpr="| lpr" ;;
    P  ) lpr=$OPTARG ;;
    \? ) echo " Unknown option '$OPTARG' !"
         echo $usage
         echo ' -h provides a help text.'
         exit 1 ;;
  esac
done
shift $(($OPTIND - 1))

if [ -z "${lpr##-*}" ] || [ "$lpr" != "${lpr#[0-9]}" ] ; then
  echo ' No printer specified with the -P option!'
  echo ' Example :  plenerd -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plenerd -P prfile  :  writes postscript-output '
  echo '                                   to the file "prfile".'
  echo
  echo $usage
  printhelp
  exit 2
fi


if [ ! -r $enerd ] ; then
   echo "Sorry, the file $enerd does not exist!"
   exit
fi
if [ "$eshift" = "auto" ] ; then
   rdeshift < $enerd
fi


#-----------------------------------------------------------------------
#  Write GNU options file.
#-----------------------------------------------------------------------
echo 'set nologscale'             >> $gnuop
if [ "$uselog" ]; then
  echo 'set logscale y'           >> $gnuop
fi
echo 'set xrange ['$ar':'$xr']'   >> $gnuop
echo 'set yrange ['$zr':'$yr']'   >> $gnuop
if [ "$G" = "1" ] ; then
    echo 'set grid'               >> $gnuop
fi
if [ "$tmf" = "1" ] ; then
    echo 'set xlabel "R [au]"'    >> $gnuop
    echo 'set ylabel "V [eV]"'    >> $gnuop
else
    echo 'set xlabel "E [eV]"'    >> $gnuop
    echo 'set ylabel "Delta"'     >> $gnuop
fi
if [ -z "$d_enerd" ] && [ -z "$e_enerd" ] && [ -z "$x_enerd" ] ; then
   echo 'plot "-"  using 1:2 notit with lines'  >> $gnuop

elif [ -n "$d_enerd" ] && [ -z "$e_enerd" ] && [ -z "$x_enerd" ] ; then
   echo 'plot "-"  us 1:2 tit "f" w l, "-"  us 1:2 tit "d" w l' >> $gnuop

elif [ -z "$d_enerd" ] && [ -n "$e_enerd" ] && [ -z "$x_enerd" ] ; then
   echo 'plot "-"  us 1:2 tit "f" w l, "-"  us 1:2 tit "e" w l' >> $gnuop

elif [ -n "$d_enerd" ] && [ -n "$e_enerd" ] && [ -z "$x_enerd" ] ; then
   echo 'plot "-"  us 1:2 tit "f" with lines, "-"  us 1:2 tit "d" w l, \
                             "-"  us 1:2  tit "e" with lines' >> $gnuop

elif [ -z "$d_enerd" ] && [ -z "$e_enerd" ] && [ -n "$x_enerd" ] ; then
   echo 'plot "-"  us 1:2 tit "f" with lines, \
                             "-"  us 1:2 tit "s" with points'  >> $gnuop

elif [ -z "$d_enerd" ] && [ -n "$e_enerd" ] && [ -n "$x_enerd" ] ; then
   echo 'plot "-"  us 1:2 tit "f" with lines, \
      "-"  us 1:2  tit "e" w l, "-"  us 1:2 tit "s" with points'  >> $gnuop

elif [ -n "$d_enerd" ] && [ -z "$e_enerd" ] && [ -n "$x_enerd" ] ; then
   echo 'plot "-"  us 1:2 tit "f" with lines, \
      "-"  us 1:2  tit "d" w l, "-"  us 1:2 tit "s" with points'  >> $gnuop

else
    echo 'No more than 3 files allowed (-d, -e, -s options)'
    exit
fi

if [ -z "$tmf" ] ; then
   rdeenerd < $enerd
else
   rdtenerd < $enerd
fi

if [ -n "$d_enerd" ] ; then
   if [ ! -r $d_enerd ] ; then
      echo "Sorry, the file $d_enerd does not exist!"
      exit
   fi
   echo 'e' >> $gnuop
   if [ -z "$tmf" ] ; then
      rdeenerd < $d_enerd
   else
      rdtenerd < $d_enerd
   fi
fi
if [ -n "$e_enerd" ] ; then
   if [ ! -r $e_enerd ] ; then
      echo "Sorry, the file $e_enerd does not exist!"
      exit
   fi
   echo 'e' >> $gnuop
   if [ -z "$tmf" ] ; then
      rdeenerd < $e_enerd
   else
      rdtenerd < $e_enerd
   fi
fi

if [ -n "$x_enerd" ] ; then
   if [ ! -r $x_enerd ] ; then
      echo "Sorry, the file $e_enerd does not exist!"
      exit
   fi
   echo 'e' >> $gnuop
   if [ -z "$tmf" ] ; then
      rdeenerd < $x_enerd
   else
      rdtenerd < $x_enerd
   fi
fi


#-----------------------------------------------------------------------
#  Call gnuplot.
#-----------------------------------------------------------------------

gnuplot -geometry +0+0 -persist "$gnuop"

if [ "$lpr" != "no" ] ; then
  echo ' Print the plot to "'$lpr'" ?   (y/n; return=no) '
  read print
  if [ ${print:-n} = 'y' ]  ; then
     echo 'set terminal postscript' > $gnupr
     echo 'set output "'$lpr'" '   >> $gnupr
     gnuplot "$gnupr" "$gnuop"
     /bin/rm -f $gnupr
  fi
fi
/bin/rm -f $gnuop
exit



exit

