#! /bin/sh

# changed so as to NOT make an output directory 

set -e

# blatently ripped from cygnus-stylesheets

CATALOG=/etc/sgml/catalog
DB_STYLESHEET=/usr/share/sgml/docbook/custom/gnome-doc-tools/gdp-both.dsl
HTML_STYLESHEET=/usr/share/sgml/docbook/custom/gnome-doc-tools/docbook.css
ADMON_GRAPHICS=/usr/share/sgml/docbook/stylesheet/dsssl/modular/images/*.gif

# Dave Mason's option to specify a different stylesheet
case $1 in
    -d) DB_STYLESHEET=$2
        shift 2
	;;
esac

echo "Using stylesheet: \"${DB_STYLESHEET}\""

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
      echo
      input_file=`basename $1`
      input_dir=`dirname $1`

# prepend path if input-file is not in $PWD
      if ! [ "$input_dir" = "." ]; then
	  input_dir=`pwd`/$input_dir
      fi

      output="`echo $input_dir`"
      echo "input file was called $input_file -- output will be in ${output}/"
      echo
  fi
fi


SAVE_PWD=`pwd`
echo "working on $input_file"
(cd $output; jade -c ${CATALOG} -t sgml -ihtml -d ${DB_STYLESHEET}\#html $input_file; cd $SAVE_PWD)


if [ $# -eq 1 ]
    then
    echo "about to copy cascading stylesheet and admon graphics to $output/"
    cp ${HTML_STYLESHEET} ${output}/
    mkdir -p ${output}/stylesheet-images
    cp ${ADMON_GRAPHICS} ${output}/stylesheet-images
fi

exit 0
