summaryrefslogtreecommitdiffstats
path: root/podwrapper.sh.in
blob: c883c6a8c811e360dd17addbdd0fef8a56d19865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/bin/bash -
# podwrapper.sh
# Copyright (C) 2010-2011 Red Hat Inc.
# @configure_input@
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

# Wrapper script around POD utilities which can include files in the
# POD and controls HTML generation.

unset CDPATH

set -e
#set -x

PACKAGE_NAME="@PACKAGE_NAME@"
PACKAGE_VERSION="@PACKAGE_VERSION@"
POD2MAN="@POD2MAN@"
POD2TEXT="@POD2TEXT@"
POD2HTML="@POD2HTML@"
POD2_STDERR_OPTION="@POD2_STDERR_OPTION@"
POD2_UTF8_OPTION="@POD2_UTF8_OPTION@"

# This script could be run with any current directory, so if a source
# or build path is required it must be relative to the following
# absolute paths:
abs_top_srcdir="@abs_top_srcdir@"
abs_top_builddir="@abs_top_builddir@"

if [ -z "$abs_top_srcdir" ]; then
    echo "*** podwrapper.sh: error: abs_top_srcdir not defined"
    echo "probably this is a very old version of autoconf and you need to"
    echo "upgrade to a recent version"
    exit 1
fi

if [ -z "$abs_top_builddir" ]; then
    echo "*** podwrapper.sh: error: abs_top_builddir not defined"
    echo "probably this is a very old version of autoconf and you need to"
    echo "upgrade to a recent version"
    exit 1
fi

declare -a inserts
declare -a pattern
declare -a indent
nr_inserts=0

TEMP=`getopt \
        -o '' \
        --long section:,name:,man:,text:,html:,insert:,verbatim: \
        -n podwrapper.sh -- "$@"`
if [ $? != 0 ]; then
    echo "podwrapper.sh: problem parsing the command line arguments"
    exit 1
fi
eval set -- "$TEMP"

while true; do
    case "$1" in
        --section)
            section="$2"
            shift 2;;
        --name)
            name="$2"
            shift 2;;
        --man)
            [ -z "$man_output" ] || {
                echo "podwrapper.sh: --text option specified more than once"
                exit 1
            }
            man_output="$2"
            shift 2;;
        --text)
            [ -z "$text_output" ] || {
                echo "podwrapper.sh: --text option specified more than once"
                exit 1
            }
            text_output="$2"
            shift 2;;
        --html)
            [ -z "$html_output" ] || {
                echo "podwrapper.sh: --html option specified more than once"
                exit 1
            }
            html_output="$2"
            shift 2;;
        --insert)
            inserts[$nr_inserts]=`echo "$2" | awk -F: '{print $1}'`
            pattern[$nr_inserts]=`echo "$2" | awk -F: '{print $2}'`
            indent[$nr_inserts]=no
            ((++nr_inserts))
            shift 2;;
        --verbatim)
            inserts[$nr_inserts]=`echo "$2" | awk -F: '{print $1}'`
            pattern[$nr_inserts]=`echo "$2" | awk -F: '{print $2}'`
            indent[$nr_inserts]=yes
            ((++nr_inserts))
            shift 2;;
        --)
            shift; break;;
        *)
            echo "podwrapper.sh: internal error in option parsing"
            exit 1;;
    esac
done

# The remaining argument is the input POD file.
if [ $# -ne 1 ]; then
    echo "podwrapper.sh [--options] input.pod"
    exit 1
fi
input="$1"

#echo "input=$input"
#echo "man_output=$man_output"
#echo "text_output=$text_output"
#echo "html_output=$html_output"
#for i in `seq 0 $(($nr_inserts-1))`; do
#    echo "insert $i: ${inserts[$i]} (pattern: ${pattern[$i]} indent: ${indent[$i]})"
#done

# Should be at least one sort of output.
[ -z "$man_output" -a -z "$text_output" -a -z "$html_output" ] && {
    echo "podwrapper.sh: no output specified"
    exit 1
}

# If name and section are not set, make some sensible defaults.
[ -z "$section" ] && section=1
[ -z "$name" ] && name=$(basename "$input" .pod)

# Perform the insertions to produce a temporary POD file.
tmpdir="$(mktemp -d)"
trap "rm -rf $tmpdir; exit $?" EXIT

if [ $nr_inserts -gt 0 ]; then
    cmd="sed"

    for i in `seq 0 $(($nr_inserts-1))`; do
        if [ "${indent[$i]}" = "yes" ]; then
            sed 's/^/ /' < "${inserts[$i]}" > $tmpdir/$i
        else
            cp "${inserts[$i]}" $tmpdir/$i
        fi

        cmd="$cmd -e /${pattern[$i]}/r$tmpdir/$i -e s/${pattern[$i]}//"
    done

    $cmd < "$input" > $tmpdir/full.pod
else
    cp "$input" $tmpdir/full.pod
fi

# Now generate the final output format(s).
if [ -n "$man_output" ]; then
    "$POD2MAN" "$POD2_STDERR_OPTION" "$POD2_UTF8_OPTION" \
        --section "$section" -c "Virtualization Support" --name "$name" \
        --release "$PACKAGE_NAME-$PACKAGE_VERSION" \
        < $tmpdir/full.pod > "$man_output".tmp
    mv "$man_output".tmp "$man_output"
fi

if [ -n "$text_output" ]; then
    "$POD2TEXT" "$POD2_STDERR_OPTION" "$POD2_UTF8_OPTION" \
        < $tmpdir/full.pod > "$text_output".tmp
    mv "$text_output".tmp "$text_output"
fi

if [ -n "$html_output" ]; then
    "$POD2HTML" \
        --css "pod.css" --htmldir "$abs_top_builddir/html" \
        < $tmpdir/full.pod > "$html_output".tmp
    mv "$html_output".tmp "$html_output"

    # Fix up some of the mess in the HTML output, mainly to make links
    # between man pages work properly.

    # Rewrite <em>manpage(n)</em> to <a href=...>manpage(n)</a> if
    # there is a linkable manual page.
    sed_cmd="sed"
    for f in $(cd "$abs_top_builddir/html" && ls -1 *.html); do
        b=$(basename $f .html)
        m=$(echo $b | sed 's/\(.*\)\.\([1-9]\)$/\1(\2)/')
        sed_cmd="$sed_cmd -e 's,<em>$m</em>,<a href=$f>$m</a>,g'"
    done
    echo $sed_cmd
    eval $sed_cmd < "$html_output" > "$html_output".tmp
    mv "$html_output".tmp "$html_output"

    # Fix links like L<guestfs-foo(3)>
    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
    mv "$html_output".tmp "$html_output"
fi