#!/usr/bin/perl -- # -*- Perl -*-

use strict;

my $elemdir = "refpages/elements";

opendir (DIR, $elemdir);
while (my $name = readdir(DIR)) {
    next if $name =~ /^\.\.?$/;
    my $dir = "$elemdir/$name";
    next if ! -d $dir;
    next if ! -f "$dir/refentry.xml" || ! -f "$dir/synopsis.e.gen";
    checkAttr($dir);
}
closedir (DIR);

sub checkAttr {
    my $dir = shift;
    my $synopsis = "$dir/synopsis.e.gen";
    my $refentry = "$dir/refentry.xml";
    my %rattr = ();
    my %sattr = ();

    open (F, $refentry);
    read (F, $_, -s $refentry);
    close (F);

    if (/<refsec.*\scondition=.ref.elem.attrdesc.(.*?)<\/refsec/s) {
	$_ = $1;
	my @terms = m/<term>.*?<\/term>/sg;
	foreach my $term (@terms) {
	    $rattr{$1} = 1 if $term =~ /<term>(.*?)<\/term>/s;
	}
    }

    open (F, $synopsis);
    read (F, $_, -s $synopsis);
    close (F);

    my @rows = m/<row[^>]+role=[\'\"]attr[\'\"].*?<\/row>/sg;
    foreach my $row (@rows) {
	$sattr{$1} = 1 if $row =~ /.*?<entry[^>]*>(.*?)<\/entry/s;
    }

    my @keys = keys %rattr;
    foreach my $key (@keys) {
	if (exists($sattr{$key})) {
	    delete $rattr{$key};
	    delete $sattr{$key};
	}
    }

    return if (!%rattr && !%sattr);

    print "$dir:\n";
    print "\tRefentry: ", join(", ", sort keys %rattr), "\n" if %rattr;
    print "\tSynopsis: ", join(", ", sort keys %sattr), "\n" if %sattr;
    print "\n";
}
