#! /bin/sh

RH_STYLESHEET=`dirname $0`/stylesheets/RHL7/redhat-html.dsl

HTML_STYLESHEET=/usr/lib/sgml/stylesheets/nwalsh-modular/test/docbook.css
ADMON_GRAPHICS=/usr/lib/sgml/stylesheets/nwalsh-modular/images/*.gif
FINAL_INDEX=generated-index.sgml

output=db2html-dir
TMPDIR=DBTOHTML_OUTPUT_DIR$$
INDEXFILE=INDEX$$
TMPSTYLE=`dirname $0`/stylesheets/STYLE$$
HTM_EXTENSIONS=0

# Process any options...

while getopts ":3" opt; do
    case $opt in
        3 ) HTM_EXTENSIONS=1 ;;
       \? ) echo "Usage: `basename $0` [options] [filename.sgml]" >&2
            echo "Options:" >&2
            echo "     -3 -- create HTML output with .htm file extensions" >&2
            exit 1 ;;
    esac
done
shift $(($OPTIND - 1))

if [ $# -gt 2 ] || [ $# -lt 1 ]
then
    echo "Usage: `basename $0` [options] [filename.sgml]" >&2
    echo "Options:" >&2
    echo "     -3 -- create HTML output with .htm file extensions" >&2
  exit 1
fi

if [ $# -eq 1 ]
then
  if [ ! -r $1 ]
  then
    echo Cannot read \"$1\".  Exiting. >&2
    exit 1
  fi
  if echo $1 | egrep -i '\.sgml$|\.sgm$' >/dev/null 2>&1
  then
    # now make sure that the output directory is always a subdirectory
    # of the current directory
    input_file=`basename $1`
    output="`echo $input_file | sed 's,\.sgml$,,;s,\.sgm$,,'`"
  fi
fi

if [ $HTM_EXTENSIONS -eq 1 ]
    then
        # We need to take the normal stylesheet, and hack a temporary version to use...
        cat $RH_STYLESHEET | sed 's/^[ 	]*"\.html"[ 	]*)[ 	]*$/  ".htm")/' > $TMPSTYLE
        # Point to the new temporary stylesheet...
        RH_STYLESHEET=$TMPSTYLE
    fi

mkdir $TMPDIR
SAVE_PWD=`pwd`
if [ $1 = `basename $1` ]; then
  echo Running jade...
  RH_STYLESHEET="`echo $RH_STYLESHEET | sed 's,^\([^/]\),\.\./\1,'`"
  (cd $TMPDIR; jade -t sgml -ihtml -d ${RH_STYLESHEET} -V html-index ../$1; cd $SAVE_PWD)

# Generate the index, fixing up the problem of bogus empty closing tags (</>)...

    for index_script in /usr/bin/collateindex.pl /usr/lib/sgml/stylesheets/nwalsh-modular/bin/collateindex.pl
    do
        if [ -f $index_script ]; then
            echo Indexing...
            perl $index_script -g -o $FINAL_INDEX $TMPDIR/HTML.index
        fi
    done

# Not sure if these are still needed; when we first tried collateindex, it threw "</>"
# in the generated index in certain cases...

    cat $FINAL_INDEX | sed 's,^[ 	]*</>[ 	]*$,,' > $INDEXFILE
    mv -f $INDEXFILE $FINAL_INDEX

# We should now have a good index; clean up and rerun...

    echo Rerunning jade...
    rm -rf $TMPDIR
    mkdir $TMPDIR
  (cd $TMPDIR; jade -t sgml -ihtml -d ${RH_STYLESHEET} ../$1; cd $SAVE_PWD)
else

# Yeah, I know it's ugly, but...

    echo Rerun this script from the directory containing your SGML... >&2
    exit 1
fi

  if [ -d ${output}.junk ]
  then
    /bin/rm -rf ${output}.junk
  fi
  if [ -d ${output} ]
  then
    mv $output ${output}.junk
  fi
  echo "Copying cascading stylesheet and admon graphics..."
  cp ${HTML_STYLESHEET} ${TMPDIR}/
  mkdir ${TMPDIR}/stylesheet-images
  cp ${ADMON_GRAPHICS} ${TMPDIR}/stylesheet-images

    echo Moving figures and cleaning up random stuff...

    if [ -d figs/ ]
        then
            tar cf - figs/ | (cd $TMPDIR; tar xf -)
            find $TMPDIR/figs/ -type d -name "CVS"|xargs rm -rf
            find $TMPDIR/figs/ -type f -name "*eps"|xargs rm -f
            find $TMPDIR/figs/ -type d -name ".xvpics"|xargs rm -rf
            find $TMPDIR/figs/ -type d -empty|xargs rm -rf
        fi

    rm -f $TMPSTYLE

  mv ${TMPDIR} $output

  rm -rf $TMPDIR

    exit 0
