#!/bin/sh

#	pnuglot
#		makes plots from data files produced by num.F
#		this file is part of FormCalc
#		last modified 26 Jul 01 th

# This program works in two steps:
#
# First, it generates a script called "file.plt", if "file" was the
# original file name. This script contains an invocation of gnuplot 
# with a lot of plotting parameters pre-set to reasonable values.
#
# It then executes the newly created .plt script to produce a rough
# version of the .eps file. This is done by letting gnuplot produce
# its output in "pslatex" format and running this output through
# LaTeX and dvips -E to produce the actual .eps figure.
#
# The draft version of the .plt script made by pnuglot can now be
# edited to fine-tune the plotting parameters. Run the .plt script
# (not pnuglot!) again for the changes to take effect.


out="./runplt.plt"
files=""
loop=1
tree=1

for arg in $* ; do
  case $arg in
  -o)
	out="next"
	;;
  -2)
	loop=0
	;;
  -3)
	tree=0
	;;
  *)
	if [ "$out" = "next" ] ; then
	  out=$arg.plt
	else
	  if (file $arg | grep -q -i -e script -e PostScript) ; then
	    echo "ignored: $arg"
	  else
	    files="$files $arg"
	    [ -z "$out" ] && out=$arg.plt
	  fi
	fi
	;;
  esac
done

if [ -z "$files" ] ; then
  echo ""
  echo "Usage: $0 [-2 -3 -o outfile] datafile(s)"
  echo "Plots datafile(s) with gnuplot. The options are:"
  echo "  -2             uses only columns 1:2 for plotting"
  echo "  -3             uses only columns 1:3 for plotting"
  echo "  -o outfile     gives the name of the output file"
  echo ""
  exit 1
fi


cat << \_TEMPLATE_ > $out

# ----- The gnuplot commands start here -----

set terminal win
# if you want black lines in different dash styles rather than
# solid lines in different colors, remove the "color solid" above

set output "STDOUT"

set size 1,1.3

#set title "Cross-section"

set format y '$%g$'
# for a log axis, use
#set logscale y
#set format y '$10^{%T}$'

# looks better than set xlabel '...'

# looks better than set ylabel '...'

set data style lines

_TEMPLATE_

if (echo "test\c"; echo 1,2,3) | grep c > /dev/null ; then
  if (echo -n test; echo 1,2,3) | grep n > /dev/null ; then
    echo_n=
    echo_c='
'
  else
    echo_n=-n
    echo_c=
  fi
else
  echo_n=
  echo_c='\c'
fi

specst='u 1:2 t "tree", '
specsl='u 1:3 t "loop"'
[ $tree = 0 ] && spect=''
[ $loop = 0 ] && specsl=''

delim='plot \
'

for file in $files ; do
  echo $echo_n "$delim  \"$file\" $specst \"$file\"  $specsl $echo_c" >> $out
  delim=', \
'
done


cat << \_TEMPLATE_ >> $out


# ----- The gnuplot commands end here -----

_TEMPLATE_

chmod 755 $out
exec wgnuplot

