#!/bin/bash
# Extract the list of moin sub-commands from the scripts source code,
# in order to improve the manpage.
# Copyright 2010, Frank lin Piat ; License GPL V2 or later.
set -e

IN=$1
OUT=$2

if echo "\\fB" | grep -vq "f" ; then
	# Dash needs more escaping (\\\\fB instead of \\fB)
	echo $0: Invalid shell >&2
	exit 2
fi

if [ ! -f "$IN" -o ! -d "$(dirname $OUT 2>/dev/null)" ]; then
	echo $0: Invalid parameters >&2
	exit 3
fi

# Let's generate a manpage with the list of moin commands and sub-comands.
sed -n -e '1,/TAG:INSERT_GENENERATED_START/p' \
	$IN > $OUT

for f in $(grep -l 'class PluginScript(' ./MoinMoin/script/*/*.py ); do
	cmd=$(dirname $f | sed -e 's,.*/,,' )
	subcmd=$(basename $f .py)

	(
	echo .sp
	echo .TP
	echo "\\fB$cmd\\fR \\fB$subcmd\\fR \\fB\\-\\-help\\fR \\fB[${subcmd}-option]\\fR"
	cat $f \
		| sed -n -e '/^Purpose:/,/^"""/p' \
		| sed -e '1d ; 2d' \
		| sed -e '/^=====/,$d' \
		| sed -e '$d' \
		| sed -e :a -e '/^\n*$/{$d;N;ba' -e '} # delete trailing blank lines'
	)>> $OUT
done

sed -n -e '/TAG:INSERT_GENENERATED_END/,$p' \
	$IN >> $OUT

