#! /bin/csh -f
# Last edited on 2008-02-04 20:49:49 by stolfi
set usage = "$0 FILE.distr... > OUTFILE.gif"

# Reads one or more files with word length distributions,
# plots them side by side labeled 0, 1, 2, etc.

set eps = "0.00001"
set title = "token length distr by gallows count"

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

set files = ( $* )

set tmp = "/tmp/$$"

set prfile = "${tmp}.pr2"
set bmfile = "${tmp}.ppm"
set gffile = "${tmp}.gif"

cat <<EOF >> ${tmp}.gnuplot
set terminal pbm color medium
set output "${bmfile}"
set xlabel "word length"
set ylabel "token count"
set title "${title}"
set xrange [0.5:12.5]
set size 1.0,1.2
EOF

set sep = "plot "
@ k = 0
foreach file ( ${files} )
  printf "${sep}"'\\\n "'"${file}"'" using 3:1 title "'"${k}"'" with linespoints' >> ${tmp}.gnuplot
  set sep = ","
  @ k = ${k} + 1
end

cat <<EOF >> ${tmp}.gnuplot

quit
EOF

gnuplot \
  -bg black \
  -pointsize 2 \
  < ${tmp}.gnuplot

if ( ( -r ${bmfile} ) && ( ! ( -z ${bmfile} ) ) ) then
  ppmtogif < ${bmfile} > ${gffile}
  cat ${gffile}
  ( xv ${gffile}; /bin/rm -f  ${gffile} ) &
endif

/bin/rm -f ${prfile} ${bmfile}
