#!/bin/ksh
# vim: set ft=sh:
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
#              C E D A R
#          S O L U T I O N S       "Software done right."
#           S O F T W A R E
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Copyright (c) 2001-2003 Kenneth J. Pronovici.
# All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# Version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Copies of the GNU General Public License are available from
# the Free Software Foundation website, http://www.gnu.org/.
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
# Author   : Kenneth J. Pronovici <pronovic@ieee.org>
# Language : kshell (ksh-88)
# Project  : docbook scripts
# Package  : render
# Revision : $Id: render,v 1.5 2003/09/08 20:39:40 pronovic Exp $
# Purpose  : Renders a DocBook XML document into other document types
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

# This file was composed using 8-space tabs and a width of 132 characters.

################
# Configuration
################

# These are configuration pieces
STYLESHEET_DIR=/usr/share/sgml/docbook/stylesheet/xsl/nwalsh
CATALOG_FILE=/etc/sgml/catalog

# This variable is used by xsltproc
export SGML_CATALOG_FILES=${CATALOG_FILE}


#######################
# The usage subroutine
#######################

function usage
{
   print ""
   print "Usage: render <file> <type>"
   print ""
   print "Where <file> is a DocBook XML file and <type> is one of:"
   print ""
   print "        html        single HTML document"
   print "        book        multi-document HTML \"book\""
   print "        pdf         Adobe PDF document"
   print "        ps          PostScript document"
   print "        txt         text document"
   print ""
   print "        all         all document types"
   print ""
   print "Output will be to the current working directory.  For the"
   print "\"book\" type, the result will be a directory named the same"
   print "as the source file.  For all others, the result will be a new"
   print "file with the same name a different extension."
   print ""
   print "This script is designed to work with Debian and requires the"
   print "following packages:"
   print ""
   print "  libxml2 libxslt1 xsltproc docbook-xml"
   print "  html2ps gs docbook-xsl-stylesheets w3m"
   print ""
   print "from the current testing distribution (woody)."
}

#######################
# The build subroutine
#######################
# Takes ${1} as file to render, ${2} as file type to create

function build
{
   FILE=${1}
   TYPE=${2}

   case ${TYPE} in
   html)
      rm -f $(basename ${FILE%%.xml}.html) >/dev/null 2>&1
      xsltproc --catalogs --output ${FILE%%.xml}.html ${STYLESHEET_DIR}/html/docbook.xsl ${FILE} 2>/dev/null
      if [[ "$(basename ${FILE%%.xml}.html)" != ${FILE%%.xml}.html ]]
      then
         mv -f ${FILE%%.xml}.html .
      fi
      print "Generated '$(basename ${FILE%%.xml}.html)'."
      ;;
   book)
      rm -rf $(basename ./${FILE%%.xml}) >/dev/null 2>&1
      mkdir -p ${FILE%%.xml} 
      cd ${FILE%%.xml} > /dev/null 2>&1
      xsltproc --catalogs ${STYLESHEET_DIR}/html/chunk.xsl ../${FILE} 2>/dev/null | egrep -v "DOCTYPE|^$"
      cd - > /dev/null 2>&1
      if [[ "$(basename ${FILE%%.xml})" != "${FILE%%.xml}" ]]
      then 
         mv ${FILE%%.xml} .
      fi
      print "Generated book in directory '$(basename ${FILE%%.xml})'."
      ;;
   pdf)
      rm -f $(basename ./${FILE%%.xml}.pdf) >/dev/null 2>&1
      xsltproc --catalogs --output ${FILE%%.xml}_tmp.html ${STYLESHEET_DIR}/html/docbook.xsl ${FILE} 2>/dev/null
      html2ps ${FILE%%.xml}_tmp.html > ${FILE%%.xml}_tmp.ps
      ps2pdf ${FILE%%.xml}_tmp.ps ${FILE%%.xml}.pdf
      rm -f ${FILE%%.xml}_tmp.html
      rm -f ${FILE%%.xml}_tmp.ps
      if [[ "$(basename ${FILE%%.xml}.pdf)" != "${FILE%%.xml}.pdf" ]]
      then 
         mv -f ${FILE%%.xml}.pdf .
      fi
      print "Generated '$(basename ${FILE%%.xml}.pdf)'."
      ;;
   ps)
      rm -f $(basename ./${FILE%%.xml}.ps) >/dev/null 2>&1
      xsltproc --catalogs --output ${FILE%%.xml}_tmp.html ${STYLESHEET_DIR}/html/docbook.xsl ${FILE} 2>/dev/null
      html2ps ${FILE%%.xml}_tmp.html > ${FILE%%.xml}.ps
      rm -f ${FILE%%.xml}_tmp.html
      if [[ "$(basename ${FILE%%.xml}.ps)" != "${FILE%%.xml}.ps" ]]
      then 
         mv ${FILE%%.xml}.ps .
      fi
      print "Generated '$(basename ${FILE%%.xml}.ps)'."
      ;;
   txt|text)
      rm -f $(basename ./${FILE%%.xml}.txt) >/dev/null 2>&1
      xsltproc --catalogs --output ${FILE%%.xml}_tmp.html ${STYLESHEET_DIR}/html/docbook.xsl ${FILE} 2>/dev/null
      w3m ${FILE%%.xml}_tmp.html -dump > ${FILE%%.xml}.txt
      rm -f ${FILE%%.xml}_tmp.html >/dev/null 2>&1
      if [[ "$(basename ${FILE%%.xml}.txt)" != "${FILE%%.xml}.txt" ]]
      then
         mv ${FILE%%.xml}.txt .
      fi
      print "Generated '$(basename ${FILE%%.xml}.txt)'."
      ;;
   *)
      print "Unknown type ${TYPE}."
      exit 1
      ;;
   esac
}


###############
# Main routine
###############

# Check arguments
if [[ $# != 2 || "${1}" = "--help" ]]
then
   usage
   exit 1
fi

# Set configuration based on arguments
FILE=${1}
TYPE=${2}

# Validate configuration
if [[ ! -r ${FILE} ]]
then
   print "Unable to read file ${FILE}."
   exit 1
fi

if [[ ${FILE%%.xml} = ${FILE} ]]
then
   print "File must end in \".xml\"."
   exit 1
fi

xmllint ${FILE} > /dev/null 2>/dev/null
if [[ $? != 0 ]]
then
   print "XML file does not validate with xmllint."
   exit 1
fi

# Build the output files
if [[ "${TYPE}" = "all" ]]
then
   for i in html book pdf ps txt
   do 
      build ${FILE} ${i}
   done
else
   build ${FILE} ${TYPE}
fi


