#!/bin/sh
#
# Generate asciidoc from the features.conf file in the top-level direftory.
# Copyright (c) Jonas Fonseca <fonseca@diku.dk>, 2005
#

CONFFILE="$1"
TMPFILE=$(mktemp import-features.conf.XXXXXX) || exit 1

strip_comment()
{
	echo "$1" | sed 's/^#\+\( \)*//'
}

print_line()
{
	line="$1"

	echo "$(strip_comment "$line")"
}

first_section=
config_default=
config_name="features.conf"
section_title=
section_mark="~"
first_section=

print_section()
{
	if [ -n "$section_title" ]; then
		# "### Feature configuration file"
		if [ "$section_title" != "Notes for developers" ]; then
			label=
			if [ -n "$config_name" ]; then
				section_title="$section_title (\`$config_name\`)"
				label="[[$(echo $config_name | sed 's/_/-/g')]]"
			fi
			cat << __END__
$label
$section_title
$(echo "$section_title" | tr 'A-Za-z -_`' "$section_mark")
$(cat $TMPFILE)

$config_default

__END__
		fi
		echo > $TMPFILE
		section_mark="^"
		section_title=
		config_name=
		config_default=
	fi
}

cat "$CONFFILE" | while read line; do
	case "$line" in
	"### "*)
		print_section
		# Reset
		section_title=$(strip_comment "$line")
		;;
	CONFIG*)
		config_name=$(echo "$line" | cut -d '=' -f 1)
		print_section
		;;
	"# Default:"*)
		config_default=$(strip_comment "$line" | sed 's/Default://')
		config_default="*Default:* $config_default"
		;;
	""|"# vim:"*)
		# Ignore
		;;
	"#"*)
		# Backslashify --help, --enable, --disable to avoid &#8212;.
		line=$(strip_comment "$line" | sed 's/ --\([hed]\)/ \\--\1/')
		echo "$line" >> $TMPFILE
		;;
	esac
done

print_section

rm -f $TMPFILE

# vim: tabstop=4 shiftwidth=4 textwidth=76