#!/bin/bash

set -e

# Set PROPALE=yes to get the necessary includes
if [ "${PROPALE}" != "yes" ]
then
    PROPALE=no
fi

error()
{
    echo "ERROR: $(basename $0): $*"
    exit 1
}

usage()
{
    echo "Usage: $(basename $0) [-c] <in-alcovedocfile> [<out-alcovebookfile>]"
}

# If in source tree, use local specfile, else use installed one
if [ -r $(dirname "$0")/../ad2ab-spec.pl ]
then
    SPECFILE=$(dirname "$0")/../ad2ab-spec.pl
else
    SPECFILE=/usr/lib/perl5/sgmlspl-specs/ad2ab-spec.pl
fi

while [ $# -gt 0 ]
do
    case "$1" in
    -c|--cdata) export CDATA_EXAMPLES=YES ;;
    -h|--help) usage; exit 0 ;;
    -*) usage; error "unknown option \`$1'" ;;
    *) break ;;
    esac
    shift
done

[ -r "${SPECFILE}" ] || error "can't find ${SPECFILE}"
echo "Note: using ${SPECFILE} as a converter"

OLDFILE=$1
OLDFILEROOT=${OLDFILE%.sgml}
TMPFILE=${OLDFILEROOT}.tmp.sgml
OLDFILEROOT=${OLDFILEROOT%.alcovedoc}
NEWFILE=${2:-${OLDFILEROOT}.alcovebook.sgml}

[ -r "$OLDFILE" ] || error "File does not exist: \`$OLDFILE'"

# Ignore indirection include files
if head -1 $OLDFILE | grep -i '<!entity' >/dev/null
then
    echo "WARNING: not converting indirection file $OLDFILE"
    exit
fi

has_doctype()
{
    head -1 $1 | grep -i doctype >/dev/null
}

(
    # add a doctype declaration if there is none
    # (necessary for included files)
    # FIXME: this does not handle multiple elts in one file
    if ! has_doctype $OLDFILE
    then
	# guess top-level element
	elt=$(head -1 $OLDFILE | sed 's/^[^<]*<\([^>]*\)>.*$/\1/')
	# add doctype decl
	echo "<!DOCTYPE $elt SYSTEM \"/usr/lib/sgml/dtd/alcovedoc.dtd\">"
    fi

    if [ $PROPALE = "yes" ]
    then
	# Protect all lines contaning refs to general entities
	# FIXME: any tag within such lines will be ignored.  I'd like
	# to do a more fine subst, but sed doesn't want newlines, and
	# it's sufficient for most uses.

	sed < $OLDFILE '/&[^;]*;/ {
i \
<![CDATA[
p
i \
]]>
d
}'
    else
	cat $OLDFILE
    fi
) > $TMPFILE

# Ensure new file gets emptied to start with
:>$NEWFILE

# If orig file had a doctype, add one
if has_doctype $OLDFILE
then
    (
	printf '<!doctype article PUBLIC "-//Alcove//DTD DocBook V4.1-Based Subset AlcoveBook V0.0//EN"'
	if [ $PROPALE = yes ]
	then
	    echo '['
	    echo '<!entity % alcove-includes PUBLIC "-//Alcove//DOCUMENT AlcoveBook Includes//EN">'
	    echo '%alcove-includes;'
	    printf ']'
	fi
	echo '>'
    ) >> $NEWFILE
fi

# We need to force a "Conformance" item in the output flow because all
# this CDATA are surely badly placed most of the time
(nsgmls $TMPFILE; echo C) | sgmlspl ${SPECFILE} >>$NEWFILE
rm $TMPFILE

echo -n Indenting

xemacs -batch -no-init-file \
    -eval '(progn (find-file "'$NEWFILE'")(indent-region (point-min) (point-max) nil)(save-buffer))' 2>&1 |
    perl -nwe 'BEGIN {use IO;}; print "."; STDOUT->flush'
echo ' done.'

echo "Things to fix manually:"
grep -A2 FIXME $NEWFILE
