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

set usage = "$0 -column NUM [-format eps|gif] [-size NUM,NUM] [-maxlen NUM] AFILE ATITLE  BFILE BTITLE  ..." 

# Input files must have records of the form 
#   ORDER ENTROPY

set ymax = "5.0"
set size = "1.00,1.00"
set format = "eps"
set column = 0
set maxlen = 20

while ( ( $#argv > 0 ) && ( "/$1" =~ /-* ) )
  if ( ( $#argv >= 2 ) && ( "/$1" == "/-column" ) ) then
    set column = "$2"; shift; shift
  else 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" == "/-maxlen" ) ) then
    set maxlen = "$2"; shift; shift
  else 
    echo "bad option"; echo "usage: ${usage}"; exit 1
  endif
end

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

if ( $column == 0 ) then
  echo 'must specify "-column NUM"'; exit 1
endif

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

if ( "/${format}" == "/eps" ) then
  set fmtline = 'postscript eps mono "TimesRoman" 24'
else if ( "/${format}" == "/gif" ) then
  set fmtline = 'gif medium size 480,360'
else
  echo "invalid plot file format" 
endif

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

set sep = "plot"
set ltype = ( 1 2 2 3 3 4 4 5 5 1 )
set ptype = ( 4 2 3 1 6 7 8 1 2 5 )
@ i = 1
while ( $#argv > 0 ) 
  if ( $#argv < 2 ) then
    echo "usage: ${usage}"; exit 1
  endif
  set file = "$1"; shift
  set title = "$1"; shift
  printf '%s \\\n  "%s" using 1:%d title "%s"' \
    "${sep}" "${file}" "${column}" "${title}" >> ${pfile}
  printf ' \\\n     with linespoints lt %d pt %d' \
    "${ltype[$i]}" "${ptype[$i]}" >> ${pfile}
  set sep = ","
  @ i = $i + 1
end
printf '\n' >> ${pfile}
printf 'quit\n' >> ${pfile}

gnuplot < ${pfile}

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

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