#! /bin/sh
# Jade Wrapper
# Script to convert a SGML file into some other format
# Send any comments to Eric Bischoff <eric@caldera.de>
# This program is under GPL license. See LICENSE file for details.

# Set help message
SGML_HELP_MESSAGE="Usage: `basename $0` [<options>] <sgml_file>\n\
where <options> are:\n\
\040 -f|--frontend <frontend>: \t Specify the frontend (source format)\n\
\040 \t\t\t\t (default is docbook)\n\
\040 -b|--backend <backend>: \t Specify the backend (destination format)\n\
\040 \t\t\t\t (default is html)\n\
\040 -c|--cat <file>: \t\t Specify an extra SGML open catalog\n\
\040 -n|--nostd: \t\t\t Do not use the standard SGML open catalogs\n\
\040 -d|--dsl <file>|default|none:\t Specify an alternate style sheet\n\
\040 \t\t\t\t (default is to use the default stylesheet)\n\
\040 -s|--sgmlbase <path>: \t Change base directory for SGML distribution\n\
\040 \t\t\t\t (usually /usr/share/sgml)\n\
\040 -p|--parser <program>: \t Specify the parser if several are installed\n\
\040 \t\t\t\t  (jade or openjade)\n\
\040 -o|--output <directory>: \t Set output directory\n\
\040 -u|--nochunks: \t\t Output only one big file\n\
\040 \t\t\t\t (overrides the stylesheet settings)\n\
\040 -i|--include <section>: \t Specify a SGML marked section to include\n\
\040 \t\t\t\t (should be marked as \"ignore\" in the SGML text)\n\
\040 -h|--help: \t\t\t Print this help message and exit\n\
\040 -v|--version: \t\t Print the version and exit"

# Get name of main SGML configuration file
which sgmlwhich >/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
  SGML_CONF=`sgmlwhich`
else
  SGML_CONF="/etc/sgml/sgml.conf"
fi

# Set version message
SGML_VERSION_MESSAGE="DocBook-utils version 0.6 (jw version 1.1)"

# Set frontend to use
SGML_FRONTEND="docbook"

# Set backend to use
SGML_BACKEND="html"

# Set option: use standard catalogs
SGML_STANDARD_CATALOGS="yes"

# Set path to user SGML catalogs
SGML_EXTRA_CATALOGS=""

# Set SGML base directory and centralized catalogs directory
SGML_BASE_DIR="/usr/share/sgml"
SGML_CATALOGS_DIR="/etc/sgml"
if [ -f "$SGML_CONF" ]
then
  RE='^[:space:]*SGML_BASE_DIR[:space:]*=[:space:]*'
  SGML_BASE_DIR=`grep $RE $SGML_CONF | sed "s/$RE//"`
  RE='^[:space:]*SGML_CATALOGS_DIR[:space:]*=[:space:]*'
  SGML_CATALOGS_DIR=`grep $RE $SGML_CONF | sed "s/$RE//"`
fi

# Set main stylesheet
SGML_STYLESHEET="default"

# Set flavour of jade parser to use
SGML_JADE=""

# Set SGML declaration and file to process
SGML_DECL=""
SGML_FILE=""

# Set output and current directories
SGML_OUTPUT_DIRECTORY="."
SGML_CURRENT_DIRECTORY=`pwd`

# Set option: do not output many chunks
SGML_NOCHUNKS=""

# Set list of "include" marked sections
SGML_INCLUDE=""

# Process options
while [ $# -gt 0 ]
do case $1 in
  -f|--frontend) SGML_FRONTEND=$2
		shift 2
		;;
  -b|--backend)	SGML_BACKEND=$2
		shift 2
		;;
  -c|--cat)	SGML_EXTRA_CATALOGS=$SGML_EXTRA_CATALOGS:$2
		shift 2
		;;
  -d|--dsl)	SGML_STYLESHEET=$2
		shift 2
		;;
  -l|--dcl)	SGML_DECL=$2
		shift 2
		;;
  -n|--nostd)	SGML_STANDARD_CATALOGS="no"
		shift 1
		;;
  -s|--sgmlbase) SGML_BASE_DIR=$2
		shift 2
		;;
  -p|--parser)	SGML_JADE=$2
		shift 2
		;;
  -o|--output)	SGML_OUTPUT_DIRECTORY=$2
		shift 2
		;;
  -u|--nochunks) SGML_NOCHUNKS="-V nochunks"
		shift 1
		;;
  -i|--include)	case $2 in
		  print|html)	echo "The marked section named \"$2\" is reserved for internal use by `basename $0`."
				exit 1
				;;
		esac
		SGML_INCLUDE="$SGML_INCLUDE -i $2"
		shift 2
		;;
  -h|--help)	echo -e "`basename $0` - Jade Wrapper\n"
		echo -e $SGML_HELP_MESSAGE
		exit 0
		;;
  -v|--version)	echo -e $SGML_VERSION_MESSAGE
		exit 0
		;;
  -*)		echo -e $SGML_HELP_MESSAGE >&2
		exit 1
		;;
  *)		if [ -z "$SGML_FILE" ]
		then
		  SGML_FILE=$1
		  shift 1
		else
		  echo -e $SGML_HELP_MESSAGE >&2
		  exit 1
		fi
		;;
  esac
done

# Check that we have a frontend
if [ -z "$SGML_FRONTEND" ]
then
  echo -e $SGML_HELP_MESSAGE >&2
  exit 1
fi
if [ -n "${SGML_FRONTEND%%/*}" ]
then
  SGML_FRONTEND=$SGML_BASE_DIR/docbook/utils-0.6/frontends/$SGML_FRONTEND
fi
if [ ! -s $SGML_FRONTEND ]
then
  echo "`basename $0`: There is no frontend called \"$SGML_FRONTEND\"." >&2
  exit 2
fi

# Check that we have a backend
if [ -z "$SGML_BACKEND" ]
then
  echo -e $SGML_HELP_MESSAGE >&2
  exit 1
fi
if [ -n "${SGML_BACKEND%%/*}" ]
then
  SGML_BACKEND=$SGML_BASE_DIR/docbook/utils-0.6/backends/$SGML_BACKEND
fi
if [ ! -s $SGML_BACKEND ]
then
  echo "`basename $0`: There is no backend called \"$SGML_BACKEND\"." >&2
  exit 2
fi

# Check that we have a file to process
if [ -z "$SGML_FILE" ]
then
  echo -e $SGML_HELP_MESSAGE >&2
  exit 1
fi
case $SGML_FILE in
   /*)	;;
   *)	SGML_FILE=`pwd`/$SGML_FILE
	;;
esac
if [ ! -s $SGML_FILE ]
then
  echo "`basename $0`: Cannot open \"$SGML_FILE\"." >&2
  exit 3
fi

# Separate the file name and the extension (if any)
SGML_FILE_NAME=`basename $SGML_FILE`
SGML_FILE_NAME=${SGML_FILE_NAME%.*}

# Determine if we are in an XML file
SGML_XML=`(grep "<?xml.*?>" $SGML_FILE; echo "sgml") | head -n 1 | sed 's/^.*xml.*$/xml/'`

# Use SGML declaration for XML if needed
if [ "$SGML_DECL" = "" -a "$SGML_XML" = "xml" ]
then SGML_DECL=${SGML_BASE_DIR}/xml.dcl
fi

# Set path to SGML catalogs (first try centralized catalog)
case $SGML_STANDARD_CATALOGS in
  yes)	export SGML_CATALOGS_DIR SGML_FILE SGML_XML
	SGML_CENTRALIZED_CATALOG=`sh $SGML_FRONTEND centralized-catalog`
	if [ -s $SGML_CENTRALIZED_CATALOG ]
	then
	  SGML_CATALOG_FILES=$SGML_CENTRALIZED_CATALOG
	else
	  SGML_CATALOG_FILES=`find $SGML_BASE_DIR -name catalog`
	  SGML_CATALOG_FILES=`echo "$SGML_CATALOG_FILES" | tr [:space:] :`
	fi
	;;
  no)	SGML_CATALOG_FILES=""
	;;
esac
SGML_CATALOG_FILES=${SGML_CATALOG_FILES}${SGML_EXTRA_CATALOGS}
SGML_CATALOG_FILES=`echo "$SGML_CATALOG_FILES" | sed 's/^://;s/:$//'`
if [ -z "$SGML_CATALOG_FILES" ]
then
  echo "`basename $0`: Please specify at least one catalog" >&2
  exit 4
fi

# Determine the backend type
case $SGML_BACKEND in
  *html) \
	SGML_TYPE=html
	;;
  *)	SGML_TYPE=print
	;;
esac

# Determine which stylesheet to use
# (run the frontend to know it if the default stylesheet or no custom stylesheet is desired)
if [ "$SGML_STYLESHEET" = "default" -o "$SGML_STYLESHEET" = "none" ]
then
  export SGML_BASE_DIR SGML_TYPE SGML_STYLESHEET
  SGML_STYLESHEET=`sh $SGML_FRONTEND style-sheet`
  SGML_RETURN=$?
  if [ $SGML_RETURN -gt 0 ]
  then exit `expr 7 + $SGML_RETURN`
  fi
fi
if [ -z "$SGML_STYLESHEET" ]
then
  echo "`basename $0`: Please specify at least one stylesheet" >&2
  exit 5
fi

# Choose a parser
if [ -z "$SGML_JADE" ]
then
  which jade >/dev/null 2>/dev/null
  if [ $? -eq 0 ]
  then
    SGML_JADE="jade"
  else
    which openjade >/dev/null 2>/dev/null
    if [ $? -eq 0 ]
    then 
      SGML_JADE="openjade"
    else
      echo "`basename $0`: No parser available" >&2
      exit 6
    fi
  fi
else
  which $SGML_JADE >/dev/null 2>/dev/null
  if [ $? -ne 0 ]
  then 
    echo "`basename $0`: parser $SGML_JADE is not available" >&2
    exit 6
  fi
fi

# Create output directory if not available
if [ -z "$SGML_OUTPUT_DIRECTORY" ]
then
  echo -e $SGML_HELP_MESSAGE >&2
  exit 1
fi
if [ ! -d $SGML_OUTPUT_DIRECTORY ]
then
  mkdir $SGML_OUTPUT_DIRECTORY 2>/dev/null
  if [ $? -ne 0 ]
  then
    echo "`basename $0`: Could not create \"$SGML_OUTPUT_DIRECTORY\" output directory" >&2
    exit 7
  fi
fi

# Prepare the parser's arguments
SGML_ARGUMENTS="$SGML_INCLUDE -i $SGML_TYPE -d $SGML_STYLESHEET $SGML_NOCHUNKS $SGML_DECL $SGML_FILE"

# Call the backend
echo "Using catalogs: `echo $SGML_CATALOG_FILES | sed 's/:/, /g'`"
echo "Using stylesheet: $SGML_STYLESHEET"
echo "Working on: $SGML_FILE"
cd $SGML_OUTPUT_DIRECTORY
export SGML_JADE SGML_FILE_NAME SGML_ARGUMENTS
export SGML_CATALOG_FILES SGML_BASE_DIR SGML_FILE SGML_STYLESHEET
sh $SGML_BACKEND
SGML_RETURN=$?
cd $SGML_CURRENT_DIRECTORY

if [ $SGML_RETURN -gt 0 ]
then exit `expr 7 + $SGML_RETURN`
fi
echo "Done."
exit 0
