#!/usr/local/bin/bash
#
# (c) 2001-2004 Nicolas Chauvat <nicolas.chauvat@logilab.fr> - License is GPL

set -e 
DIFF="xmldiff -x"

TMPFILE_S=$(mktemp /tmp/xmlrev.start.XXXXXX) || exit 1
TMPFILE_X=$(mktemp /tmp/xmlrev.dest.XXXXXX) || exit 1
TMPFILE_D=$(mktemp /tmp/xmlrev.xupdate.XXXXXX) || exit 1
TMPFILE_XSLT=$(mktemp /tmp/xmlrev.xslt.XXXXXX) || exit 1
TMPFILES="$TMPFILE_S $TMPFILE_X $TMPFILE_D $TMPFILE_XSLT"


function guess_doc_type() {
    # $1 filepath
    FT=`file -b -- "$1"`
    case "$FT" in
	*SGML*)
	    echo SGML
	    ;;
	*XML*)
	    echo XML
	    ;;
        *text*)
            case "$1" in
                *.xml|*.XML|*.xsl|*.XSL|*.xslt|*.XSLT)
                    echo XML
                    ;;
            esac
            ;;
    esac
}

function sgml_to_xml() {
    # $1 filepath
    sgmlnorm "$1" > "$TMPFILE_S"
    xmllint --sgml --nowarning "$TMPFILE_S" | grep -v "<!DOCTYPE"
}

function normalize() {
    # $1 filepath
    DT=`guess_doc_type "$1"`
    case "$DT" in
	SGML)
	    sgml_to_xml "$1" > "$TMPFILE_X"
	    echo $TMPFILE_X
	    ;;
	XML)
	    echo $1
	    ;;
    esac
}

function check_args(){
   if [ -z "$1" ] || [ -z "$2" ]
   then
        usage
        exit 1
    fi

   for filename in "$1" "$2"
   do
     if [ ! -e "$filename" ]
     then
         echo Error:
         echo $filename not found
         echo
         usage
         exit 2
     fi
     DOC=`normalize "$filename"`
     if [ -z "$DOC" ]
     then
        echo Error: $filename is not an XML or SGML file
        usage
        exit 3
     fi
   done
}

function diff() {
    check_args "$1" "$2"
    DOC_OLD=`normalize "$1"`
    DOC_NEW=`normalize "$2"`

    $DIFF "$DOC_OLD" "$DOC_NEW" || true
}

function revision() {
    check_args "$1" "$2"
    DOC_OLD=`normalize "$1"`
    DOC_NEW=`normalize "$2"`

    $DIFF "$DOC_OLD" "$DOC_NEW" > "$TMPFILE_D" || true
    xsltproc "$ML_DIR"/xmlrev.xslt "$TMPFILE_D" > "$TMPFILE_XSLT"
    xsltproc "$TMPFILE_XSLT" "$DOC_OLD"
}

function usage(){
    echo "USAGE: $0 [OPTIONS] from_file to_file"
    echo
    echo OPTIONS:
    echo "    --revision"
    echo "      show difference between revisions as an HTML file (default)"
    echo "    --diff"
    echo "      show difference between revisions as xupdate"
}

ML_DIR=/usr/share/sgml/stylesheet/xmldiff

case "$1" in
    --help)
        usage
        exit 0
        ;;
    --revision)
	revision "$2" "$3"
	;;
    --diff)
	diff "$2" "$3"
	;;
    *)
	revision "$1" "$2"
	;;
esac

rm -f $TMPFILES
