#! /bin/sh

# Make sure we have an absolute path to the backend and stylesheet, as
# jw doesn't like relative paths...

STUFFPATH=`dirname $0`
if [ -n "${STUFFPATH##/*}" ]
then
    STUFFPATH=$PWD/$STUFFPATH
fi

#
#Grab name of the SGML file without extension
#


O_OPTION=0
for i in "$@"
do
    if [ $O_OPTION = "1" ]
        then
            FILENAME=$i
            O_OPTION=0
        fi
    if [ $i = "-o" ]
        then
            O_OPTION=1
        fi
done

if [ -z $FILENAME ]
    then
# Grab name of the SGML file...
        FILENAME="`echo $i | sed 's,\.sgml$,,;s,\.sgm$,,'`"
    fi

# Generate index
jw -f docbook \
    -b $STUFFPATH/index \
    -d $STUFFPATH/stylesheets/redhat.dsl#html \
    -o $FILENAME-tmp \
    $*

# Generate PDF with generated index
jw -f docbook \
    -b $STUFFPATH/indexed-pdfprint \
    -d $STUFFPATH/stylesheets/redhat.dsl#print \
    $*

# Remove temp directory
echo -n Removing temp directory $PWD/$FILENAME-tmp...
rm -rf $PWD/$FILENAME-tmp
echo Done!

