/ .. / / -> download
#!/bin/sh

show_help()
{
	echo "Usage: s1kd2pdf [-c <cfg>] [-d <dbtofo>] [-o <out>] [-s <s1000dtodb>] <src> [<params>]"
	echo
	echo "Options:"
	echo "  -c <cfg>         Specify a FOP XML configuration file."
	echo "  -D               Create template for -d option."
	echo "  -d <dbtofo>      Use a custom stylesheet for Docbook-to-FO conversion."
	echo "  -F               Output XSL-FO instead of PDF."
	echo "  -I               Output Apache FOP intermediary format instead of PDF."
	echo "  -o <out>         Output to <out> instead of <src>.pdf."
	echo "  -S               Create template for -S option."
	echo "  -s <s1000dtodb>  Use a custom stylesheet for S1000D-to-DocBook conversion."
	echo "  <src>            Source S1000D module."
	echo "  <params>         xsltproc parameters to pass to both stylesheets."
	echo
	echo "Example:"
	echo "  s1kd2pdf -o test.pdf test.xml -param show.unclassified 0 -stringparam publication.code 'Test'"
}

FOP_CONFIG=/etc/fop.xml

dir="$(dirname $(dirname "$0"))/share/xml/s1000d/stylesheet/S1000D-XSL-Stylesheets"
s2d="$dir/s1000dtodb/s1000dtodb.xsl"
d2f="$dir/dbtofo/dbtofo.xsl"
fmt=pdf
out=
cfg=

custom_s1000dtodb()
{
	cat <<-EOF
		<?xml version="1.0" encoding="UTF-8"?>
		<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
		  <xsl:import href="$s2d"/>
		</xsl:stylesheet>
	EOF
}

custom_dbtofo()
{
	cat <<-EOF
		<?xml version="1.0" encoding="UTF-8"?>
		<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
		  <xsl:import href="$d2f"/>
		</xsl:stylesheet>
	EOF
}

while getopts c:Dd:FIo:Ss:h? opt
do
	case $opt in
		c)
			cfg="$OPTARG"
			;;
		D)
			custom_dbtofo
			exit
			;;
		d)
			d2f="$OPTARG"
			;;
		F)
			fmt='fo'
			;;
		I)
			fmt='if'
			;;
		o)
			out="$OPTARG"
			;;
		S)
			custom_s1000dtodb
			exit
			;;
		s)
			s2d="$OPTARG"
			;;
		h|?)
			show_help
			exit 2
			;;
	esac
done

shift $((OPTIND - 1))

src="$1"
shift

if test -z "$out"
then
	out="$(echo "$src"|rev|cut -d. -f2-|rev).$fmt"
fi

# Use the global FOP config if -c is not specified
if test -z "$cfg" -a -e "$FOP_CONFIG"
then
	cfg="$FOP_CONFIG"
fi

args=

if test -n "$cfg"
then
	args="$args -c $cfg"
fi

case $fmt in
	pdf)
		xsltproc --xinclude "$@" "$s2d" "$src" |
		xsltproc --xinclude -param fop1.extensions 1 "$@" "$d2f" - |
		fop $args - "$out"
		;;
	fo)
		xsltproc --xinclude "$@" "$s2d" "$src" |
		xsltproc --xinclude -param fop1.extensions 1 "$@" "$d2f" - > "$out"
		;;
	if)
		xsltproc --xinclude "$@" "$s2d" "$src" |
		xsltproc --xinclude -param fop1.extensions 1 "$@" "$d2f" - |
		fop $args -fo - -if application/pdf "$out"
		;;
esac


/ gopher://khzae.net/0/s1000d/S1000D-XSL-Stylesheets/src/s1kd2pdf
Styles: Light Dark Classic