#!/bin/csh -f

setenv LANG C

if ($#argv != 2) then
    echo Usage: $0 'timeout matrix.txt < /dev/null >&'  $0'log &'
    echo '    (over)writes matrix_timeout.solved'
    exit $#argv
    endif

set timeout = $1
set inf = $2
set infr = $inf:r
set outf = ${infr}_${timeout}.solved

set width = `head -1 $inf | tr ' ' '\012' | wc -l `
# echo width $width
@ reps = $width - 1

if (-e $outf) /bin/rm $outf
@ me = 2
while ($me <= $width)
    @ opp = $me
    cat $inf | awk -v meN=$me -v oppN=$opp -v to=$timeout \
      'NR==1{print $1,$meN,$oppN;next;} {me=(tolower($meN)=="inf"||$meN>to)?"Inf":$meN; opp=(tolower($oppN)=="inf"||$oppN>to)?"Inf":$oppN; print $1,me,opp;}' | \
        simpleCompare.awksh | \
        awk 'NR==1{me=$1;opp=$2; next;} NF==2{print me,opp,$1;}' | \
        cat >> $outf
    @ me = $me + 1
    end

cat $outf | \
  awk '{me=$1;if(me!=oldme){print tot;tot=0;printf "%s ",me;oldme=me;}tot+=$3;ck+=$3;}END{print tot;print "cksum", ck}'
# USE sort -r -n -k2,2  to show high scorers first.
