Version 1.7.17.
[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 declare -a inserts
35 declare -a pattern
36 declare -a indent
37 nr_inserts=0
38
39 TEMP=`getopt \
40         -o '' \
41         --long section:,name:,man:,text:,html:,insert:,verbatim: \
42         -n podwrapper.sh -- "$@"`
43 if [ $? != 0 ]; then
44     echo "podwrapper.sh: problem parsing the command line arguments"
45     exit 1
46 fi
47 eval set -- "$TEMP"
48
49 while true; do
50     case "$1" in
51         --section)
52             section="$2"
53             shift 2;;
54         --name)
55             name="$2"
56             shift 2;;
57         --man)
58             [ -z "$man_output" ] || {
59                 echo "podwrapper.sh: --text option specified more than once"
60                 exit 1
61             }
62             man_output="$2"
63             shift 2;;
64         --text)
65             [ -z "$text_output" ] || {
66                 echo "podwrapper.sh: --text option specified more than once"
67                 exit 1
68             }
69             text_output="$2"
70             shift 2;;
71         --html)
72             [ -z "$html_output" ] || {
73                 echo "podwrapper.sh: --html option specified more than once"
74                 exit 1
75             }
76             html_output="$2"
77             shift 2;;
78         --insert)
79             inserts[$nr_inserts]=`echo "$2" | awk -F: '{print $1}'`
80             pattern[$nr_inserts]=`echo "$2" | awk -F: '{print $2}'`
81             indent[$nr_inserts]=no
82             ((++nr_inserts))
83             shift 2;;
84         --verbatim)
85             inserts[$nr_inserts]=`echo "$2" | awk -F: '{print $1}'`
86             pattern[$nr_inserts]=`echo "$2" | awk -F: '{print $2}'`
87             indent[$nr_inserts]=yes
88             ((++nr_inserts))
89             shift 2;;
90         --)
91             shift; break;;
92         *)
93             echo "podwrapper.sh: internal error in option parsing"
94             exit 1;;
95     esac
96 done
97
98 # The remaining argument is the input POD file.
99 if [ $# -ne 1 ]; then
100     echo "podwrapper.sh [--options] input.pod"
101     exit 1
102 fi
103 input="$1"
104
105 #echo "input=$input"
106 #echo "man_output=$man_output"
107 #echo "text_output=$text_output"
108 #echo "html_output=$html_output"
109 #for i in `seq 0 $(($nr_inserts-1))`; do
110 #    echo "insert $i: ${inserts[$i]} (pattern: ${pattern[$i]} indent: ${indent[$i]})"
111 #done
112
113 # Should be at least one sort of output.
114 [ -z "$man_output" -a -z "$text_output" -a -z "$html_output" ] && {
115     echo "podwrapper.sh: no output specified"
116     exit 1
117 }
118
119 # If name and section are not set, make some sensible defaults.
120 [ -z "$section" ] && section=1
121 [ -z "$name" ] && name=$(basename "$input" .pod)
122
123 # Perform the insertions to produce a temporary POD file.
124 tmpdir="$(mktemp -d)"
125 trap "rm -rf $tmpdir; exit $?" EXIT
126
127 if [ $nr_inserts -gt 0 ]; then
128     cmd="sed"
129
130     for i in `seq 0 $(($nr_inserts-1))`; do
131         if [ "${indent[$i]}" = "yes" ]; then
132             sed 's/^/ /' < "${inserts[$i]}" > $tmpdir/$i
133         else
134             cp "${inserts[$i]}" $tmpdir/$i
135         fi
136
137         cmd="$cmd -e /${pattern[$i]}/r$tmpdir/$i -e s/${pattern[$i]}//"
138     done
139
140     $cmd < "$input" > $tmpdir/full.pod
141 else
142     cp "$input" $tmpdir/full.pod
143 fi
144
145 # Now generate the final output format(s).
146 if [ -n "$man_output" ]; then
147     "$POD2MAN" --stderr -u \
148         --section "$section" -c "Virtualization Support" --name "$name" \
149         --release "$PACKAGE_NAME-$PACKAGE_VERSION" \
150         < $tmpdir/full.pod > "$man_output".tmp
151         mv "$man_output".tmp "$man_output"
152 fi
153
154 if [ -n "$text_output" ]; then
155     "$POD2TEXT" --stderr -u \
156         < $tmpdir/full.pod > "$text_output".tmp
157         mv "$text_output".tmp "$text_output"
158 fi
159
160 if [ -n "$html_output" ]; then
161     "$POD2HTML" \
162         --css "pod.css" --htmldir "$builddir/html" \
163         < $tmpdir/full.pod > "$html_output".tmp
164         mv "$html_output".tmp "$html_output"
165 fi