+#!/usr/bin/env bash
+
+# Receivers, filters, writers are called "modules" in this script
+print_modhelp()
+{
+ local ggo="$1"
+
+ $GENGETOPT --show-detailed-help \
+ --set-version "" \
+ --set-package "" \
+ < "$ggo" | awk 'BEGIN {
+ have_purpose=0
+ have_usage=0
+ } {
+ if (!have_purpose) {
+ if ($0 ~ /^ *$/)
+ next
+ printf(" (%s):", $0)
+ have_purpose=1
+ next
+ }
+ if (!have_usage) {
+ if ($0 ~ /^Usage: /) {
+ have_usage=1
+ }
+ next
+ }
+ print $0
+ }'
+}
+
+make_help()
+{
+ local target="$1" module ggo
+
+ ggo="$GGO_DIR/$1.ggo"
+ $GENGETOPT --show-detailed-help \
+ --set-version "$VERSION" \
+ --set-package "para_$1" \
+ < "$ggo"
+
+ if [[ "$target" == 'recv' || "$target" == 'audiod' ]]; then
+ for module in $RECEIVERS; do
+ ggo="$GGO_DIR/${module}_recv.ggo"
+ [[ ! -f "$ggo" ]] && continue
+ printf "\nOptions for the $module receiver"
+ print_modhelp "$ggo"
+ done
+ fi
+ if [[ "$target" == 'filter' || "$target" == 'audiod' ]]; then
+ for module in $FILTERS; do
+ ggo="$GGO_DIR/${module}_filter.ggo"
+ [[ ! -f "$ggo" ]] && continue
+ printf "\nOptions for the $module filter"
+ print_modhelp "$ggo"
+ done
+ fi
+ if [[ "$target" == 'write' || "$target" == 'audiod' ]]; then
+ for module in $WRITERS; do
+ ggo="$GGO_DIR/${module}_write.ggo"
+ [[ ! -f "$ggo" ]] && continue
+ printf "\nOptions for the $module writer"
+ print_modhelp "$ggo"
+ done
+ fi
+}
+
+set -u
+
+(($# != 1)) && exit 1
+
+# These must be set by the caller (make or help2man)
+export COMMAND_LISTS FILTERS GENGETOPT GGO_DIR HELP2MAN HELP2MAN_DIR \
+ RECEIVERS VERSION WRITERS
+
+# If either --version or --help-xxx was given, we are being called by help2man
+if [[ "$1" == "--version" ]]; then
+ echo "$VERSION"
+ exit $?
+fi
+if [[ "$1" =~ --help- ]]; then
+ make_help "${1#--help-}"
+ exit $?
+fi
+
+# Called by make, run help2man
+output_file="$1"
+target="${output_file##*/para_}"
+target="${target%.*}" # server, audiod, filter, ...
+link="$HELP2MAN_DIR/para_$target"
+
+cl_opts=
+for cl in $COMMAND_LISTS; do
+ cl_opts+=" --include $cl"
+done
+
+# Create a symlink para_$target, pointing to this script. This hack is
+# necessary because help2man always includes the name of the executable in its
+# output.
+ln -sf "$PWD/$0" "$link"
+
+# This will call us again twice, with either --help-$target or --version given.
+$HELP2MAN --no-info --help-option "--help-$target" $cl_opts \
+ "$link" > "$output_file"
+if (($? != 0)); then
+ rm -f "$output_file"
+ exit 1
+fi