#! /bin/csh -f

# Merges two or more files produced by est-freqs 
# Assumes each line of each file has fields COUNT WORD
# where COUNT is a number (nteger or fraction)
# WARNING: assumes the word field ($2) does not contain blanks.

set usage = "$0 FILE1 FILE2 ... > join.cmp"

set tmp = "/tmp/$$"
set jfile = "${tmp}-join.epr"
set sfile = "${tmp}-sort.epr"
set tfile = "${tmp}-temp.epr"

/bin/touch ${jfile}
set ofmt = "0"
# ${n} is the number of fields in ${jfile}; initially just the key
@ n = 1
set noglob
set files = ()
while ($#argv > 0) 
  set files = ( ${files} $1 )
  cat $1 | sort -b +1 -2 > ${sfile}
  join -a 1 -a 2 -j1 2 -e 0 -o "${ofmt},1.1" \
    ${sfile} ${jfile} > ${tfile}
  mv ${tfile} ${jfile}
  @ n = ${n} + 1
  set ofmt = "${ofmt},2.${n}"
  shift
end

cat ${jfile} | format-multi-counts

/bin/rm -f ${jfile} ${sfile}