#!/bin/bash
####################################################################
#                  PLFLUX                                          #
# This shell script plots the flux and reaction probability.       #
# 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="flux"
dir=`pwd`
lpr="no"
rr=0
ar='*'
xr='*'
yr='*'
zr='0'
for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done

purp="Purpose: Plot of the flux or the reaction probability."
usage="Usage: plflux [ -r -f -d -e -G -a -x -y -z -p ] [ -P printer ] [ -h ] "
help1='-h     : print this help text.'
help2='-r     : plots reaction probability rather than flux.'
hlp2a='-G     : draw grid lines.'
help8='-f path: use "path" as input file rather than "./flux".'
hlp8a='-d path: use "path" as input file for a comparison data set.'
hlp8b='-e path: similar to -d. Thus at most three data sets may be shown.'
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 ":hGrd:e:f:a:x:y:z:pP:" opt; do
    case $opt in
    h  ) echo $purp
         echo $usage
         echo ' '"$help1"
	 echo ' '"$help2"
	 echo ' '"$hlp2a"
	 echo ' '"$help8"
	 echo ' '"$hlp8a"
	 echo ' '"$hlp8b"
	 echo ' '"$help9"
	 echo ' '"$help3"
	 echo ' '"$help4"
	 echo ' '"$help5"
	 echo ' '"$help6"
	 echo ' '"$help7"
	 echo ' '
	 exit ;;
    r  ) rr=1 ;;
    G  ) grid=1 ;;
    f  ) file=$OPTARG
	 if [ "$file" != "${file%/}" ] ; then file=$file/flux ; fi ;;
    d  ) file2=$OPTARG
	 if [ "$file2" != "${file2%/}" ] ; then file2=$file2/flux ; fi ;;
    e  ) file3=$OPTARG
	 if [ "$file3" != "${file3%/}" ] ; then file3=$file3/flux ; fi ;;
    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##-*}" ] || [ "$lpr" != "${lpr#[0-9]}" ] ; then
  echo ' No printer specified with the -P option!'
  echo ' Example :  plflux'$ver' -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plflux'$ver' -P prfile  :  writes postscript-output '
  echo '                                   to the file "prfile".'
  echo
  echo $usage
  echo ' '$help1
  echo ' '$help2
  echo ' '$hlp2a
  echo ' '$help8
  echo ' '$hlp8a
  echo ' '$hlp8b
  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

if [ -n "$file3" ] && [ -z "$file2" ] ; then
    file2=$file3
    unset file3
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

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

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


#-----------------------------------------------------------------------
#  Write GNU options file.
#-----------------------------------------------------------------------
line=`fgrep '#%' $file | cut -c 3-22`
echo 'set style data lines' > $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 "Energy[ev]"'  >> $gnuop
echo 'set ylabel "'$ylab'"'     >> $gnuop
echo 'set title "'$line'    '$file'" '  >> $gnuop
if [ "$rr" = 1 ] ; then
    echo 'set ylabel "Prob"'     >> $gnuop
    if [ -n "$file3" ] ; then
	echo 'plot "'$file'" us 1:4 tit "Prob",' \
         '"'$file2'" us 1:4 tit "2" ,' \
         '"'$file3'" us 1:4 tit "3" '  >> $gnuop
    elif [ -n "$file2" ] ; then
	echo 'plot "'$file'" us 1:4 tit "Prob",' \
         '"'$file2'" us 1:4 tit "2" '  >> $gnuop
    else
	echo 'plot "'$file'" us 1:4 tit "Prob"'     >> $gnuop
    fi

else
    echo 'set ylabel "Flux"'     >> $gnuop
    if [ -n "$file3" ] ; then
	echo 'plot "'$file'" us 1:2 tit "Flux",' \
          ' "" us 1:3 tit "DELTA",' \
         '"'$file2'" us 1:2  tit "2" ,' \
         '"'$file3'" us 1:2  tit "3" ' >> $gnuop
    elif [ -n "$file2" ] ; then
	echo 'plot "'$file'" us 1:2 tit "Flux",' \
          ' "" us 1:3 tit "DELTA",' \
         '"'$file2'" us 1:2  tit "2" ' >> $gnuop
    else	
	echo 'plot "'$file'" us 1:2 tit "Flux",\
          "" us 1:3 tit "DELTA"' >> $gnuop
    fi
fi

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










