#! /bin/csh -f
# Last edited on 2001-11-03 02:15:41 by stolfi

set usage = "$0 [-complement] [-sampleDots] INFILE EPSNAME"

# Reads an .fcv or .cvc file and plots a graph of 
# curvature as a function of sample number.

set cmd = $0
echo "=== begin ${cmd:t} ===" 
echo "${cmd:t} $*"

set sgn = "+1"
set style = "lines lt 1"
while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) )
  if ( "x$1" == "x-complement" ) then
    set sgn = "-1"; shift
  else if ( "x$1" == "x-sampleDots" ) then
    set style = "linespoints lt 1 pt 7 pointsize 0.75"; shift
  else
    echo "usage: ${usage}"; exit 1
  endif
end

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

set infile = "$1"; shift
set epsname = "$1"; shift

set ext = "${infile:e}"
set inname = "${infile:r}"
set tmp = "/tmp/$$-${inname:t}.plt"

if ( ! -r ${inname}.lambda ) then
  echo "${inname}.lambda not found"; exit 1
endif
set lambda = `cat ${inname}.lambda`

if ( "${ext}" == "fcv" ) then
  cat ${infile} \
    | gawk \
        -v sgn="${sgn}" \
        ' /^unit = / { unit=$3; next; } \
          /^[-+ 0-9]/ { printf "%f\n", sgn * unit * $1; next; } \
        '  \
    > ${tmp}
else if ( "${ext}" == "cvc" ) then
  cat ${infile} \
    | gawk \
        -v sgn="${sgn}" \
        ' /^unit = / { unit=$3; next; } \
          /^(begin|samples|length|epsilon|delta|end)/ { next; } \
          /^[A-Za-z0]/ {  \
            let = $1;  \
            val = index("zyxwvutsrqponmlkjihgfedcba0ABCDEFGHIJKLMNOPQRSTUVWXYZ", let); \
            if ((val == 0)||(length(let) != 1)) \
              { printf "bad letter (%s)\n", let > "/dev/stderr"; } \
            printf "%2d\n", sgn * (val - 27); next; \
          } \
        '  \
    > ${tmp}
else
  echo "bad extension ${ext}"; exit 1
endif

echo "writing ${epsname}.eps"

gnuplot <<EOF
set terminal postscript eps monochrome "TimesRoman" 18
set output "${epsname}.eps"
# set size 2,1
set size 1.2,0.40
# set title "${infile}"
set nokey
set ylabel
set noxtics
set format x ""
set format y "%.3f"
set xlabel
plot \
  0 with lines lt 2, \
  "${tmp}" using 1 with ${style}
quit
EOF

/bin/rm -f ${tmp}

ghostview ${epsname}.eps

echo "=== end ${cmd:t} =========" 
