#!/bin/bash
#####################################################################
#                          PLUPDATE                                 #
# This shell script plots the update times using gnuplot.           #
# HDM 07/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}_$$
fl=$tmp/gnu_upd.${LOGNAME}_$$
fl2=$tmp/gnu_upd2.${LOGNAME}_$$
file="update"
dir=`pwd`
lpr="no"
sout=0
error=0
for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done
ar=0
zr=0
xr='*'
yr='*'

purp="Purpose: Plot the CMF update times or the CMF A and Phi errors."
usage="Usage: plupdate [-f -d -a -x -y -z -G -e] [-p | -P printer] [-h]."
help1='-h     : print this help text.'
help2='-f path: use "path" as input file rather than "./update".'
hlp2b='-d path: use "path" as input file for a comparison data set.'
help3='-a val : set lower range of x to val.'
help4='-x val : set upper range of x to val.'
help5='-y val : set upper range of y to val.'
help6='-z val : set lower range of y to val.'
help7='-G     : draw grid lines.'
help8='-e     : Plot the A- and Phi-errors rather than the update time.'
help9='-p     : prompt for printing the plot at default printer lpr.'
hlp10='-P printer : specify alternative printer'
hlp11='(e.g. -P "| lpr -Pps2"  or  -P file).'

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

while getopts ":hGef:d:a:x:y:z:pP:" opt; do
    case $opt in
    h  ) echo $purp
         echo $usage
         echo ' '"$help1"
         echo ' '"$help2"
         echo ' '"$hlp2b"
         echo ' '"$help3"
         echo ' '"$help4"
         echo ' '"$help5"
         echo ' '"$help6"
         echo ' '"$help7"
         echo ' '"$help8"
         echo ' '"$help9"
         echo ' '"$hlp10 $hlp11"
	 echo ' '
         exit ;;
    f  ) file=$OPTARG
	 if [ "$file" != "${file%/}" ] ; then file=$file/update ; fi ;;
    d  ) file2=$OPTARG
         if [ "$file2" != "${file2%/}" ] ; then file2=$file2/update ; fi ;;
    a  ) ar=$OPTARG ;;
    x  ) xr=$OPTARG ;;
    y  ) yr=`echo $OPTARG | tr 'd' 'e'`;;
    z  ) zr=`echo $OPTARG | tr 'd' 'e'`;;
    G  ) grid=1     ;;
    e  ) error=1
         if [ "$zr" = 0 ] ; then zr='1.e-9' ; fi ;;
    p  ) lpr="| lpr" ;;
    P  ) lpr=$OPTARG ;;
    \? ) echo ' Unkown option!'
         echo $usage
         echo ' -h provides a help text.'
         exit 1 ;;
  esac
done
shift $(($OPTIND - 1))

if [ -z "${lpr##-*}" ] ; then
  echo ' No printer specified with the -P option!'
  echo ' Example :  plstate -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plstate -P prfile  :  writes postscript-output '\
                   'to the file "prfile".'
  echo
  echo $usage
  echo ' '"$help1"
  echo ' '"$help2"
  echo ' '"$hlp2b"
  echo ' '"$help3"
  echo ' '"$help4"
  echo ' '"$help5"
  echo ' '"$help6"
  echo ' '"$help7"
  echo ' '"$help8"
  echo ' '"$help9"
  echo ' '"$hlp10 $hlp11"
  echo ' '
  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

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

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

#-----------------------------------------------------------------------
#  Write GNU options file.
#-----------------------------------------------------------------------
gpwd=`pwd`
echo 'set style data lines'      > $gnuop
if [ -n "$grid" ] ; then
    echo 'set grid'             >> $gnuop
fi
echo 'set xlabel "time[fs]"'    >> $gnuop
echo 'set xrange ['$ar':'$xr']' >> $gnuop

if [ $error -eq 0 ] ; then
   echo 'set yrange ['$zr':'$yr']' >> $gnuop
   echo 'set nologscale'           >> $gnuop
   echo 'set title "update times,   '$gpwd '"' >> $gnuop
   if [ -n "$file2" ] ; then
      echo 'plot "'"$fl"'" using 5:2 notitle ,'\
               '"'"$fl2"'" using 5:2 notitle'   >> $gnuop
   else
      echo 'plot "'"$fl"'" using 5:2 notitle'   >> $gnuop
   fi
else
   if [ -z "$zr" ] ; then  zr=1.e-9 ; fi
   echo 'set yrange ['$zr':'$yr']' >> $gnuop
   echo 'set logscale y'           >> $gnuop
   echo 'set title "CMF errors,   '$gpwd '"' >> $gnuop
   if [ -n "$file2" ] ; then
      echo 'plot "'"$fl"'" using 5:3 title "A-error",\'   >> $gnuop
      echo     ' "'"$fl"'" using 5:4 title "Phi-error",\' >> $gnuop
      echo    ' "'"$fl2"'" using 5:3 title "A-error 2",\' >> $gnuop
      echo    ' "'"$fl2"'" using 5:4 title "Phi-error 2"' >> $gnuop
   else
      echo 'plot "'"$fl"'" using 5:3 title "A-error",\' >> $gnuop
      echo     ' "'"$fl"'" using 5:4 title "Phi-error"' >> $gnuop
   fi
fi

   sed -e 's/\*/ /' -e '/^ *[0-9]*                    /d' $file  > $fl
if [ -n "$file2" ] ; then
   sed -e 's/\*/ /' -e '/^ *[0-9]*                    /d' $file2 > $fl2
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
/bin/rm -f $fl
/bin/rm -f $fl2
exit










