#!/bin/bash

#   eLyXer -- convert LyX source files to HTML output.
#
#   Copyright (C) 2009 Alex Fernández
#
#   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/>.

# Alex 20090319: run all tests

# first from the current directory
echo "Testing eLyXer -- any text below this line signals an error"
for file in test/*.lyx; do
	name=$(dirname "$file")/$(basename "$file" .lyx)
	./elyxer.py --quiet --css ../docs/lyx.css "$name.lyx" "$name-test.html"
	diff -u --ignore-matching-lines="create-date" "$name-good.html" "$name-test.html"
done

# now a limited subset of tests from within the directory
cd test
name="with images-1-5"
../elyxer.py --quiet --css ../docs/lyx.css "$name.lyx" "$name-test.html"
diff -u --ignore-matching-lines="create-date" "$name-good.html" "$name-test.html"
../elyxer.py --html --quiet --css ../docs/lyx.css "$name.lyx" "$name-html-test.html"
diff -u --ignore-matching-lines="create-date" "$name-html-good.html" "$name-html-test.html"
../elyxer.py --quiet --css ../docs/lyx.css --forceformat ".jpg" "$name.lyx" "$name-jpg-test.html"
diff -u --ignore-matching-lines="create-date" "$name-jpg-good.html" "$name-jpg-test.html"

# directory tests
cd subdir
name="image-directory"
image="mourning.png"
rm -f $image
../../elyxer.py --directory .. --quiet --css ../../docs/lyx.css "$name.lyx" "$name-test.html"
diff -u --ignore-matching-lines="create-date" "$name-good.html" "$name-test.html"
if [ ! -e $image ]; then echo "$image is missing; bad conversion."; fi
name="appendix-1-6"
cp -f ../$name.lyx .
../../elyxer.py --copyright --directory .. --quiet --css ../../docs/lyx.css "$name.lyx" "$name-test.html"
diff -u --ignore-matching-lines="create-date" "$name-good.html" "$name-test.html"
cd ..

# test TOC generation
name="appendix-1-6"
../elyxer.py --quiet --toc --toctarget "$name-test.html" --css ../docs/toc.css --target contents "$name.lyx" "$name-toc-test.html"
diff -u --ignore-matching-lines="create-date" "$name-toc-good.html" "$name-toc-test.html"

# test raw generation
name="helloworld"
../elyxer.py --quiet --raw "$name.lyx" "$name-raw-test.html"
diff -u --ignore-matching-lines="create-date" "$name-raw-good.html" "$name-raw-test.html"

# test lowmem generation
name="index-1-6"
../elyxer.py --quiet --lowmem --css ../docs/lyx.css "$name.lyx" "$name-lowmem-test.html"
diff -u --ignore-matching-lines="create-date" "$name-lowmem-good.html" "$name-lowmem-test.html"

# test Python 2.4 generation
name="index-1-6"
python2.4 ../elyxer.py --quiet --css ../docs/lyx.css "$name.lyx" "$name-py2.4-test.html"
diff -u --ignore-matching-lines="create-date" "$name-good.html" "$name-py2.4-test.html"

# test stdin + stdout generation
name="footnotes-1-6"
cat "$name.lyx" | ../elyxer.py --css ../docs/lyx.css > "$name-stdio-test.html"
diff -u --ignore-matching-lines="create-date" "$name-stdio-good.html" "$name-stdio-test.html"

# test --splitpart generation
name="index-1-6"
../elyxer.py --quiet --splitpart 1 --css ../../docs/lyx.css "$name.lyx" "parts/$name-part-test.html"
for file in parts/$name*.html; do
	goodname=${file/"-test"/"-good"}
	diff -u --ignore-matching-lines="create-date" "$goodname" "$file"
done

# test template generation
name="helloworld"
../elyxer.py --quiet --template template.html "$name.lyx" "$name-template-test.html"
diff -u --ignore-matching-lines="create-date" "$name-template-good.html" "$name-template-test.html"


