#!/bin/csh -f
# 
# results - print  a huge table of results for the given directories
#
# usage: 
#   results [-n] [-w] dir...
#  -w is for writable files
#  -n is to not put each directory on a new page.

if ($#argv == 0) then
	   echo "usage: results [-n] [-w] dir..."
	   exit 1
endif

set Process=`procdir`

set tmpdir=/tmp/results$$
onintr cleanup
mkdir $tmpdir
set outfile=$tmpdir/results
set data=$tmpdir/data

unset writable
unset nopage

while ("$1" =~ -*)
    if ("$1" == "-w") then
    	   set writable
	   shift
    else if ("$1" == "-n") then
    	   set nopage
	   shift
    else
	   echo "usage: results [-n] [-w] dir..."
	   exit 1
    endif
end

if ($?nopage) then
    set out=$outfile
    echo > $out
endif

if ($?writable) then
	   # writable files tests
	   foreach dir ($*)
	      if (! -d $dir) then
	   		 echo $dir not a directory
	   		 continue
	      endif

    	   	 # Select output file
    	   	 if ($?nopage) then
    	   	  	 echo "" >> $out
    	   	  	 echo $dir >> $out
    	   	 else
        	   	 set name=`echo $dir | sed 's+/+-+g'`
    	   	  	 set out=$outfile-$name
    	   	  	 echo > $out
    	   	 endif

	      # Print the common parameters
	      printparms $dir/*.data-a >> $out
	      echo > $data
	   
	      foreach file ($dir/*.data-a)
	   	   set type=`echo $file:t | sed 's/\..*//'`
	        if ($type != w) then
	   		 echo Error- type $type file found "($file)"
	   		 exit 1
	        endif
	   	   set dir=$file:h
	   	   set parms=("$dir" `echo $file:t | sed 's/\./ /g'`)
	   	   echo -n $parms[3-6] $parms[9-11] " " >> $data
	   	   cat $file >> $data
	   	   echo ""  >> $data
	      end
	      # reformat the file and print it out
	      Awk -f $Process/results-w.awk $data >> $out
	      rm -f $data
	   end
else
	   # normal prefetching tests
	   foreach dir ($*)
	      if (! -d $dir) then
	   		 echo $dir not a directory
	   		 continue
	      endif

    	   	 # Select output file
    	   	 if ($?nopage) then
    	   	  	 echo "" >> $out
    	   	  	 echo $dir >> $out
    	   	 else
        	   	 set name=`echo $dir | sed 's+/+-+g'`
    	   	  	 set out=$outfile-$name
    	   	  	 echo > $out
    	   	 endif

	      # Print the common parameters
	      printparms $dir/*.data-a >> $out
	      echo > $data
	   
	      foreach file ($dir/*.data-a)
	   	   set type=`echo $file:t | sed 's/\..*//'`
	        if ($type == w || $type == N) then
	   		 echo Error- type $type file found "($file)"
	   		 exit 1
	        endif
	   	   set dir=$file:h
	   	   set parms=("$dir" `echo $file:t | sed 's/\./ /g'`)
	   	   echo -n $parms[3-10] $parms[14-16] " " >> $data
	   	   cat $file >> $data
	   	   echo ""  >> $data
	      end
	      # reformat the file and print it out
	      Awk -f $Process/results.awk $data >> $out
	      rm -f $data
	   end
endif

(cd $tmpdir; printwide *)

cleanup:
rm -fr $tmpdir
