#! /bin/sh

# read a list of C files with inline XML usage given on the command line, and
# write a usage function on stdout

tmpf=`mktemp /tmp/gen-help.XXXXXX`
trap "rm -f $tmpf*" EXIT
quote() {
    sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' -e 's/^/"/' -e 's/$/\\n"/'
}

cat <<EOF
#include <stdio.h>
#include "utils.h"

void print_usage(void)
{
	fprintf(stderr,
"Usage: ctdb-test [options]\n"
"Options available:\n"
"\n"
EOF

for file in "$@"
do
    for line in `fgrep -n '/*** XML Argument:' < $file | cut -d: -f1`;
    do
	if [ -L tools/link-dtd ]; then

	    cat > $tmpf <<EOF
<?xml version="1.0"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD Docbook XML V4.1.2//EN"
"`pwd`/tools/link-dtd/docbookx.dtd">
<article>
EOF
	    tools/extract-help $file $line >> $tmpf
	    echo '</article>' >> $tmpf

	    tr '\n' ' ' < $tmpf | sed -e 's/[[:space:]]\{2,\}/ /g' |
		xsltproc tools/usage.xsl - | fold -w80 -s > $tmpf.txt

	    quote < $tmpf.txt
	else
	    # if we don't have docbook, just strip out the tags and grab
	    # the first two lines
	    tools/extract-help $file $line > $tmpf
	    sed 's/<arg [^>]*>/   /;s/<[^>]*>//g;' < $tmpf | head -3 | quote
	fi

    done
done

cat <<EOF
);
}
EOF
