#!/bin/sh

# Generate a Moin syntax table for header file documentation.

DN=$1

if [ ! -e "$DN" ] ; then
    cat 1>&2 <<EOF
Usage: $0 <directory>

Obtain header file details from within a given include directory.
EOF
    exit 1
fi

# Obtain the description from the top of a file.

get_description()
{
    ENDLINE=$(grep -n -m 1 '^ \*$' "$1" | cut -f 1 -d :)
    head -n "$ENDLINE" "$1" | tail -n +2 | head -n -1 | sed 's/^ \* //' | concat_lines
}

# Concatenate lines.

concat_lines()
{
    while read LINE ; do
        echo -n "$LINE "
    done
}

# Format a table of header filenames and descriptions.

make_table()
{
    for FN in "$1/"*"/"*".h" ; do

        # Detect non-matching patterns.

        if [ ! -e "$FN" ] ; then
            break

        # Emit the header at the start.

        elif [ ! "$HEADER" ] ; then
            echo "|| '''Header File''' §|| '''Contents''' §||"
            HEADER=1
        fi

        PARENT=$(dirname "$FN")
        SECTION=$(basename "$PARENT")
        FILENAME=$(basename "$FN")

        echo -n '|| `'"$SECTION/$FILENAME"'` §|| '
        get_description "$FN"
        echo ' §||'
    done
}

make_table "$DN" | column -s '§' -t

# vim: tabstop=4 expandtab shiftwidth=4
