Convert writers to lopsub.
[paraslash.git] / man_util.bash
1 #!/usr/bin/env bash
2
3 # writers are called "modules" in this script
4 print_modhelp()
5 {
6 local ggo="$1"
7
8 $GENGETOPT --show-detailed-help \
9 --set-version "" \
10 --set-package "" \
11 < "$ggo" | awk 'BEGIN {
12 have_purpose=0
13 have_usage=0
14 } {
15 if (!have_purpose) {
16 if ($0 ~ /^ *$/)
17 next
18 printf(" (%s):", $0)
19 have_purpose=1
20 next
21 }
22 if (!have_usage) {
23 if ($0 ~ /^Usage: /) {
24 have_usage=1
25 }
26 next
27 }
28 print $0
29 }'
30 }
31
32 make_help()
33 {
34 local target="$1" module ggo
35
36 ggo="$GGO_DIR/$1.ggo"
37 $GENGETOPT --show-detailed-help \
38 --set-version "$VERSION" \
39 --set-package "para_$1" \
40 < "$ggo"
41
42 }
43
44 set -u
45
46 (($# != 1)) && exit 1
47
48 # These must be set by the caller (make or help2man)
49 export COMMAND_LISTS FILTERS GENGETOPT GGO_DIR HELP2MAN HELP2MAN_DIR \
50 RECEIVERS VERSION WRITERS
51
52 # If either --version or --help-xxx was given, we are being called by help2man
53 if [[ "$1" == "--version" ]]; then
54 echo "$VERSION"
55 exit $?
56 fi
57 if [[ "$1" =~ --help- ]]; then
58 make_help "${1#--help-}"
59 exit $?
60 fi
61
62 # Called by make, run help2man
63 output_file="$1"
64 target="${output_file##*/para_}"
65 target="${target%.*}" # server, audiod, filter, ...
66 link="$HELP2MAN_DIR/para_$target"
67
68 cl_opts=
69 tempfiles=
70 for cl in $COMMAND_LISTS; do
71 tempfiles+=" $cl.man_util.$$"
72 sed -e '/^\.SH / s/$/]/1' -e '/^\.SH / s/^\.SH /[/1' "$cl" > "$cl.man_util.$$"
73 cl_opts+=" --include $cl.man_util.$$"
74 done
75
76 # Create a symlink para_$target, pointing to this script. This hack is
77 # necessary because help2man always includes the name of the executable in its
78 # output.
79 ln -sf "$PWD/$0" "$link"
80
81 # This will call us again twice, with either --help-$target or --version given.
82 $HELP2MAN --no-info --help-option "--help-$target" $cl_opts \
83 "$link" > "$output_file"
84 ret=$?
85 rm -f $tempfiles
86 if (($ret != 0)); then
87 rm -f "$output_file"
88 exit 1
89 fi