virt-make-fs: Set MBR partition type byte correctly (RHBZ#746295).
[libguestfs.git] / podwrapper.sh.in
1 #!/bin/bash -
2 # podwrapper.sh
3 # Copyright (C) 2010-2011 Red Hat Inc.
4 # @configure_input@
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 # Wrapper script around POD utilities which can include files in the
21 # POD and controls HTML generation.
22
23 unset CDPATH
24
25 set -e
26 #set -x
27
28 PACKAGE_NAME="@PACKAGE_NAME@"
29 PACKAGE_VERSION="@PACKAGE_VERSION@"
30 POD2MAN="@POD2MAN@"
31 POD2TEXT="@POD2TEXT@"
32 POD2HTML="@POD2HTML@"
33 POD2_STDERR_OPTION="@POD2_STDERR_OPTION@"
34 POD2_UTF8_OPTION="@POD2_UTF8_OPTION@"
35
36 # This script could be run with any current directory, so if a source
37 # or build path is required it must be relative to the following
38 # absolute paths:
39 abs_top_srcdir="@abs_top_srcdir@"
40 abs_top_builddir="@abs_top_builddir@"
41
42 if [ -z "$abs_top_srcdir" ]; then
43     echo "*** podwrapper.sh: error: abs_top_srcdir not defined"
44     echo "probably this is a very old version of autoconf and you need to"
45     echo "upgrade to a recent version"
46     exit 1
47 fi
48
49 if [ -z "$abs_top_builddir" ]; then
50     echo "*** podwrapper.sh: error: abs_top_builddir not defined"
51     echo "probably this is a very old version of autoconf and you need to"
52     echo "upgrade to a recent version"
53     exit 1
54 fi
55
56 declare -a inserts
57 declare -a pattern
58 declare -a indent
59 nr_inserts=0
60
61 TEMP=`getopt \
62         -o '' \
63         --long section:,name:,man:,text:,html:,insert:,verbatim: \
64         -n podwrapper.sh -- "$@"`
65 if [ $? != 0 ]; then
66     echo "podwrapper.sh: problem parsing the command line arguments"
67     exit 1
68 fi
69 eval set -- "$TEMP"
70
71 while true; do
72     case "$1" in
73         --section)
74             section="$2"
75             shift 2;;
76         --name)
77             name="$2"
78             shift 2;;
79         --man)
80             [ -z "$man_output" ] || {
81                 echo "podwrapper.sh: --text option specified more than once"
82                 exit 1
83             }
84             man_output="$2"
85             shift 2;;
86         --text)
87             [ -z "$text_output" ] || {
88                 echo "podwrapper.sh: --text option specified more than once"
89                 exit 1
90             }
91             text_output="$2"
92             shift 2;;
93         --html)
94             [ -z "$html_output" ] || {
95                 echo "podwrapper.sh: --html option specified more than once"
96                 exit 1
97             }
98             html_output="$2"
99             shift 2;;
100         --insert)
101             inserts[$nr_inserts]=`echo "$2" | awk -F: '{print $1}'`
102             pattern[$nr_inserts]=`echo "$2" | awk -F: '{print $2}'`
103             indent[$nr_inserts]=no
104             ((++nr_inserts))
105             shift 2;;
106         --verbatim)
107             inserts[$nr_inserts]=`echo "$2" | awk -F: '{print $1}'`
108             pattern[$nr_inserts]=`echo "$2" | awk -F: '{print $2}'`
109             indent[$nr_inserts]=yes
110             ((++nr_inserts))
111             shift 2;;
112         --)
113             shift; break;;
114         *)
115             echo "podwrapper.sh: internal error in option parsing"
116             exit 1;;
117     esac
118 done
119
120 # The remaining argument is the input POD file.
121 if [ $# -ne 1 ]; then
122     echo "podwrapper.sh [--options] input.pod"
123     exit 1
124 fi
125 input="$1"
126
127 #echo "input=$input"
128 #echo "man_output=$man_output"
129 #echo "text_output=$text_output"
130 #echo "html_output=$html_output"
131 #for i in `seq 0 $(($nr_inserts-1))`; do
132 #    echo "insert $i: ${inserts[$i]} (pattern: ${pattern[$i]} indent: ${indent[$i]})"
133 #done
134
135 # Should be at least one sort of output.
136 [ -z "$man_output" -a -z "$text_output" -a -z "$html_output" ] && {
137     echo "podwrapper.sh: no output specified"
138     exit 1
139 }
140
141 # If name and section are not set, make some sensible defaults.
142 [ -z "$section" ] && section=1
143 [ -z "$name" ] && name=$(basename "$input" .pod)
144
145 # Perform the insertions to produce a temporary POD file.
146 tmpdir="$(mktemp -d)"
147 trap "rm -rf $tmpdir; exit $?" EXIT
148
149 if [ $nr_inserts -gt 0 ]; then
150     cmd="sed"
151
152     for i in `seq 0 $(($nr_inserts-1))`; do
153         if [ "${indent[$i]}" = "yes" ]; then
154             sed 's/^/ /' < "${inserts[$i]}" > $tmpdir/$i
155         else
156             cp "${inserts[$i]}" $tmpdir/$i
157         fi
158
159         cmd="$cmd -e /${pattern[$i]}/r$tmpdir/$i -e s/${pattern[$i]}//"
160     done
161
162     $cmd < "$input" > $tmpdir/full.pod
163 else
164     cp "$input" $tmpdir/full.pod
165 fi
166
167 # Now generate the final output format(s).
168 if [ -n "$man_output" ]; then
169     "$POD2MAN" "$POD2_STDERR_OPTION" "$POD2_UTF8_OPTION" \
170         --section "$section" -c "Virtualization Support" --name "$name" \
171         --release "$PACKAGE_NAME-$PACKAGE_VERSION" \
172         < $tmpdir/full.pod > "$man_output".tmp
173     mv "$man_output".tmp "$man_output"
174 fi
175
176 if [ -n "$text_output" ]; then
177     "$POD2TEXT" "$POD2_STDERR_OPTION" "$POD2_UTF8_OPTION" \
178         < $tmpdir/full.pod > "$text_output".tmp
179     mv "$text_output".tmp "$text_output"
180 fi
181
182 if [ -n "$html_output" ]; then
183     mkdir -p "$abs_top_builddir/html"
184     "$POD2HTML" \
185         --css "pod.css" --htmldir "$abs_top_builddir/html" \
186         < $tmpdir/full.pod > "$html_output".tmp
187     mv "$html_output".tmp "$html_output"
188
189     # Fix up some of the mess in the HTML output, mainly to make links
190     # between man pages work properly.
191
192     # Rewrite <em>manpage(n)</em> to <a href=...>manpage(n)</a> if
193     # there is a linkable manual page.
194     sed_cmd="sed"
195     for f in $(cd "$abs_top_builddir/html" && ls -1 *.html); do
196         b=$(basename $f .html)
197         m=$(echo $b | sed 's/\(.*\)\.\([1-9]\)$/\1(\2)/')
198         sed_cmd="$sed_cmd -e 's,<em>$m</em>,<a href=$f>$m</a>,g'"
199     done
200     echo $sed_cmd
201     eval $sed_cmd < "$html_output" > "$html_output".tmp
202     mv "$html_output".tmp "$html_output"
203
204     # Fix links like L<guestfs-foo(3)>
205     sed 's,<a href="#\([a-z]\+\)">guestfs-\1(\([1-9]\)),<a href="guestfs-\1.\2.html">guestfs-\1(\2),g' < "$html_output" > "$html_output".tmp
206     mv "$html_output".tmp "$html_output"
207 fi