#! /bin/csh -f
# Last edited on 2008-02-04 20:50:17 by stolfi

set usage = "$0 [-format eps|png] [-size NUM,NUM] [-xmax NUM] [-ymax NUM] AFILE ATITLE ALT  BFILE BTITLE BLT ..." 

# Input files must have records of the form 
#   LOSZ HISZ NRECIPES
# Plots NRECIPES against (LOSZ+HISZ)/2

set size = "1.00,1.00"
set format = "eps"
set ymax = "100"
set xmax = "100"

while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) )
  if ( ( $#argv >= 2 ) && ( "/$1" == "/-format" ) ) then
    set format = "$2"; shift; shift
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-size" ) ) then
    set size = "$2"; shift; shift
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-ymax" ) ) then
    set ymax = "$2"; shift; shift;
  else if ( ( $#argv >= 2 ) && ( "/$1" == "/-xmax" ) ) then
    set xmax = "$2"; shift; shift;
  else 
    echo "bad option"; echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv < 3 ) then
  echo "usage: ${usage}"; exit 1
endif

set pfile = "/tmp/$$.gnuplot"
set ofile = "/tmp/$$.${format}"

if ( "/${format}" == "/eps" ) then
  set fmtline = 'postscript eps mono "TimesRoman" 24'
  set ltype = ( 1 2 3 4 5 6 7 8 9 )
  set ptype = ( 7 1 2 3 4 6 5 8 9 )
else if ( "/${format}" == "/png" ) then
  set fmtline = 'png medium color; set size 0.75,0.75'
  set ltype = ( 3 1 5 7 8 9 4 2 6 )
  set ptype = ( 1 2 3 4 5 6 7 8 9 )
else
  echo "invalid plot file format" 
endif

set showpng = display

cat > ${pfile} <<EOF
# set term x11
set term ${fmtline}
set output "${ofile}"
set size ${size}
set xrange [-5:(${xmax}+5)]
set yrange [0:${ymax}]
EOF

set sep = "plot"
@ i = 1
while ( $#argv > 0 ) 
  if ( $#argv < 3 ) then
    echo "usage: ${usage}"; exit 1
  endif
  set file = "$1"; shift
  set title = "$1"; shift
  set ltx = "$1"; shift
  printf '%s \\\n  "%s" using (($1+$2)/2+(%s/5.0)):3 title "%s"' \
    "${sep}" "${file}" "${i}" "${title}" >> ${pfile}
  printf ' \\\n     with histeps lt %d' \
    "${ltype[$ltx]}" >> ${pfile}
  set sep = ","
  @ i = $i + 1
end
printf '\n' >> ${pfile}
printf 'quit\n' >> ${pfile}

/usr/bin/gnuplot < ${pfile}

cat ${ofile}
if ( "/${format}" == "/eps" ) then
  ghostview ${ofile}
else if ( "/${format}" == "/png" ) then
  ${showpng} ${ofile}
else
  echo "invalid plot file format" 
endif

/bin/rm -f ${ofile} ${pfile}