#!/usr/local/bin/perl
# Index of descriptor names.
# Arnaud Le Hors lehors@w3.org/Ian Jacobs ij@w3.org
# $Id: mkdescidx,v 2.5 2006-10-09 18:55:52 ihickson Exp $
use English;


if (($_ = $ARGV[0], /^-r/) && $ARGV[0]) {
    shift;
    $realpath = $ARGV[0];
    shift;
} else {
    $realpath = ();
}

$PROGNAME = substr($0, rindex($0, "/") + 1);

if (!$ARGV[1]) {
    print "Usage: $PROGNAME [-r realpath] indexfile dbase\n";
    exit 1;
} else {
    $indexf = $ARGV[0];
    $dbasef = $ARGV[1];
}

### main

# compute path relative to index file
$root = "";
@elems = split("/", $indexf);
pop @elems;			# get rid of the file itself
foreach (@elems) {
    $root .= "../";
}

# copy file in memory
sub readdb {
    if (!open(DBASE, $_[0])) {
	die "$PROGNAME Error: Cannot open file: $_[0]\n";
    }
    $INPUT_RECORD_SEPARATOR="/*";
    $buf = "";
    $indexes = ();
    while ($buf = <DBASE>) {
	$INPUT_RECORD_SEPARATOR="*/";
	unless (eof(DBASE)) {
	    $entry=<DBASE>;
	    $INPUT_RECORD_SEPARATOR="/*";
	    if ($entry =~ /((.*?);;.*)\*\//sio) {
		$data = $1;
		$key = $2;
		# Newlines and tabs-> space
		$data =~ s/[\n\t]+/ /gs;
		$indexes{$key} = $data;
	    } else {
		warn "Bad syntax: $entry\n";
	    }
	}
    }
    close(DBASE);
}

sub cleanup {
    # Put in right HTML markup
    $arg = $_[0];
    $arg =~ s/^[ \t\n]+//gs;
    $arg =~ s/[ \t\n]+$//gs;
    $arg =~ s/[&]/\&amp\;/gs;
    $arg =~ s/[<]/\&lt\;/gs;
    $arg =~ s/[>]/\&gt\;/gs;
    $arg =~ s/\{([^\}]*)\}([^{]*)\{\}/<a href=\"$1\">$2<\/a>/g;
    return "$arg";
}

# Descriptor database entries between "/*" and "*/"

sub format_name {
    $name = cleanup($_[0]);
    print OUTPUT "<td><span class=\"descinst-" . $name . " xref\">'" . $name . "'<\/span>\n";
}


sub format_val {
    # Replace <n> by link to value def of n
    # Replace <'m'> by link to descriptor 'm'
    $value = $_[0];
    $value =~ s/^[ \t\n]+//gs;
    $value =~ s/[ \t\n]+$//gs;
    $value =~ s/\<([^']+?)\>/<span class=\"value-inst-\1\">\&lt\;\1\&gt\;<\/span>/sgi;
    $value =~ s/\<\'(.+?)\'\>/<span class=\"descinst-\1\">\'\1\'<\/span>/sgi;
    print OUTPUT "<td>$value\n";
}

sub format_init {
    print OUTPUT "<td>" . cleanup($_[0]) . "\n";
}

#sub format_media {
#    # If just visual, replace by space.
#    $fmedia = cleanup($_[0]);
#    $fmedia =~ s/^visual$/&nbsp;/;
#    print OUTPUT "<td>$fmedia\n";
#}

sub table_head {
    print OUTPUT "<table border=1 align=center>\n";
    print OUTPUT "<thead>";
    print OUTPUT "<tr align=center>";
    print OUTPUT "<th>Name";
    print OUTPUT "<th>Values";
    print OUTPUT "<th>Initial value";
    print OUTPUT "</thead>";
    print OUTPUT "\n";
}

sub table_body {
    foreach $key (sort (keys %indexes)) {
	($name, $val, $init, $media) = split(/;;/, $indexes{$key});
	print OUTPUT "<tr>";
	format_name($name);
	format_val($val);
	format_init($init);
	print OUTPUT "\n";
    }
}

sub table_foot {
    print OUTPUT "</table>\n";
}

# main
readdb($dbasef);
open(OUTPUT, "> $indexf");
table_head();
table_body();
table_foot();
close(OUTPUT);
close(DBASE);
