#!/bin/bash
####################################################################
#                  PLCAP                                           #
# This shell script plots the reflection and transmission of a CAP #
# using reflex84 and gnuplot.                                      #
# HDM 11/99                                                        #
####################################################################
#set -x
#-----------------------------------------------------------------------
#  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=$tmp/ref_${LOGNAME}_$$.pl
lpr="no"
: ${sout:=0}
for option in $@; do
  if [ "$option" = "-P" ] ; then
    lpr="--"
  fi
done

if [ -z "$ver" ] ; then
    if [ -n "$MCTDH_VERSION" ] ; then
	ver="$MCTDH_VERSION"
    else
	ver=84
    fi
fi

purp="Purpose: Plot of the reflection and transmission of a CAP."
usage="Usage: plcap [ -h -s -v -y -z -G -b -d -p -P printer "
usag1="-n order  -e eta  -l length  -m mass] E1 E2."
help1='-h : print this help text.'
help2='-s : suppress messages of reflex'$ver'.'
hlp2a='-r : enables messages of reflex'$ver'. (default; toggle for -s).'
help3='-v val : specifies the program version.'
hlp3a='The default version (can be set by MCTDH_VERSION) is: '$ver
help4='-y val : set upper range of y to val. ( 0=automatic; default ).'
help5='-z val : set lower range of y to val. ( 0=automatic; default ).'
help6='-G : Grid lines are plotted.'
hlp6a='-F : Grid lines are disabled. (default; toggle for -G).'
help7="-b : Plot (refl.+transm.) for four eta's.  (eta, 2*eta, 4*eta, 8*eta)."
hlp7a='-a : Plot (refl.+transm.) for one eta.  (default; toggle for -b).'
hlp7b='-d : Square transmission. Using a DVR the CAP is suffered twice. '
hlp7c='-f : Normal transmission.  (default; toggle for -d).'
help8='-p : prompt for printing the plot at default printer lpr.'
help9='-P printer : specify alternative printer'
hlp10='(e.g. -P "| lpr -Pps2"  or  -P file).'
hlpaa='The following options define the CAP. The user is prompted'
hlpab=' for missing input.'
hlp11='-n order : CAP order.'
hlp12='-e eta : CAP strength eta. (? or 0 -> search for optimal eta).'
hlp13='-l length : CAP length (add one half of the grid spacing).'
hlp14='-m mass : particle (reduced) mass in au (if.gt.500) or amu (if.le.500).'
hl14a='-M mass : like -m, but atomic-unit enforced.'
hl14b='-u unit : Energy unit (default is eV).'
hlp15='E1  E2 : Energy range in eV.'
hlp16='Example: plcap -n 3 -m 4 -l 1.5 -e ? 0.1 10 '
hlp17='Example: plcap -n 3 -m 4 -l 1.5 -e 0.01 -b -G 0.1 10 '
hlp18='Example: plcap'

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

while getopts ":hv:FGabdfy:z:n:e:l:M:u:m:rspP:" opt; do
    case $opt in
    h  ) echo $purp
         echo $usage
         echo '               '$usag1
         echo ' '$help1
         echo ' '$help2
         echo ' '$hlp2a
         echo ' '$help3
         echo '          '$hlp3a
         echo ' '$help4
         echo ' '$help5
         echo ' '$help6
         echo ' '$hlp6a
         echo ' '$help7
         echo ' '$hlp7a
         echo ' '$hlp7b
         echo ' '$hlp7c
         echo ' '$help8
         echo ' '$help9 $hlp10
         echo ' '
         echo ' '$hlpaa $hlpab
         echo ' '$hlp11
         echo ' '$hlp12
         echo ' '$hlp13
         echo ' '$hlp14
         echo ' '$hl14a
         echo ' '$hl14b
         echo ' '$hlp15
         echo ' '
         echo ' '$hlp16
         echo ' '$hlp17
         echo ' '$hlp18
         echo
         exit ;;
    a  ) bopt=""    ;;
    b  ) bopt="-b"  ;;
    d  ) dopt="-d"  ;;
    f  ) dopt=""    ;;
    F  ) gopt=""    ;;
    G  ) gopt="-G"  ;;
    x  ) xr=$OPTARG ;;
    y  ) yr=`echo $OPTARG | tr 'd' 'e'`;;
    z  ) zr=`echo $OPTARG | tr 'd' 'e'`;;
    v  ) ver=$OPTARG ;;
    n  ) n=$OPTARG ;;
    e  ) eta=$OPTARG ;;
    l  ) length=$OPTARG ;;
    m  ) mass=$OPTARG
         unset unit ;;
    M  ) mass=$OPTARG
         unit="au"  ;;
    u  ) unite=$OPTARG ;;
    r  ) sout=0 ;;
    s  ) sout=1 ;;
    p  ) lpr="| lpr" ;;
    P  ) lpr=$OPTARG ;;
    \? ) echo ' Unkown option!'
         echo $usage
         echo '               '$usag1
         echo ' -h provides a help text.'
         if [ ! -f $file ] ; then exit 1 ; fi ;;
  esac
done
shift $(($OPTIND - 1))

if [ -z "${lpr##-*}" ] || [ "$lpr" != "${lpr#[0-9]}" ] ; then
  echo ' No printer specified with the -P option!'
  echo ' Example :  plcap -P "| lpr -Pps2"  :  prints to ps2-printer.'
  echo ' Example :  plcap -P prfile  :  writes postscript-output '\
                   'to the file "prfile".'
  echo
  echo $usage
  echo '               '$usag1
  echo ' '$help1
  echo ' '$help2
  echo ' '$hlp2a
  echo ' '$help3
  echo '          '$hlp3a
  echo ' '$help4
  echo ' '$help5
  echo ' '$help6
  echo ' '$hlp6a
  echo ' '$help7
  echo ' '$hlp7a
  echo ' '$hlp7b
  echo ' '$hlp7c
  echo ' '$help8
  echo ' '$help9 $hlp10
  echo ' '$hlp11
  echo ' '$hlp12
  echo ' '$hlp13
  echo ' '$hlp14
  echo ' '$hl14a
  echo ' '$hl14b
  echo ' '$hlp15
  echo ' '$hlp16
  echo ' '$hlp17
  echo ' '$hlp18
  exit 2
fi
if [ -n "$1" ] ; then
  e1=$1
fi
if [ -n "$2" ] ; then
  e2=$2
fi
if [ -z "$n" ] ; then
    echo ' CAP order =? '
    read n
fi
if [ -z "$eta" ] ; then
    echo ' CAP strength eta =? '
    read eta
fi
if [ -z "$length" ] ; then
    echo ' CAP length =?'
    read length
fi
if [ -z "$mass" ] ; then
    echo ' mass =? ( [amu] if m<500 else [au] ).'
    read mass
fi
if [ -z "$e1" ] ; then
    echo ' Energy E1 =? [eV].'
    read e1
fi
if [ -z "$e2" ] ; then
    echo ' Energy E2 =? [eV].'
    read e2
fi

#-----------------------------------------------------------------------
#  Ensure that the variables contain only one number.
#-----------------------------------------------------------------------
n=`echo $n | cut -d' ' -f1`
echo $eta | fgrep \? > /dev/null && eta=0
eta=`echo $eta | cut -d' ' -f1`
length=`echo $length | cut -d' ' -f1`
mass=`echo $mass | cut -d' ' -f1`
e1=`echo $e1 | cut -d' ' -f1`
e2=`echo $e2 | cut -d' ' -f1`
ma=$mass
if [ $ma != ${ma%%[eEdD]*} ] ; then ma=9000 ; fi
ma=`echo $ma | cut -d'.' -f1`

if [ -z "$unit" ] ; then
    if [ $ma -le 500 ] ; then
       unit="amu"
    else
       unit="au"
    fi
fi
unite=${unite:=eV}
export unit
export unite

#-----------------------------------------------------------------------
#  Call reflex80 and gnuplot.
#-----------------------------------------------------------------------
if [ $sout -eq 0 ] ; then
  reflex$ver -g -o $file $gopt $bopt $dopt $n $eta $length \
            $mass $unit $e1 $e2 $unite
else
  reflex$ver -g -o $file $gopt $bopt $dopt $n $eta $length \
            $mass $unit $e1 $e2 'eV'  | fgrep -2 Not
fi

if [ -n "$yr" ] || [ -n "$zr" ]; then
    : ${yr:='*'}
    : ${zr:='*'}
    if [ "$yr" = "0" ]; then yr='*'; fi
    if [ "$zr" = "0" ]; then zr='*'; fi
    echo 'set yr ['$zr':'$yr']'  > $gnuop
 else
    gnuop=
fi

gnuplot -geometry +0+0 -persist $gnuop $file  2> /dev/null

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" "$file"  2> /dev/null
     /bin/rm -f $gnupr
  fi
fi
/bin/rm -f $gnuop
/bin/rm -f $file

##  Read new options or exit

while : ; do
newopt=
echo
echo 'New Options ?    (q for quit)'
read newopt

nquit=`echo $newopt | cut -b1`
nhelp=`echo $newopt | cut -b1,2`

if [ "$nquit" = "q" ] ; then exit ; fi
if [ "$nhelp" = "-h" ] ; then
    echo ' '$help1
    echo ' '$help2
    echo ' '$hlp2a
    echo ' '$help3
    echo '          '$hlp3a
    echo ' '$help4
    echo ' '$help5
    echo ' '$help6
    echo ' '$hlp6a
    echo ' '$help7
    echo ' '$hlp7a
    echo ' '$hlp7b
    echo ' '$hlp7c
    echo ' '$help8
    echo ' '$help9 $hlp10
    echo ' '
    echo ' '$hlpaa $hlpab
    echo ' '$hlp11
    echo ' '$hlp12
    echo ' '$hlp13
    echo ' '$hlp14
    echo ' '$hlp15
    echo ' '
    echo ' '$hlp16
    echo ' '$hlp17
    echo ' '$hlp18
else

# Export the parameters and call recursively plcap.
  export n
  export eta
  export length
  export mass
  export e1
  export e2
  export bopt
  export dopt
  export gopt
  export zr
  export yr
  export ver
  export sout

  plcap $newopt
  exit
fi
done












