#!/bin/csh -f
#
# dummydriver: link driver to this to make dry runs.
#
# This tests each line of the input file to make sure that
# all data directories and pattern files exist.
# It also echos the lines to stdout.
# 
# usage (just like driver, but only filename form)
#  dummydriver [-elog] filename [startline]
# We check that startline is either a number or a filename whose
# directory must exist.
# (we also do not allow (here) -elog on the line of a test file)

if ($#argv == 0) then
    echo ERROR: No arguments
    exit
endif

if ($1 =~ -*) then
    if ($#argv == 1) then
    	   echo ERROR: First argument begins with dash, no second arg.
    	   exit 1
    endif
    set filename="$2"
    if ($#argv > 2) set startline=$3
else
    set filename="$1"
    if ($#argv > 1) set startline=$3
endif

if ($?startline) then
    echo Start line $startline
    if ($startline !~ [0-9]*) then
    	   # looks like a filename, not a number
    	   set dir=$startline:h
        if (! -d $dir) then
    	   	  echo ERROR: directory $dir does not exist for startline file
    	   endif
    endif
endif

if (! -e "$filename") then
    echo ERROR: Test file \""$filename"\" not found.
    exit 1
endif

set tmpfile=/tmp/dummy$$
onintr cleanup

# strip comments and blank lines from file, as well as -elog arguments
sed -e 's/#.*//' -e '/^$/d' -e 's/driver [ ]*-[0-9]* /driver /' \
       $filename > $tmpfile

# now pick out data directory names and pattern files
set dirs=`cat $tmpfile | cols 2 | sort | uniq`
set pats=`cat $tmpfile | cols 3 | sort | uniq`

foreach dir ($dirs)
    if (! -d $dir) then
    	   echo ERROR"($filename)": Missing data directory $dir
    endif
end

foreach pat ($pats)
    if (! -e $pat) then
    	   echo ERROR"($filename)": Missing pattern file $pat
    endif
end

cat $filename

cleanup:
rm -f $tmpfile
