#!/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.gpl", 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 .gpl 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 .gpl script made by pnuglot can now be
# edited to fine-tune the plotting parameters. Run the .gpl script
# (not pnuglot!) again for the changes to take effect.


out=""
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.gpl
	else
	  if (file $arg | grep -q -i -e script -e PostScript) ; then
	    echo "ignored: $arg"
	  else
	    files="$files $arg"
	    [ -z "$out" ] && out=$arg.gpl
	  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
#!/bin/sh

trap "rm -f tmpgpl.*" 0 1 2 3 9 15

gnuplot << \_EOF_

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

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

set output "tmpgpl.pslatex"
set lmargin 10
set rmargin 4
set tmargin 0
set bmargin 2

set size 1,1.3
set key spacing 2

#set title "Cross-section"

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

set label '[l]{$\sqrt s$/GeV}' at graph 1.05,0
# looks better than set xlabel '...'

set label '[tr]{$\dfrac{\d\sigma}{\d\Omega}$/pb}' at graph -.15,.95
# 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

specs='u 1:2 t "tree", \
  "" u 1:3 t "loop"'
[ $tree = 0 ] && specs='u 1:3 t "loop"'
[ $loop = 0 ] && specs='u 1:2 t "tree"'

delim='plot \
'

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


cat << \_TEMPLATE_ >> $out


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

_EOF_

cat << \_EOF_ > tmpgpl.tex
\documentclass[11pt]{article}
\usepackage{amsmath}
\textwidth=500bp
\oddsidemargin=0bp
\evensidemargin=0bp
\parindent=0bp
\pagestyle{empty}
\def\d{\mathrm{d}}

% it's a good idea to use PostScript fonts here since the figures
% will likely be expanded or shrunk to fit the final size
\renewcommand{\rmdefault}{ppl}
\DeclareSymbolFont{operators}{OT1}{pplcm}{m}{n}
\DeclareSymbolFont{letters}{OML}{pplcm}{m}{it}
\DeclareSymbolFont{largesymbols}{OMX}{psycm}{m}{n}
\DeclareSymbolFont{bold}{OT1}{ppl}{bx}{n}
\DeclareSymbolFont{italic}{OT1}{ppl}{m}{it}
\DeclareMathAlphabet{\mathrm}{OT1}{ppl}{m}{n}
\DeclareMathAlphabet{\mathbf}{OT1}{ppl}{bx}{n}
\DeclareMathAlphabet{\mathit}{OT1}{ppl}{m}{it}

\begin{document}
_EOF_

sed /endinput/d tmpgpl.pslatex >> tmpgpl.tex

echo '\end{document}' >> tmpgpl.tex

epsfile="`basename $0 .gpl`.eps"
latex tmpgpl.tex
dvips -E -o $epsfile tmpgpl.dvi
gv -magstep 2 $epsfile &

_TEMPLATE_

chmod 755 $out
exec $out

