#! /bin/csh -f
# Last edited on 2012-05-03 20:25:35 by stolfilocal

set usage = "$0 [-noylabels] CONST  AFILE ATITLE  BFILE BTITLE  ..." 

# The CONST is the constant for the ideal Zipf plot.
# Input files must have records of the form 
#   LENGTH COUNT FREQ ...
# Uses the "FREQ" column to plot.

set ylabelfmt = "%g";
while ( ( $#argv > 0 ) && ( "/$1" =~ /-.* ) )
  if ( ( $#argv >= 1 ) && ( "/$1" == "/-noylabels" ) ) then
    set ylabelfmt = ""; shift
  else 
    echo "bad option"; echo "usage: ${usage}"; exit 1
  endif
end

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

set const = "$1"; shift

set pfile = "/tmp/$$.gnuplot"
set ofile = "/tmp/$$.eps"

cat > ${pfile} <<EOF
# set term x11
set term postscript eps mono "TimesRoman" 24
set output "${ofile}"
set size ratio -1 1.20,1.50
set logscale xy
set xrange [0.8:]
set yrange [0.00002:0.5]
set format y "${ylabelfmt}"
const = ${const}
EOF

printf 'plot (x<0.9 ? 0/0 : (x>5000 ? 0/0 : const/x)) notitle with lines linetype 3' >> ${pfile}

set sep = ","
set ltype = ( 1 2 2 3 3 4 4 5 5 )
set ptype = ( 4 2 3 1 6 7 8 1 2 )
@ 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 ($0+1):2 title "%s"' \
    "${sep}" "${file}" "${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}
ghostview ${ofile}
/bin/rm -f ${ofile} ${pfile}