#!/bin/bash
#
# combine test script
#
# Copyright 2002, 2013 Daniel P. Valentine; All rights reserved
#
# The first argument is the source test directory.  The assumption
# is that we start out in the right place to build files.
#
export bld=$(pwd)
# Forces the use of the newly delivered modules that we supply in the test.
export GUILE_LOAD_PATH="${bld}/../src:..." 
cd ${1}
${bld}/../src/combine -n -z 3 -r test.1 -k 1 -m 1 -u -h 30 -r test.2 -k 1 -m 2 -u -h 30 -t ${bld}/testout.txt -w test.3 2> ${bld}/stats.txt
if  [[ "$( diff -q ${bld}/stats.txt stats1.txt)" == "" ]] && [[ "$( diff -q ${bld}/testout.txt result1.txt )" == "" ]]
  then
  echo "Test 1: (General matching, reference-based output.) PASSED"
else
  echo "Test 1: (General matching, reference-based output.) FAILED"
  fi

${bld}/../src/combine -n -z 3 -o 1-3 -t ${bld}/testout.txt -w -r test.1 -k 1 -m 1 -u -h 30 -r test.2 -k 1 -m 2 -u -h 30 test.3 2> ${bld}/stats.txt
if  [[ "$( diff -q ${bld}/stats.txt stats2.txt)" == "" ]] && [[ "$( diff -q ${bld}/testout.txt result2.txt )" == "" ]]
  then
  echo "Test 2: (General matching, data-based output.) PASSED"
else
  echo "Test 2: (General matching, data-based output.) FAILED"
  fi

${bld}/../src/combine -n -z 3 -o 1-3 -t ${bld}/testout.txt -w --no-check-all-reference -r test.1 -k 1 -m 1 -u -h 30 -r test.2 -k 1 -m 2 -u -h 30 test.3 2> ${bld}/stats.txt
if  [[ "$( diff -q ${bld}/stats.txt stats3.txt)" == "" ]] && [[ "$( diff -q ${bld}/testout.txt result2.txt )" == "" ]]
  then
  echo "Test 3: (Only necessary ref checking.) PASSED"
else
  echo "Test 3: (Only necessary ref checking.) FAILED"
  fi

${bld}/../src/combine -K XXX -o 1 -w -r test.1 -k 1 -m 1 -K Y -p -o 1 test.3 > ${bld}/testout.txt 2> ${bld}/stats.txt
if  [[ "$( diff -q ${bld}/stats.txt stats4.txt)" == "" ]] && [[ "$( diff -q ${bld}/testout.txt result4.txt )" == "" ]]
  then
  echo "Test 4: (Constant strings and reference filler.) PASSED"
else
  echo "Test 4: (Constant strings and reference filler.) FAILED"
  fi

${bld}/../src/combine -f -o 1 -w -r test.4 -k 1 -m 1 -o 1 -w -t ${bld}/testout2.txt test.3 > ${bld}/testout.txt 2> ${bld}/stats.txt
if  [[ "$( diff -q ${bld}/stats.txt stats5.txt)" == "" ]] && \
    [[ "$( diff -q ${bld}/testout.txt result5a.txt )" == "" ]] && \
    [[ "$( diff -q ${bld}/testout2.txt result5b.txt )" == "" ]]
  then
  echo "Test 5: (Multiple reference matches.) PASSED"
else
  echo "Test 5: (Multiple reference matches.) FAILED"
  fi

${bld}/../src/combine -z 5 -s 4-9 -r test.5 -k 1 -m 1-3 -o 2 -w -D , test.6 > ${bld}/testout.txt 2>${bld}/stats.txt
if  [[ "$( diff -q ${bld}/stats.txt stats6.txt)" == "" ]] && \
    [[ "$( diff -q ${bld}/testout.txt result6.txt )" == "" ]]
  then
  echo "Test 6: (Delimited reference fields and sums.) PASSED"
else
  echo "Test 6: (Delimited reference fields and sums.) FAILED"
  fi

${bld}/../src/combine -X '../src/util.scm' -w -D , -d , -o '1(rpad input-field 5),2(lpad input-field 10)' test.5 > ${bld}/testout.txt 2>${bld}/stats.txt
if  [[ "$( diff -q ${bld}/stats.txt stats7.txt)" == "" ]] && \
    [[ "$( diff -q ${bld}/testout.txt result7.txt )" == "" ]]
  then
  echo "Test 7: (Extension load and field modification.) PASSED"
else
  echo "Test 7: (Extension load and field modification.) FAILED"
  fi

# Clean up a bit.
rm ${bld}/testout*.txt ${bld}/stats.txt
