#!/bin/bash
#######################################################################
#                          PLGEN                                      #
# This shell script plots general data read from file(s).             #
# HDM 02/01                                                           #
#######################################################################

#-----------------------------------------------------------------------
#  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}_$$
dir=`pwd`
lpr="no"
grid=0
logy=0
logx=0
using='1:2'
psopts=""


for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done

purp="Purpose: Plot the data of (a) general file(s). (gnuplot wrapper)."
usage='Usage: plgen [options] file_1 [using_1] [-i]'
usag1='[.... file_n [using_n] [-i]].'
help1='-h     : print this help text.'
hlp1a='-a val : set lower range of x to val.'
help2='-x val : set upper range of x to val.'
help3='-y val : set upper range of y to val.'
hlp3a='-z val : set lower range of y to val.'
hlp3b='-i     : "with impulses" (rather than lines). This option must'
hlp3c='          come after the file-name (and using).'
hlp3h='-s     : "with points" (rather than lines). (Large points).'
hlp3i='          This option must come after the file-name (and using).'
hlp3j='-d     : "with points" (rather than lines). (Small points).'
hlp3k='          This option must come after the file-name (and using).'
hlp3d='-L lab : Legend of line. This option must come after the file-name'
hlp3e='          and using and after -i (if given).'
hlp3f='-X lab : x-Label. Legend of abscissa.'
hlp3g='-Y lab : y-Label. Legend of ordinate.'
hlp3h='-T     : set "tics out".'
hlp3u='-u str : The string str is used as "using" for all files.'
help4='-l     : use logarithmic y-scale.'
hlp4a='-m     : use logarithmic x-scale.'
help5='-G     : draw grid lines.'
help6='-b     : draw zero line (base-line).'
hlp6a='-n     : no titles.'
hlp6b='-t tit : title.'
hlp6c='-o     : smooth lines'
help7='-p     : prompt for printing the plot at default printer lpr.'
help8='-P printer : specify alternative printer'
help9=' (e.g. -P "| lpr -Pps2"  or  -P file).'
hlp10='-O opt : additional options for Gnuplot PostScript output.'
hlp11='         (e.g. -O "color solid" for colored instead of dashed lines.)'
hlp12='         (or   -O "lw 2.5 font 22" for larger linewidths and fonts.)'
hlp13='         (type "help set terminal postscript" in gnuplot for more info.)'

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


while getopts ":hlmGTnboa:u:x:y:z:t:X:Y:pP:O:" opt; do
    case $opt in
    h  ) echo "$purp"
         echo "$usage $usag1"
         echo "Options:"
         echo " $help1"
         echo " $hlp1a"
         echo " $help2"
         echo " $help3"
         echo " $hlp3a"
         echo " $hlp3b"
         echo " $hlp3c"
         echo " $hlp3h"
         echo " $hlp3i"
         echo " $hlp3j"
         echo " $hlp3k"
         echo " $hlp3d"
         echo " $hlp3e"
         echo " $hlp3f"
         echo " $hlp3g"
         echo " $hlp3h"
         echo " $hlp3u"
         echo " $help4"
         echo " $hlp4a"
         echo " $help5"
         echo " $help6"
         echo " $hlp6a"
         echo " $hlp6b"
         echo " $hlp6c"
         echo " $help7"
         echo " $help8 $help9"
         echo " $hlp10"
         echo " $hlp11"
         echo " $hlp12"
         echo " $hlp13"
         echo ' '
         echo 'Remark: The using input is to be quoted if it contains () or $'
         echo '        The using input may be droped if it is 1:2 '
         echo '        The option "-u str" sets the using input to str for all files.'
         echo '        This allows constructs like: plgen -u 1:3 *.pl .'
         echo '        A "-" as filename is a shorthand for taking the'\
                       'previous file.'
         echo ' '
         echo 'Example: plgen file 1:2 file 1:4'\
                        ' (equivalent to  plgen file - 1:4)'
         echo '         plgen file1 file2 file3'
         echo "         plgen -l  auto 1:4  auto1"\
              " '1:(\$4*20)'  auto2 '1:(\$4*400)' "
         echo "         plgen spectrum.pl '1:(\$3+0.5)'  filter.eig '1:(\$2*35)' -i"
         echo "         plgen -b -z -0.02 spectrum.pl 1:3 -L MCTDH  sp -L 'semi classic'"
         echo "         plgen file 0:1   The data of column 1 will be plotted against 0,1,2,.."
         echo ' '
         exit ;;
    a  ) ar=$OPTARG ;;
    b  ) b=1        ;;
    n  ) not=notit  ;;
    x  ) xr=$OPTARG ;;
    y  ) yr=`echo $OPTARG | tr 'd' 'e'`;;
    z  ) zr=`echo $OPTARG | tr 'd' 'e'`;;
    t  ) title=$OPTARG ;;
    l  ) logy=1 ;;
    m  ) logx=1 ;;
    X  ) xlab=$OPTARG ;;
    Y  ) ylab=$OPTARG ;;
    T  ) tics=1 ;;
    u  ) using=$OPTARG ;;
    G  ) grid=1 ;;
    o  ) smooth='smooth csplines' ;;
    p  ) lpr="| lpr" ;;
    P  ) lpr=$OPTARG ;;
    O  ) psopts=$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 :  plauto -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plauto -P prfile  :  writes postscript-output '\
                   'to the file "prfile".'
  echo
  echo "$usage "
  echo " $help1"
  echo " $hlp1b"
  echo " $hlp1a"
  echo " $help2"
  echo " $help3"
  echo " $hlp3a"
  echo " $help4"
  echo " $hlp4a"
  echo " $help5"
  echo " $help6"
  echo " $hlp6a"
  echo " $hlp6b"
  echo " $hlp6c"
  echo " $help7"
  echo " $help8 $help9"
  echo " $hlp10"
  echo " $hlp11"
  echo " $hlp12"
  echo " $hlp13"
  exit 2
fi

if [ -z "$1" ] ; then
    echo "No argumets given! Try 'plgen -h'."
    exit
fi

#-----------------------------------------------------------------------
#  Write GNU options file.
#-----------------------------------------------------------------------
echo 'set style data lines'      > $gnuop
echo 'set xrange ['$ar':'$xr']' >> $gnuop
echo 'set yrange ['$zr':'$yr']' >> $gnuop

if [ $logy -eq 1 ] ; then
  echo 'set logscale y'    >> $gnuop
else
  echo 'set nologscale'    >> $gnuop
fi
if [ $logx -eq 1 ] ; then
  echo 'set logscale x'    >> $gnuop
fi
if [ -z "$not" ] ; then echo 'set title "'$dir'"' >> $gnuop ; fi
if [ -n "$b" ]   ; then echo 'set xzeroaxis'      >> $gnuop ; fi
if [ -n "$title" ] ; then echo 'set title "'"$title"'"' >> $gnuop ; fi
if [ -n "$xlab" ]  ; then echo 'set xlabel "'"$xlab"'"' >> $gnuop ; fi
if [ -n "$ylab" ]  ; then echo 'set ylabel "'"$ylab"'"' >> $gnuop ; fi

if [ $grid -eq 1 ] ; then
  echo 'set grid'          >> $gnuop
else
  echo 'set nogrid'        >> $gnuop
fi

if [ -n "$tics" ]; then
  echo "set tics out"      >> $gnuop
fi

# a1=file, a2=using, a3=new-file

not0=$not
a1=$1
if [ "$2" = "${2%%:*}" ] ; then
    a2=$using
    a3=$2
    shift 1
else
    a2=$2
    a3=$3
    shift 2
fi

if [ "$1" = "-i" ] ; then
   sty="with impulses"
   a3=$2
   shift 1
elif [ "$1" = "-s" ] ; then
   sty="with points ps 3"
   a3=$2
   shift 1
elif [ "$1" = "-d" ] ; then
   sty="with points ps 2"
   a3=$2
   shift 1
fi
if [ "$1" = "-L" ] ; then
   not='title "'"$2"'"'
   if [ "$2" = " " ] ; then  not='notit' ; fi
   a3=$3
   shift 2
fi


if [ -z "$a3" ] ; then
    echo 'plot "'$a1'" using '$a2 $smooth $not $sty    >> $gnuop
    unset a2
    unset sty
    not=$not0
else
    if [ "$a3" = "-" ] ; then a3=$a1 ; fi
    echo 'plot "'$a1'" using '$a2 $smooth $not $sty' ,\' >> $gnuop
    unset a2
    unset sty
    not=$not0
    a0=$a1
    a1=$1
    if [ "$a1" = "-" ] ; then a1=$a0 ; fi
    if [ "$2" = "${2%%:*}" ] ; then
      a2=$using
      a3=$2
        shift 1
    else
        a2=$2
        a3=$3
        shift 2
    fi
    if [ "$1" = "-i" ] ; then
       sty="with impulses"
       a3=$2
       shift 1
    elif [ "$1" = "-s" ] ; then
       sty="with points ps 3"
       a3=$2
       shift 1
    elif [ "$1" = "-d" ] ; then
       sty="with points ps 2"
       a3=$2
       shift 1
    fi
    if [ "$1" = "-L" ] ; then
       not='title "'"$2"'"'
       if [ "$2" = " " ] ; then  not='notit' ; fi
       a3=$3
       shift 2
    fi
fi

while [ -n "$a2" ] ; do
    if [ -z "$a3" ] ; then
      echo '"'$a1'" using '$a2 $smooth $not $sty >> $gnuop
      unset a2
      unset sty
      not=$not0
    else
      if [ "$a3" = "-" ] ; then a3=$a1 ; fi
      echo '"'$a1'" using '$a2 $smooth $not $sty' ,\' >> $gnuop
      unset a2
      unset sty
      not=$not0
      a0=$a1
      a1=$1
      if [ "$a1" = "-" ] ; then a1=$a0 ; fi
        if [ "$2" = "${2%%:*}" ] ; then
          a2=$using
            a3=$2
            shift 1
        else
            a2=$2
            a3=$3
            shift 2
        fi
        if [ "$1" = "-i" ] ; then
           sty="with impulses"
           a3=$2
           shift 1
        elif [ "$1" = "-s" ] ; then
           sty="with points ps 3"
           a3=$2
           shift 1
        elif [ "$1" = "-d" ] ; then
           sty="with points ps 2"
           a3=$2
           shift 1
        fi
        if [ "$1" = "-L" ] ; then
           not='title "'"$2"'"'
           if [ "$2" = " " ] ; then  not='notit' ; fi
           a3=$3
           shift 2
        fi
    fi
done


#-----------------------------------------------------------------------
#  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 '$psopts >> $gnupr
#     echo 'set terminal postscript eps solid color '$psopts >> $gnupr
     echo 'set output "'$lpr'" '            >> $gnupr
     gnuplot "$gnupr" "$gnuop"
     /bin/rm -f $gnupr
  fi
fi
/bin/rm -f $gnuop
exit

