#!/bin/bash

# Copyright (C) 2008, 2009 Free Software Foundation, Inc.

# This file is part of GNUnited Nations.

# GNUnited Nations 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.

# GNUnited Nations 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 GNUnited Nations.  If not, see <http://www.gnu.org/licenses/>.

function version () {
cat <<EOF
gnun-validate-html (GNUnited Nations) 0.2
Copyright (C) 2009 Free Software Foundation, Inc.
You may redistribute copies of GNUnited Nations
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.
EOF
}

# FIXME: Use AC_INIT's 5th argument and AC_PACKAGE_URL once Autoconf
# 2.64 is released.
function usage () {
cat <<EOF
Usage: gnun-validate-html OPTION | FILE

Options:
      --version              Just print version info and return
      --help                 Print this usage information message

Report bugs to bug-gnun@gnu.org
GNUnited Nations home page: <http://www.gnu.org/software/gnun/>
General help using GNU software: <http://www.gnu.org/gethelp/>
EOF
}

set -e
set -o pipefail

case "$1" in
    --help )
	usage
	exit 0
	;;
    --version )
	version
	exit 0
	;;
    * )
	;;
esac

if [ $# -ne 1 ]; then
    echo 1>&2 Error: FILE argument is required.  See --help for details.
    exit 1
fi

ROOT=../..
DTD=/usr/share/gnun

# Create two tempfiles and mark them for deletion on exit.
TMP1=`mktemp -t gnun.1.XXXXXX`
TMP2=`mktemp -t gnun.2.XXXXXX`
trap "rm -f $TMP1 $TMP2" EXIT

# Expand input file's #include directives and save the result in
# $TMP1.
cat $1 | sed "s/<\!--#include virtual=\"\/\?\(.*\)\" -->/include(\`\1')/g" \
  | m4 -EE -I $ROOT > $TMP1

# Execute xmllint on $TMP1 and save it's output to $TMP2.
set +o pipefail
set +e
cat $TMP1 | xmllint --path "$DTD" --valid --nonet --noout - 2> $TMP2
LINTSTATUS=$?
set -e
set -o pipefail

# Parse xmllint's error output (if any) and print it by inserting
# additional context line after every occurrence of "line X" where X
# is a decimal number.  It is helpful to show the contents of those
# mentioned lines, since xmllint runs on intermediate input file (with
# expanded #include directives) and the translator can not easily look
# up for references in it.
cat $TMP2 | sed '
  /line [[:digit:]]\+/ {
     p
     s=^.*line \([[:digit:]]\+\).*$=head -n \1 '"$TMP1"' | tail -n 1=
     e
  }'

# Exit with xmlint's original exit status.
exit $LINTSTATUS
