#!/bin/csh -f

set tempscript = /tmp/lfreq.awk.$user.$$
set tempinput =  /tmp/lfreq.inp.$user.$$

if ($#argv < 1) then
  cat > $tempinput
else
  set tempinput = ""
endif

foreach file ($* $tempinput)
  set counts = `cat $file | wc`
  echo \
'{for (letno=1; letno<=length($0); letno++) \
    {letter = substr($0,letno,1) ; \
     lfreq[letter]++}} \
 END {for (letter in lfreq) { \
        printf "%s	%d", letter, lfreq[letter]' > $tempscript
  @ i = 1
  while( $i <= $#counts )
    echo '        printf "	%f", lfreq[letter]/'$counts[$i] >> $tempscript
    @ i++
  end
  echo '        print ""}}' >> $tempscript
  cat $file \
    | awk -f $tempscript \
    | sort -nr +1 > $file.lf
end

if ("$tempinput" != "") cat $tempinput.lf

rm -f $tempscript $tempinput $tempinput.lf
