#! /bin/bash
#
# Sample Run file for Assignment 1.
#
# Copyright 2013 Systems Deployment, LLC
# Author: Morris Bernstein (morris@systems-deployment.com)

# Uncomment for debugging:
# set -x

# Shell variables to make modification easier.
BUILDDIR=build
OUTDIR=out
DATADIR=data
#DATASUFFIX=.data
DATASUFFIX=.txt
PROGRAM=wordcount

#SOURCES="*.cc"
SOURCES="allocator.cc list.cc list_main.cc"

# Clean up from previous run (if any).
rm -rf $BUILDDIR $OUTDIR
rm -f $PROGRAM

# Create directories the files generated by the compiler and for the
# output generated by the wordcount.
mkdir -p  $BUILDDIR $OUTDIR

# Build the program.  Since it's trivial, we don't need the overhead
# of maintaining a separate makefile.
# Note the flags will put the program and the temporaries into the
# build subdirectory.  Check to make sure something was built before
# continuing.
g++ -g -o $BUILDDIR/$PROGRAM $SOURCES
#g++ -v -save-temps=obj -g -o $BUILDDIR/$PROGRAM $SOURCES
if  [ ! -x $BUILDDIR/$PROGRAM ] ; then
    echo "Build failed"
    exit 1
fi
mv $BUILDDIR/$PROGRAM .

# Run program on each data file.
DATA=$DATADIR/*$DATASUFFIX
for data in $DATA; do
    # Construct out and err variables by subsituting the extension
    # and adding a directory prefix.  e.g. data/foo.data becomes
    # out/foo.out and out/foo.err
    out=$OUTDIR/`basename $data $DATASUFFIX`.out
    err=$OUTDIR/`basename $data $DATASUFFIX`.err
    ./$PROGRAM < $data > $out 2> $err
    if [ "$?" != "0" ] ; then
	echo "Test run on $data failed"
	exit 1
    fi
    # Uncomment the following if you have baseline files to compare
    # output:
    # baseline=baseline/`basename $data`.baseline
    # diff $baseline $out
    # if [ "$?" != "0" ] ; then
    #     echo "Verification failed on $data"
    #     exit 1
    # fi
done
