#! /usr/local/bin/perl

use SDBM_File;

open (CAT, ">./categories");

@last_a = ('', '', '', '', '', '', '');

open (ENTITIES, "./entities");
dbmopen (%entities, "./dis-entities", 0644);
while (<ENTITIES>) {
	chop;
	next if /^\s*#/;
	s/\s*$//g;
	@a = split ('\,');
	$key = pack ("CCSCCCC",
		$a[0], $a[1], $a[2], $a[3], $a[4], $a[5], $a[6]);
	$j = 0;
	for ($i=0; $i<7; ++$i) {
		$j = index(substr($_, $j), ',') + $j + 1;
	}
	$entities{$key} = substr($_, $j);
	$entities{$key} =~ s/^\s+//g;
	$match = 1;
	for ($i=0; $i<7 && $match == 1; ++$i) {
		if ($a[$i] ne $last_a[$i]) {
			$match = 0;
		}
	}
	if ($match == 1) {
		print STDERR "Yow! duplicate \"$_\"\n";
	}
	if ($a[4] eq '0') {
		print $entities{$key}, "\n";
		print CAT $_, "\n";
	}

	@last_a = @a;

}
dbmclose (entities);
close (ENTITIES);

open (SITES, "./sites");
dbmopen (%sites, "./dis-sites", 0666);
while (<SITES>) {
    chop;
    if (/^#/) {
    }
    else {
	@a = split ('\,');
	$key = pack ("S", $a[0]);
	$sites{$key} = $a[1];
    }
}
dbmclose (sites);
close (ENTITIES);

