#!/bin/sh -e
# Copyright 2006-2007  Neil Williams <linux@codehelp.co.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

cd ../test

echo
echo "Starting test one of five. . "
echo

export XML_CATALOG_FILES="../test/catalog.xml"

../src/pilot-qof -x complete.xml -f select.sql -w ../test/result.xml

# 185 is the smallest size of a QSF XML file - it is the
# size of an empty book.
# Hopefully, the reliance on the ls -l output won't change!
if [ -f ../test/result.xml -a ../test/result.xml -nt ../test/complete.xml ]
then
	RESULT=`ls -l ../test/result.xml | cut -d" " -f5`
	COMPLETE=`ls -l ../test/complete.xml | cut -d" " -f5`
	if [ $RESULT -lt $COMPLETE -a $RESULT -gt 185 ]
	then
		rm ../test/result.xml
	else
		echo "FAILED to generate test results from SQL - aborting..."
		rm ../test/result.xml
		exit 1
	fi
fi

echo "test one OK. Starting test two . . "
echo
# each uses the STDOUT from pilot-qof and STDIN to xsltproc 
# as these are local files and we don't need the interim results.
../src/pilot-qof -x complete.xml --invoice-city -t 2006-01-20 -c Business | \
xsltproc -o ../test/ --nonet http://www.data-freedom.org/xsl/pilot-qof-invoice-xhtml.xsl -
if [ -f ../test/ANYTOWN_20_01_2006.html ]
then
	rm ../test/ANYTOWN_20_01_2006.html
else
	echo "FAILED to generate HTML invoice - aborting..."
	rm ../test/ANYTOWN_20_01_2006.html
	exit 1
fi

echo
echo "test two OK. Starting test three . . "
echo

mkdir ../test/tmp/
../src/pilot-qof -x address.xml -c Friends | \
xsltproc -o ../test/tmp/ http://www.data-freedom.org/xsl/pilot-qof-address-vcard1.xsl -
if [ -f ../test/tmp/Geek.vcf ] 
then
	rm -rf ../test/tmp/
else 
	echo "FAILED to generate vcards - aborting..."
	rm -rf ../test/tmp/
	exit 1
fi

echo
echo "test three OK. Starting test four . . "

# calcurse tests do *not* depend on calcurse being installed.
mkdir ../test/ctmp/
../src/pilot-qof -x todo.xml -c isbnsearch | \
 xsltproc -o ../test/ctmp/calcurse1 \
 http://www.data-freedom.org/xsl/pilot-qof-calcurse-todo.xsl - 
if [ ! -f ../test/ctmp/calcurse1 ]
then
	echo "FAILED on calcurse todo list - aborting..."
	rm -rf ../test/ctmp/
	exit 1
fi

echo
echo "test four OK. Starting test five . . "

../src/pilot-qof -x datebook.xml -c Personal -t 2006 | \
 xsltproc -o ../test/ctmp/calcurse2 \
 http://www.data-freedom.org/xsl/pilot-qof-calcurse-apts.xsl -
if [ -f ../test/ctmp/calcurse2 ]
then
	rm -rf ../test/ctmp/
else
	echo "FAILED on calcurse appointments - aborting..."
	rm -rf ../test/ctmp/
	exit 1
fi

echo
echo "test five OK. Starting test six . . "

../src/pilot-qof -s "INSERT INTO pilot_todo (date_due, todo_description, category, todo_priority, todo_complete) values ('2006-03-27T09:00:00Z', 'testing', 'Debian', 1, 0);" -w ../test/insert.xml
XML=`grep Debian ../test/insert.xml`
if [ "$XML" = "      <string type=\"category\">Debian</string>" ]
then
	rm ../test/insert.xml
else
	echo "FAILED to INSERT data and parse results - aborting..."
	rm ../test/insert.xml
	exit 1
fi

echo ""
echo "Six tests completed successfully."
echo ""

exit 0
