#!/bin/bash
####################################################################
#                  PLWTT                                           #
# This shell script plots the CAP expectation value W_tt.          #
# Needs GNUPLOT Version 3.7 (or higher).                           #
# HDM 10/99                                                        #
####################################################################

#-----------------------------------------------------------------------
#  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}_$$
file="wtt"
dir=`pwd`
lpr="no"
rr=0
ar='*'
xr='*'
yr='*'
zr='1.e-11'
for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done

purp="Purpose: Plot of the CAP expectation value W_tt."
usage="Usage: plwtt [  -f -a -G -x -y -z -p ] [ -P printer ] [ -h ] "
help1='-h     : print this help text.'
help8='-f path: use "path" as input file rather than "./wtt".'
help2='-G     : draw grid lines.'
help9='-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.'
help6='-p     : prompt for printing the plot at default printer lpr.'
help7='-P printer : specify alternative printer (e.g. -P "| lpr -Pps2").'

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

while getopts ":hGf:a:x:y:z:pP:" opt; do
    case $opt in
    h  ) echo $purp
         echo $usage
         echo ' '"$help1"
	 echo ' '"$help8"
	 echo ' '"$help2"
	 echo ' '"$help9"
	 echo ' '"$help3"
	 echo ' '"$help4"
	 echo ' '"$help5"
	 echo ' '"$help6"
	 echo ' '"$help7"
	 echo ' '
	 exit ;;
    f  ) file=$OPTARG
	 if [ "$file" != "${file%/}" ] ; then file=$file/wtt ; fi ;;
    G  ) grid=1     ;;
    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 ' Unkown option!'
         echo ' -h provides a help text.'
	 echo $usage
	 exit 1 ;;
  esac
done

if [ -z "${lpr##-*}" ] ; then
  echo ' No printer specified with the -P option!'
  echo ' Example :  plwtt -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plwtt -P prfile  :  writes postscript-output '
  echo '                                  to the file "prfile".'
  echo
  echo $usage
  echo ' '$help1
  echo ' '$help8
  echo ' '$help9
  echo ' '$help3
  echo ' '$help4
  echo ' '$help5
  echo ' '$help6
  echo ' '$help7
  exit 2
fi

if [ -n "${file%%/*}" ] ; then
    file=$dir/$file
fi
cd `dirname $file`
file="`pwd`/`basename $file`"

if [ ! -r "$file" ] || [ ! -f "$file" ] ; then
  echo ' The file "'$file'" does not exist or is not a readable regular file!'
  exit 2
fi

#-----------------------------------------------------------------------
#  Write GNU options file.
#-----------------------------------------------------------------------
echo 'set style data lines' > $gnuop
echo 'set logscale y'      >> $gnuop
if [ -n "$grid" ] ; then
    echo 'set grid'        >> $gnuop
fi
echo 'set xrange ['$ar':'$xr']' >> $gnuop
echo 'set yrange ['$zr':'$yr']' >> $gnuop
echo 'set xlabel "Time[fs]"'    >> $gnuop
echo 'set ylabel "W_tt"'        >> $gnuop
echo 'set title "'$file'" '     >> $gnuop
echo 'plot "'$file'" us 1:2 tit "W_tt", "" us 1:4 tit "W_tt/norm^2"' >> $gnuop

#-----------------------------------------------------------------------
#  Call gnuplot command and write print command lines.
#-----------------------------------------------------------------------

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

