#!/bin/bash
#####################################################################
#                          PLSPEED                                  #
# This shell script plots the CPU time spend between two outputs.   #
# 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}_$$
file="speed"
dir=`pwd`
lpr="no"
sout=0
error=0
ar=0
tim=1
ut='[s]'
for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done

purp="Purpose: Plot the CPU time used between two outputs (out1)."
usage="Usage: plspeed [-f -a -x -y -z -G -m -s] [-p | -P printer] [-h]."
help1='-h     : print this help text.'
hlp1a='-f path: use "path" as input file rather than "./speed".'
hlp1b='-d path: use "path" as input file for a comparison data set.'
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.'
help6='-G     : draw grid lines.'
help7='-m     : show CPU time in minutes (rather than seconds).'
help8='-s     : show CPU time in hours (Stunden).'
help9='-p     : prompt for printing the plot at default printer lpr.'
hlp9a='-P printer : specify alternative printer'
hlp9b='(e.g. -P "| lpr -Pps2"  or  -P file).'

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

while getopts ":hmsGd:f:a:x:y:z:pP:" opt; do
    case $opt in
    h  ) echo $purp
         echo $usage
         echo ' '"$help1"
         echo ' '"$hlp1a"
         echo ' '"$hlp1b"
         echo ' '"$help2"
         echo ' '"$help3"
         echo ' '"$help4"
         echo ' '"$help5"
         echo ' '"$help6"
         echo ' '"$help7"
         echo ' '"$help8"
         echo ' '"$help9"
         echo ' '"$hlp9a $hlp9b"
	 echo ' '
         exit ;;
    f  ) file=$OPTARG
	 if [ "$file" != "${file%/}" ] ; then file=$file/speed ; fi ;;
    d  ) file2=$OPTARG
         if [ "$file2" != "${file2%/}" ] ; then file2=$file2/speed ; fi ;;
    a  ) ar=$OPTARG ;;
    x  ) xr=$OPTARG ;;
    y  ) yr=`echo $OPTARG | tr 'd' 'e'`;;
    z  ) zr=`echo $OPTARG | tr 'd' 'e'`;;
    G  ) grid=1     ;;
    m  ) tim=60
         ut='[min]' ;;
    s  ) tim=3600
         ut='[hour]' ;;
    p  ) lpr="| lpr" ;;
    P  ) lpr=$OPTARG ;;
    \? ) echo ' Unkown option!'
         echo $usage
         echo ' -h provides a help text.'
         exit 1 ;;
  esac
done

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 ' '$hlp1a
  echo ' '$hlp1b
  echo ' '$help2
  echo ' '$help3
  echo ' '$help4
  echo ' '$help5
  echo ' '$help6
  echo ' '$help7
  echo ' '$help8
  echo ' '$help9
  echo ' '$hlp9a $hlp9b
  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
echo 'set xlabel "time[fs]"'    >> $gnuop
echo 'set ylabel "'$ut'"'       >> $gnuop
echo 'set nologscale'           >> $gnuop
echo 'set xrange ['$ar':'$xr']' >> $gnuop
echo 'set yrange ['$zr':'$yr']' >> $gnuop
if [ -n "$grid" ] ; then
    echo 'set grid'             >> $gnuop
fi
echo 'set title "Speed (delta-CPU),   '$gpwd '"' >> $gnuop
if [ -n "$file2" ] ; then
    echo 'plot "'$file'" us 1:($3/'$tim') tit "1" ,'\
         ' "'$file2'" us 1:($3/'$tim') tit "2" ' >> $gnuop
else
    echo 'plot "'$file'" using 1:($3/'$tim') notitle'  >> $gnuop
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











