#! /bin/sh

#DB_STYLESHEET=/usr/lib/sgml/DB_STYLESHEETs/dbtohtml.dsl
DB_STYLESHEET=/usr/lib/sgml/stylesheets/cygnus-both.dsl
#DB_STYLESHEET=/u/rosalia/devo/doctools/DB_STYLESHEETs/cygnus-both.dsl
HTML_STYLESHEET=/usr/lib/sgml/stylesheets/nwalsh-modular/test/docbook.css

#output=db2html.html
output=db2html-dir
TMPDIR=DBTOHTML_OUTPUT_DIR$$

echo TMPDIR is $TMPDIR

if [ $# -gt 2 ]
then
  echo "Usage: `basename $0` [filename.sgml]" >&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
#    output="`echo $1 | sed 's,\.sgml$,.html,;s,\.sgm$,.html,'`"
    output="`echo $1 | sed 's,\.sgml$,,;s,\.sgm$,,'`"
  fi
fi

# we used to generate a single file, but with the modular DB_STYLESHEETs
# it's best to make a new directory with several html files in it
#cat $* | jade -d $DB_STYLESHEET -t sgml -V nochunks > $TMPFN

mkdir $TMPDIR
SAVE_PWD=`pwd`
(cd $TMPDIR; jade -t sgml -ihtml -d ${DB_STYLESHEET}\#html ../$1; cd $SAVE_PWD)

if [ $# -eq 1 ]
then
  if [ -d ${output}.junk ]
  then
    /bin/rm -rf ${output}.junk
  fi
  if [ -d ${output} ]
  then
    mv $output ${output}.junk
  fi
  echo "about to copy the cascading stylesheet file to temporary directory"
  cp ${HTML_STYLESHEET} ${TMPDIR}/
  echo "about to rename temporary directory to $output"
  mv ${TMPDIR} $output
else
  cat $TMPDIR/*
fi

rm -rf $TMPDIR

exit 0
