Convert filters 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 if [[ "$target" == 'write' || "$target" == 'audiod' ]]; then
43 for module in $WRITERS; do
44 ggo="$GGO_DIR/${module}_write.ggo"
45 [[ ! -f "$ggo" ]] && continue
46 printf "\nOptions for the $module writer"
47 print_modhelp "$ggo"
48 done
49 fi
50 }
51
52 set -u
53
54 (($# != 1)) && exit 1
55
56 # These must be set by the caller (make or help2man)
57 export COMMAND_LISTS FILTERS GENGETOPT GGO_DIR HELP2MAN HELP2MAN_DIR \
58 RECEIVERS VERSION WRITERS
59
60 # If either --version or --help-xxx was given, we are being called by help2man
61 if [[ "$1" == "--version" ]]; then
62 echo "$VERSION"
63 exit $?
64 fi
65 if [[ "$1" =~ --help- ]]; then
66 make_help "${1#--help-}"
67 exit $?
68 fi
69
70 # Called by make, run help2man
71 output_file="$1"
72 target="${output_file##*/para_}"
73 target="${target%.*}" # server, audiod, filter, ...
74 link="$HELP2MAN_DIR/para_$target"
75
76 cl_opts=
77 tempfiles=
78 for cl in $COMMAND_LISTS; do
79 if [[ "$cl" =~ lsg ]]; then
80 tempfiles+=" $cl.man_util.$$"
81 sed -e '/^\.SH / s/$/]/1' -e '/^\.SH / s/^\.SH /[/1' "$cl" > "$cl.man_util.$$"
82 cl_opts+=" --include $cl.man_util.$$"
83 else
84 cl_opts+=" --include $cl"
85 fi
86 done
87
88 # Create a symlink para_$target, pointing to this script. This hack is
89 # necessary because help2man always includes the name of the executable in its
90 # output.
91 ln -sf "$PWD/$0" "$link"
92
93 # This will call us again twice, with either --help-$target or --version given.
94 $HELP2MAN --no-info --help-option "--help-$target" $cl_opts \
95 "$link" > "$output_file"
96 ret=$?
97 rm -f $tempfiles
98 if (($ret != 0)); then
99 rm -f "$output_file"
100 exit 1
101 fi