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