#! /bin/csh -f
# Last edited on 2000-05-26 15:54:58 by stolfi

# Merges two or more files produced by compute-freqs 
# WARNING: assumes the word field ($3) does not contain blanks.

set usage = "$0 file1.frq file2.frq ... > join.mfr"

set path = ( ${STOLFIHOME}/voynich/work $path )

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

/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 +2 > ${sfile}
  join \
      -a 1 -a 2 \
      -j1 3 -j2 1 -e 0 -o "${ofmt},1.1,1.2" \
      ${sfile} ${jfile} \
    > ${tfile}
  mv ${tfile} ${jfile}
  @ n = ${n} + 1
  set ofmt = "${ofmt},2.${n}"
  @ n = ${n} + 1
  set ofmt = "${ofmt},2.${n}"
  shift
end

echo "# ${files}"
cat ${jfile} \
  | sort -b +0 -1 \
  | gawk ' \
      /./ { \
        for (i=2;i<=NF;i+=2) printf " %6d %7.5f", $(i), $(i+1); \
        printf "  %s\n", $1; \
      } \
    '

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