#!/bin/bash

MYNAME=vine-manual-build

print_usage()
{
cat << EOM
${MYNAME} convert DocBook XML to html for Vine Linux.

Usage: ${MYNAME} [OPTION]... TARGET

  -c, --css=CSSURI          change link of cascading style sheet to CSSURI
  -i, --imagedir=IMAGEDIR   change directory of common images.
  -w, --web                 change xsl style sheet for web
      --clean               remove html files (ignore TARGET)
      --help                display help and exit
      --version             output version information and exit
EOM
}

print_version()
{
cat << EOM
${MYNAME} 0.9.1

Copyright (C) 2011 Project Vine.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Written by Yasumichi Akahoshi <yasumichi@vinelinux.org>
EOM
}

ORG_CSS=/usr/share/doc/Vine/css/vine.css
ORG_IMGDIR=/usr/share/doc/Vine/images
TARGET=
STYLE=/usr/share/sgml/docbook/vine-style-xsl/local.xsl
OPTIONS="--stringparam chunker.output.indent yes"

#
# parse arguments.
#
if [ -z "$*" ]; then
	print_usage
	exit 1
fi

while [ ! -z "$*" ]; do
	case $1 in
		-c|--css)
			NEW_CSS=$2
			shift
			;;
		-i|--imagedir)
			NEW_IMGDIR=$2
			shift
			;;
		-w|--web)
			STYLE=/usr/share/sgml/docbook/vine-style-xsl/web.xsl
			;;
		--help)
			print_usage
			exit
			;;
		--version)
			print_version
			exit
			;;
		--clean)
			rm *.html
			exit
			;;
		*)
			if [ -z "${TARGET}" ]; then
				TARGET=$1
				OPTIONS="${OPTIONS} --stringparam db.chunk.basename `basename $TARGET .xml`"
			else
				echo "Too many targets." >&2
				echo >&2
				print_usage
				exit 1
			fi
			;;
	esac
	shift
done

if [ -z $TARGET ]; then
	echo "No target"
	exit 1
fi

#
# convert DocBook XML to html
#
xsltproc ${OPTIONS} ${STYLE} ${TARGET}

#
# replace URI of cascading style sheet
#
if [ -n "${NEW_CSS}" ]; then
	sed -i "s|${ORG_CSS}|${NEW_CSS}|" *.html
fi

#
# replace directory of common images
#
if [ -n "${NEW_IMGDIR}" ]; then
	sed -i "s|${ORG_IMGDIR}|${NEW_IMGDIR}|" *.html
fi

# vi:syntax=sh
