-
Notifications
You must be signed in to change notification settings - Fork 2
/
genman.sh
executable file
·325 lines (278 loc) · 9.2 KB
/
genman.sh
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/sh
# genman.sh
# This is a script for Debian packagers,
# to generate man files from executables' --help options,
# using help2man.
# Tested on Dash, should work with other POSIX shells.
# Version 20220126
# Copyright (C) 2018-2022 John Crawley <[email protected]>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
# Put this script in the package source debian/ directory
# (or some other location, to taste),
# add a file debian/genman-list, debian/<packagename>.genman-list
# or debian/<packagename>.<section-number>.genman-list
# with a list of the executables whose manpages are to be built.
# Shell globs may be used.
# If set, <section-number> determines the manual section,
# otherwise section 1 is used by default.
# Multiple genman-list files can be used, for source building
# multiple packages, or for different manual sections.
#
# Built manpages will be put in debian/genman-pages/, and
# their paths will be appended to an existing debian/*manpages file,
# or put in a new manpages or <packagename>.manpages file.
#
# see genman.sh --help for more info.
#
# Run the script manually before building the package,
# or to auto-run, add this to debian/rules:
# (adjust the path to genman.sh if necessary)
###
#override_dh_installman:
# debian/genman.sh
# dh_installman
#
#override_dh_clean:
# dh_clean
# debian/genman.sh --clean
#
# Also add help2man to Build-Depends in debian/control.
set -e
src_name=$(dpkg-parsechangelog -S Source) || {
echo "$0: Not in debian source directory?" >&2
exit 1
}
pkg_name="$src_name" # will be replaced if <packagename>.*.genman-list found
pkg_ver=$(dpkg-parsechangelog -S Version)
# default if genman-list file has no digit in name
default_section=1
# BunsenLabs generic content
# Other vendors, please edit to taste.
includes="[author]
Written by the BunsenLabs team.
[reporting bugs]
Please report bugs at:
https://github.com/BunsenLabs/${src_name}/issues
[see also]
The BunsenLabs forums may be able to answer your questions:
https://forums.bunsenlabs.org
"
HELP="genman.sh is a wrapper script round 'help2man'
to generate man pages for debian packages.
Usage: genman.sh [OPTIONS] [file]
This is a script for debian packagers, which automates the generation
of simple man pages from the output of executables' \"--help\" options,
along with information from dpkg, and configuration files
in the package source debian/ directory.
Arguments:
<no arguments>
Generate the necessary files in the source directory.
--clean
Return everything in the package debian/ directory
to the state it was in before running the script.
--test <executable>
Display the manpage that would be generated for this file.
Nothing is changed in the source directory.
--makeone <executable>
Generate a single man file for this one executable and
place it in the working directory (package source root).
(You will still need to add it to debian/<package>.manpages.)
-h --help
Show this message.
Configuration:
The script will look for files in debian/
<packagename>.<section>.genman-list
<packagename>.genman-list
genman-list
If <section> ([1-8]) is missing, assume 1.
If <packagename> is also missing, get from dpkg.
The genman-list file should have a list of the executables
(paths relative to the package root) whose manpages are to be built.
Shell globs may be used.
Multiple genman-list files can be used, for source building
multiple packages, or for different manual sections.
Built manpages will be put in debian/genman-pages/, and
their paths will be appended to an existing debian/*manpages file,
or put in a new manpages or <packagename>.manpages file.
The script can be located anywhere, but should be run
from the package source root directory.
Run it manually before building the package,
or to auto-run, add this to debian/rules:
(adjust the path to genman.sh if necessary)
override_dh_installman:
<tab>debian/genman.sh
<tab>dh_installman
override_dh_clean:
<tab>dh_clean
<tab>debian/genman.sh --clean
Also add help2man to Build-Depends in debian/control.
"
### it may not be necessary to edit below this line ###
include_file=debian/genman.include
### functions ###
# pass file with list of executables to make manpages for,
# with file or shell glob on each line
build_mans() (
while read -r line
do
[ -f "$line" ] && {
mk_man "$line"
continue
}
for file in $line # shell glob
do
[ -f "$file" ] || continue
mk_man "$file"
done
done < "$1"
)
# pass "executable" file (it need not actually be executable in the source)
mk_man() (
exec="$1"
cmd=${1##*/}
manfile="${manpg_dir}/${cmd}.${section}"
execflag=false
mkdir -p "$manpg_dir"
grep "${cmd}\(.[1-8]\)\? *\$" "${manpages_file}" >/dev/null 2>&1 && {
echo "$0: ${cmd} already in ${manpages_file}, skipping"
return 0
}
[ -x "$exec" ] && execflag=true
[ "$execflag" = false ] && chmod +x "$exec"
default_desc="a script provided by ${pkg_name}"
desc="$( ./"$exec" --help 2>&1 | sed -rn "/^ *$cmd/ {s/^ *$cmd( -|:| is)? *//p;q}")"
[ -z "$desc" ] && desc="$default_desc"
help2man ./"$exec" --no-info --no-discard-stderr --version-string="$cmd $pkg_ver" --section="$section" --name="$desc" --include="$include_file" | sed "s|$HOME|~|g" > "$manfile"
[ "$execflag" = false ] && chmod -x "$exec"
echo "$manfile" >> "${manpages_file}"
)
# pass "executable" file
# output is piped to 'man -l -' for inspection
# or by --create option to make single file in current directory
test_man() (
createflag=false
[ "$1" = '--create' ] && {
createflag=true
shift
}
exec="$1"
cmd=${1##*/}
manfile=./"${cmd}.${section}"
execflag=false
[ -x "$exec" ] && execflag=true
[ "$execflag" = false ] && chmod +x "$exec"
default_desc="a script provided by ${pkg_name}"
desc="$( ./"$exec" --help 2>&1 | sed -rn "/^ *$cmd/ {s/^ *$cmd( -|:| is)? *//p;q}")"
[ -z "$desc" ] && desc="$default_desc"
if [ "$createflag" = false ]
then
help2man ./"$exec" --no-info --no-discard-stderr --version-string="$cmd $pkg_ver" --section="$section" --name="$desc" --include="$include_file" | sed "s|$HOME|~|g" | man -l -
else
help2man ./"$exec" --no-info --no-discard-stderr --version-string="$cmd $pkg_ver" --section="$section" --name="$desc" --include="$include_file" | sed "s|$HOME|~|g" > "$manfile"
fi
[ "$execflag" = false ] && chmod -x "$exec"
)
### script starts here ###
case $1 in
--clean)
rm -rf debian/*genman-pages
# avoid cleaning existing files
[ -f "$include_file" ] && rm -f debian/*manpages
rm -f "$include_file"
for i in debian/*.genman.bckp # restore backups
do
[ -f "$i" ] || continue
mv "$i" "${i%.genman.bckp}"
done
exit 0
;;
--test)
[ -n "$2" ] || { echo "Please pass a file to test." >&2; exit 1;}
section="$default_section"
include_file="$(mktemp)"
cat <<EOF > "$include_file"
$includes
EOF
test_man "$2"
rm -f "$include_file"
exit
;;
--makeone)
[ -n "$2" ] || { echo "Please pass a file to make a manpage for." >&2; exit 1;}
section="$default_section"
include_file="$(mktemp)"
cat <<EOF > "$include_file"
$includes
EOF
test_man --create "$2"
rm -f "$include_file"
exit
;;
--help|-h)
echo "$HELP"
exit 0
;;
'')
;;
*)
echo "${0}: ERROR \"${1}\": no such option" >&2
echo
echo "$HELP"
exit 1
;;
esac
# avoid multiple runs
[ -f "$include_file" ] && {
echo "$0: manpages already built" >&2
exit 1
}
# backup any existing manpages files
for file in debian/*manpages
do
[ -f "$file" ] || continue
cp "$file" "$file".genman.bckp
done
cat <<EOF > "$include_file"
$includes
EOF
for list in debian/*.genman-list
do
[ -f "$list" ] || continue
found_list=true
pkg_name="${list##*/}"
pkg_name="${pkg_name%.genman-list}"
# If filename is <package>.[1-9].genman-list
# then digit determines section, otherwise use default set above.
case $pkg_name in
*.[1-8])
section="${pkg_name##*.}"
pkg_name="${pkg_name%.*}"
;;
*)
section="$default_section"
;;
esac
manpages_file=debian/${pkg_name}.manpages
manpg_dir=debian/${pkg_name}.genman-pages
build_mans "$list"
done
# simple option
if [ "$found_list" != true ] && [ -f debian/genman-list ]
then
section="$default_section"
manpages_file=debian/manpages
manpg_dir=debian/genman-pages
build_mans debian/genman-list
fi