#!/bin/bash

#-----------------------------------------------------------------------
#
#                                  PLFDFOUR
#
# Plots, by using Gnuplot, the Fourier transform of the autocorrelation
# function computed with the filter program.
#
# 12/99 MB
#
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
# Exit status:
#   0: o.k.
#   1: illegal options
#   2: file not existent or readable
#-----------------------------------------------------------------------
# set locale, so that date etc will be in uniform format.
export LC_ALL=C

#-----------------------------------------------------------------------
# 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}_$$

purpose="\nPurpose: Plots Fourier file created by the filter-diagonalisation program."
usage="Usage: plfdfour [-h] [-a xmin] [-x xmax] [-y ymax] [-p] [-P printer] file"
options='Options:\n
-h:\t\t print this help text\n
-a xmin:\t set the lower range of x to xmin\n
-x xmax:\t set the upper range of x to xmax\n
-y ymax:\t set the upper range of y to ymax\n
-p:\t\t prompt for printing the plot at default printer lpr\n
-P printer:\t specify printer (e.g. -P "| lpr -Pps2" or -P file.ps)\n
file:\t\t name of file containing the spectrum\n'

#-----------------------------------------------------------------------
# Read and check arguments
#-----------------------------------------------------------------------

while getopts ":a:x:hpP:y:" option; do
  case $option in
    h ) echo -e $purpose
        echo -e $usage
        echo -e $options
        exit 0 ;;
    a ) xmin=$OPTARG ;;
    x ) xmax=$OPTARG ;;
    y ) ymax=$OPTARG ;;
    p ) lpr="| lpr" ;;
    P ) lpr=$OPTARG
        if [ -z "${lpr##-*}" ] ; then
     	  echo -e "\nERROR: no printer specified with -P option!"
	  $0 -h
	  exit 1
	fi ;;
    \?) echo -e "\nERROR: option $OPTARG unknown!"
	$0 -h
	exit 1 ;;
  esac
done

file=${!OPTIND}
file="${file%%.ft}.ft"
if [ ! -r $file ] ; then
  echo -e "\nERROR: file $file not existent or readable!\n"
  exit 2
fi

if [ -z "$xmax" ] ; then
  xmax=$(tail -n 1 $file | awk '{print $1}')
fi

#-----------------------------------------------------------------------
# Define GNU options
#-----------------------------------------------------------------------

dir=$(pwd)
if [ -z "${dir##$HOME/*}" ] ; then  dir="~/${dir##$HOME/}" ; fi
echo -e 'set style data lines'                    >  $gnuop
echo -e 'set xlabel "Energy"'                     >> $gnuop
echo -e 'set ylabel "Intensity"'                  >> $gnuop
echo -e "set xrange [$xmin:$xmax]"                >> $gnuop
echo -e "set yrange [0:$ymax]"                    >> $gnuop
echo -e 'set title "'$dir'/'$file',   '$(date)'"' >> $gnuop
echo -e 'plot "'$file'" notitle'                  >> $gnuop

#-----------------------------------------------------------------------
# Call Gnuplot
#-----------------------------------------------------------------------

gnuplot -geometry +0+0 -persist $gnuop
if [ ! -z "$lpr" ] ; then
  echo -e 'Print the plot to "'$lpr'" (y/n, return=no)? \c'
  read print
  if [ ${print:-n} = 'y' ]  ; then
     echo "set terminal postscript" >> $gnupr
     echo 'set output "'$lpr'"'     >> $gnupr
     gnuplot $gnupr $gnuop
  fi
fi

#-----------------------------------------------------------------------
# Remove garbage and exit
#-----------------------------------------------------------------------

/bin/rm -f $gnupr $gnuop
exit 0






