#!/usr/local/bin/perl
# Verify the following in the sample style sheet:
# 1) All properties are correct
# 2) All properties are represented
# Ian Jacobs - ij@w3.org
# $Id: chksample,v 1.5 2006-10-09 18:55:52 ihickson Exp $

# To use long names like $INPUT_RECORD_SEPARATOR.
use English;

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

if (!$ARGV[0] || !$ARGV[1]) {
    print "Usage: $PROGNAME style_sheet propertydb\n";
    exit 1;
} else {
    $style = $ARGV[0];
    shift;
    $propdbf = $ARGV[0];
    shift;
}

if (($_ = $ARGV[0], /^-e/) && $ARGV[1]) {
    shift;
    $elements = $ARGV[1];
    shift;
} else {
    $elements = "";
}

# regexps
$sp = "[ \t\n]*";		# whitespace
$propname = "[a-zA-Z-]+";
$propval = "[a-zA-Z0-9.-]+";
$patt = "$sp(.*?)$sp";
$blockst = "[/][*]";
$blocket = "[*][/]";

sub readdbfile {
    if (!open(DBASE, $_[0])) {
	print STDERR "$PROGNAME Error: Cannot open dbfile: $_[0]\n";
	return;
    }
    $dbbuf = "";
    while (<DBASE>) {
	$dbbuf .= $_;
    }
    $_ = $dbbuf;
    while (/$blockst$patt;;.*?$blocket/s) {
	$placeholder = $';
        # Initialize each property to "False"
	my @propnames = split(/\s+/, $1);
	foreach my $i (@propnames) {
	  $propdb{$i} = "False";
	}
	$_ = $placeholder;
    }
}

sub readstyle {
    if (!open(STYLE, $_[0])) {
	print STDERR "$PROGNAME Error: Cannot open style sheet: $_[0]\n";
	return;
    }
    $buf = "";

    $INPUT_RECORD_SEPARATOR="{";
    while ($buf = <STYLE>) {
	$INPUT_RECORD_SEPARATOR="}";
	unless (eof(STYLE)) {
	    $rule = <STYLE>;
	    $INPUT_RECORD_SEPARATOR="{";
	    $_ = $rule;
	    while (/$sp($propname)$sp:$sp($propval);?/) {
		$placeholder = $';
		$property = $1;
		if (! exists $propdb{$property}) {
		    print STDERR "Unknown: $property.\n";
		} else {
		    $propdb{$property} = "True";
		}
		$_ = $placeholder;
	    }
	}
    }
}

### main
@propdb = ();
readdbfile($propdbf);
readstyle($style);
foreach $key (sort %propdb) {
    if ($propdb{$key} eq "False") {
	print STDOUT "Unused: $key\n";
    }
}


