#! /bin/csh -f

set usage = "$0 [ -lctx M ] [ -rctx N ] [ -colw N ] PAT1 PAT2 ... < INFILE > TABLEFILE"

set lctx = 0
set rctx = 0
set colw = 30

while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) ) 
  if ( ( $#argv >= 2 ) && ( "x$1" == "x-lctx" ) ) then
    set lctx = "$2"; shift; shift;
  else if ( ( $#argv >= 2 ) && ( "x$1" == "x-rctx" ) ) then
    set rctx = "$2"; shift; shift;
  else if ( ( $#argv >= 2 ) && ( "x$1" == "x-colw" ) ) then
    set colw = "$2"; shift; shift;
  else 
    echo "usage: ${usage}"; exit 1
  endif
end

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

set tmp = "/tmp/$$"
@ i = 0
set tfiles = ( )
set totw = 0

cat > ${tmp}.wds

while ( $#argv > 0 )
  @ i = ${i} + 1
  @ totw = ${totw} + ${colw} + 2
  set pat = "$1"; shift
  cat ${tmp}.wds \
        | enum-contexts -vPAT="${pat}" -vLCTX="${lctx}" -vRCTX="${rctx}" \
        | wfreq \
        > ${tmp}.${i}
  set tfiles = ( ${tfiles} ${tmp}.${i} )
end

pr -m -s'  ' -t -i' '1 -w ${totw} ${tfiles} \
  | expand
