Add a new internal-autosync API to perform autosync.
[libguestfs.git] / podwrapper.sh.in
1 #!/bin/bash -
2 # podwrapper.sh
3 # Copyright (C) 2010 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
34 # This script could be run with any current directory, so if a source
35 # or build path is required it must be relative to the following
36 # absolute paths:
37 abs_top_srcdir="@abs_top_srcdir@"
38 abs_top_builddir="@abs_top_builddir@"
39
40 if [ -z "$abs_top_srcdir" ]; then
41     echo "*** podwrapper.sh: error: abs_top_srcdir not defined"
42     echo "probably this is a very old version of autoconf and you need to"
43     echo "upgrade to a recent version"
44     exit 1
45 fi
46
47 if [ -z "$abs_top_builddir" ]; then
48     echo "*** podwrapper.sh: error: abs_top_builddir not defined"
49     echo "probably this is a very old version of autoconf and you need to"
50     echo "upgrade to a recent version"
51     exit 1
52 fi
53
54 declare -a inserts
55 declare -a pattern
56 declare -a indent
57 nr_inserts=0
58
59 TEMP=`getopt \
60         -o '' \
61         --long section:,name:,man:,text:,html:,insert:,verbatim: \
62         -n podwrapper.sh -- "$@"`
63 if [ $? != 0 ]; then
64     echo "podwrapper.sh: problem parsing the command line arguments"
65     exit 1
66 fi
67 eval set -- "$TEMP"
68
69 while true; do
70     case "$1" in
71         --section)
72             section="$2"
73             shift 2;;
74         --name)
75             name="$2"
76             shift 2;;
77         --man)
78             [ -z "$man_output" ] || {
79                 echo "podwrapper.sh: --text option specified more than once"
80                 exit 1
81             }
82             man_output="$2"
83             shift 2;;
84         --text)
85             [ -z "$text_output" ] || {
86                 echo "podwrapper.sh: --text option specified more than once"
87                 exit 1
88             }
89             text_output="$2"
90             shift 2;;
91         --html)
92             [ -z "$html_output" ] || {
93                 echo "podwrapper.sh: --html option specified more than once"
94                 exit 1
95             }
96             html_output="$2"
97             shift 2;;
98         --insert)
99             inserts[$nr_inserts]=`echo "$2" | awk -F: '{print $1}'`
100             pattern[$nr_inserts]=`echo "$2" | awk -F: '{print $2}'`
101             indent[$nr_inserts]=no
102             ((++nr_inserts))
103             shift 2;;
104         --verbatim)
105             inserts[$nr_inserts]=`echo "$2" | awk -F: '{print $1}'`
106             pattern[$nr_inserts]=`echo "$2" | awk -F: '{print $2}'`
107             indent[$nr_inserts]=yes
108             ((++nr_inserts))
109             shift 2;;
110         --)
111             shift; break;;
112         *)
113             echo "podwrapper.sh: internal error in option parsing"
114             exit 1;;
115     esac
116 done
117
118 # The remaining argument is the input POD file.
119 if [ $# -ne 1 ]; then
120     echo "podwrapper.sh [--options] input.pod"
121     exit 1
122 fi
123 input="$1"
124
125 #echo "input=$input"
126 #echo "man_output=$man_output"
127 #echo "text_output=$text_output"
128 #echo "html_output=$html_output"
129 #for i in `seq 0 $(($nr_inserts-1))`; do
130 #    echo "insert $i: ${inserts[$i]} (pattern: ${pattern[$i]} indent: ${indent[$i]})"
131 #done
132
133 # Should be at least one sort of output.
134 [ -z "$man_output" -a -z "$text_output" -a -z "$html_output" ] && {
135     echo "podwrapper.sh: no output specified"
136     exit 1
137 }
138
139 # If name and section are not set, make some sensible defaults.
140 [ -z "$section" ] && section=1
141 [ -z "$name" ] && name=$(basename "$input" .pod)
142
143 # Perform the insertions to produce a temporary POD file.
144 tmpdir="$(mktemp -d)"
145 trap "rm -rf $tmpdir; exit $?" EXIT
146
147 if [ $nr_inserts -gt 0 ]; then
148     cmd="sed"
149
150     for i in `seq 0 $(($nr_inserts-1))`; do
151         if [ "${indent[$i]}" = "yes" ]; then
152             sed 's/^/ /' < "${inserts[$i]}" > $tmpdir/$i
153         else
154             cp "${inserts[$i]}" $tmpdir/$i
155         fi
156
157         cmd="$cmd -e /${pattern[$i]}/r$tmpdir/$i -e s/${pattern[$i]}//"
158     done
159
160     $cmd < "$input" > $tmpdir/full.pod
161 else
162     cp "$input" $tmpdir/full.pod
163 fi
164
165 # Now generate the final output format(s).
166 if [ -n "$man_output" ]; then
167     "$POD2MAN" --stderr -u \
168         --section "$section" -c "Virtualization Support" --name "$name" \
169         --release "$PACKAGE_NAME-$PACKAGE_VERSION" \
170         < $tmpdir/full.pod > "$man_output".tmp
171     mv "$man_output".tmp "$man_output"
172 fi
173
174 if [ -n "$text_output" ]; then
175     "$POD2TEXT" --stderr -u \
176         < $tmpdir/full.pod > "$text_output".tmp
177     mv "$text_output".tmp "$text_output"
178 fi
179
180 if [ -n "$html_output" ]; then
181     "$POD2HTML" \
182         --css "pod.css" --htmldir "$abs_top_builddir/html" \
183         < $tmpdir/full.pod > "$html_output".tmp
184     mv "$html_output".tmp "$html_output"
185
186     # Fix up some of the mess in the HTML output, mainly to make links
187     # between man pages work properly.
188
189     # Rewrite <em>manpage(n)</em> to <a href=...>manpage(n)</a> if
190     # there is a linkable manual page.
191     sed_cmd="sed"
192     for f in $(cd "$abs_top_builddir/html" && ls -1 *.html); do
193         b=$(basename $f .html)
194         m=$(echo $b | sed 's/\(.*\)\.\([1-9]\)$/\1(\2)/')
195         sed_cmd="$sed_cmd -e 's,<em>$m</em>,<a href=$f>$m</a>,g'"
196     done
197     echo $sed_cmd
198     eval $sed_cmd < "$html_output" > "$html_output".tmp
199     mv "$html_output".tmp "$html_output"
200
201     # Fix links like L<guestfs-foo(3)>
202     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
203     mv "$html_output".tmp "$html_output"
204 fi