1#!/bin/sh
2#$Id$
3#Copyright (c) 2012-2015 Pierre Pronchery <khorben@defora.org>
4#
5#Redistribution and use in source and binary forms, with or without
6#modification, are permitted provided that the following conditions are met:
7#
8# * Redistributions of source code must retain the above copyright notice, this
9#   list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above copyright notice,
11#   this list of conditions and the following disclaimer in the documentation
12#   and/or other materials provided with the distribution.
13#
14#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15#AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16#IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17#DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
18#FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19#DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20#SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22#OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23#OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25
26
27#variables
28PREFIX="/usr/local"
29[ -f "../config.sh" ] && . "../config.sh"
30PROGNAME="docbook.sh"
31#executables
32DEBUG="_debug"
33FOP="fop"
34INSTALL="install -m 0644"
35MKDIR="mkdir -m 0755 -p"
36RM="rm -f"
37XMLLINT="xmllint"
38XSLTPROC="xsltproc --nonet --xinclude"
39
40
41#functions
42#debug
43_debug()
44{
45	echo "$@" 1>&3
46	"$@"
47}
48
49
50#docbook
51_docbook()
52{
53	target="$1"
54
55	source="${target%.*}.xml"
56	[ -f "$source" ] || source="${source#$OBJDIR}"
57	ext="${target##*.}"
58	ext="${ext##.}"
59	case "$ext" in
60		html)
61			XSL="http://docbook.sourceforge.net/release/xsl/current/xhtml/docbook.xsl"
62			[ -f "${source%.*}.xsl" ] && XSL="${source%.*}.xsl"
63			[ -f "${target%.*}.xsl" ] && XSL="${target%.*}.xsl"
64			if [ -f "${target%.*}.css.xml" ]; then
65				XSLTPROC="$XSLTPROC --param custom.css.source \"${target%.*}.css.xml\" --param generate.css.header 1"
66			elif [ -f "${source%.*}.css.xml" ]; then
67				XSLTPROC="$XSLTPROC --param custom.css.source \"${source%.*}.css.xml\" --param generate.css.header 1"
68			fi
69			$DEBUG $XSLTPROC -o "$target" "$XSL" "$source"
70			;;
71		pdf)
72			XSL="http://docbook.sourceforge.net/release/xsl/current/fo/docbook.xsl"
73			[ -f "${source%.*}.xsl" ] && XSL="${source%.*}.xsl"
74			[ -f "${target%.*}.xsl" ] && XSL="${target%.*}.xsl"
75			$DEBUG $XSLTPROC -o "${target%.*}.fo" "$XSL" "$source" &&
76			$DEBUG $FOP -fo "${target%.*}.fo" -pdf "$target"
77			$RM -- "${target%.*}.fo"
78			;;
79		1|2|3|4|5|6|7|8|9)
80			XSL="http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl"
81			$DEBUG $XSLTPROC -o "$target" "$XSL" "$source"
82			;;
83		*)
84			_error "$target: Unknown type"
85			return 2
86			;;
87	esac
88
89	if [ $? -ne 0 ]; then
90		_error "$target: Could not create page"
91		$RM -- "$target"
92		return 2
93	fi
94}
95
96
97#error
98_error()
99{
100	echo "$PROGNAME: $@" 1>&2
101	return 2
102}
103
104
105#usage
106_usage()
107{
108	echo "Usage: $PROGNAME [-c|-i|-u][-P prefix] target..." 1>&2
109	return 1
110}
111
112
113#main
114clean=0
115install=0
116uninstall=0
117while getopts "ciuP:" name; do
118	case "$name" in
119		c)
120			clean=1
121			;;
122		i)
123			uninstall=0
124			install=1
125			;;
126		u)
127			install=0
128			uninstall=1
129			;;
130		P)
131			PREFIX="$OPTARG"
132			;;
133		?)
134			_usage
135			exit $?
136			;;
137	esac
138done
139shift $((OPTIND - 1))
140if [ $# -eq 0 ]; then
141	_usage
142	exit $?
143fi
144
145#check the variables
146if [ -z "$PACKAGE" ]; then
147	_error "The PACKAGE variable needs to be set"
148	exit $?
149fi
150
151[ -z "$DATADIR" ] && DATADIR="$PREFIX/share"
152[ -z "$MANDIR" ] && MANDIR="$DATADIR/man"
153
154exec 3>&1
155while [ $# -gt 0 ]; do
156	target="$1"
157	shift
158
159	#determine the type
160	ext="${target##*.}"
161	ext="${ext##.}"
162	case "$ext" in
163		html)
164			instdir="$DATADIR/doc/$ext/$PACKAGE"
165			source="${target#$OBJDIR}"
166			source="${source%.*}.xml"
167			xpath="string(/refentry/refmeta/manvolnum)"
168			section=$($XMLLINT --xpath "$xpath" "$source")
169			if [ $? -eq 0 -a -n "$section" ]; then
170				instdir="$DATADIR/man/html$section"
171			fi
172			;;
173		pdf)
174			instdir="$DATADIR/doc/$ext/$PACKAGE"
175			;;
176		1|2|3|4|5|6|7|8|9)
177			instdir="$MANDIR/man$ext"
178			;;
179		*)
180			_error "$target: Unknown type"
181			exit 2
182			;;
183	esac
184
185	#clean
186	[ "$clean" -ne 0 ] && continue
187
188	#uninstall
189	if [ "$uninstall" -eq 1 ]; then
190		target="${target#$OBJDIR}"
191		$DEBUG $RM -- "$instdir/$target"		|| exit 2
192		continue
193	fi
194
195	#install
196	if [ "$install" -eq 1 ]; then
197		source="${target#$OBJDIR}"
198		dirname=
199		if [ "${source%/*}" != "$source" ]; then
200			dirname="/${source%/*}"
201		fi
202		$DEBUG $MKDIR -- "$instdir$dirname"		|| exit 2
203		$DEBUG $INSTALL "$target" "$instdir/$source"	|| exit 2
204		continue
205	fi
206
207	#create
208	#XXX ignore errors
209	_docbook "$target"					|| break
210done
211