#! /bin/sh

# converts a SMART table to a matrix
# copyright Hans Paijmans 1995
# ========================================
# 13 dec. 1995 change: option -l added 
# ========================================

naam="$1"
laatste=0

if [ -z $naam ] ; then 
   naam="-h"; 
fi

if [ $naam = '-h' ] ; then
   echo "Usage : matrix [-h | -l] | filename"
   echo "        filename : indexfile (possibly by SMART) in the format"
   echo "                   filename weight concept"
   echo "        -h       : this message"
   echo "        -l       : put concept last in stead of first"
   echo "Output: Crosstab of index-file in the format"
   echo "        concept weight_file_1 weight_file_2 ... weight_file_n"
   echo "Copyright Hans Paijmans 1995"
   exit 1
fi

if [ $naam = '-l' ] ; then 
   naam=$2;
   laatste=1;
fi

awk '
   BEGIN {
       files=1}

{
  if ((length($0)>3) && (substr($0,1,1)!="#"))
     {
     if (vorige!=$1) {filenaam[$1]=files++;vorige=$1}
         woord[$3]=woord[$3] ";" $1 ";" $2
     }

}

END { 

    for (concept in woord)
          {
           if (l!=1) printf concept "\t"
           for (n=0;n<files;n++) fiel[n]=0;
           aantal=split(woord[concept],con,";");

           for (n=2;n<=aantal;n=n+2) 
               {
               for (k in filenaam)
                    if (k == con[n]) fiel[filenaam[k]]=con[n+1]; 
               }
           for (n=1;n<files;n++) printf fiel[n] " ";
           if (l==1) printf concept "\t"
           printf ("\n");
          }
    }
' l=$laatste $naam


