#!/bin/sh
# requirements: docbook_4 docbook-xsl-stylesheets fop libxslt

VERSION=`cat ../VERSION`

DB='http://docbook.sourceforge.net/release/xsl/current/'

# Usage: rebuild name
function rebuild {
  name=`echo $1 | sed 's/\.xml\.in$//'`
  if [ -z $name ]; then
      echo "The name argument is empty"
      return 1
  fi

  if [ ! -f $name.xml.in ]; then
      echo "The $name.xml.in missing"
      return 2
  fi
  
  sed "s:@VERSION@:$VERSION:" $name.xml.in > $name.xml
  xsltproc --nonet $DB/manpages/docbook.xsl $name.xml
  xsltproc --nonet --output $name.html  $DB/html/docbook.xsl $name.xml
  xsltproc --nonet --output $name.fo \
  --stringparam paper.type A4 \
  --stringparam body.start.indent 0pt \
  --stringparam title.margin.left 0pt \
  --stringparam variablelist.as.blocks 1 \
  $DB/fo/docbook.xsl $name.xml
  fop $name.fo $name.pdf
  rm -f $name.fo $name.xml
}

if [ -z $1 ]; then
    for f in *.xml.in; do
        rebuild $f
    done
else
    while [ ! -z $1 ]; do
        rebuild $1
        shift 1
    done
fi
