#!/usr/bin/perl -w
use strict;

#  Copyright (C) 2003 Anthony de Boer
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of version 2 of the GNU General Public License as
#  published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

my $gnd = $ENV{'GALLERY'};
die "No GALLERY in environment\n" unless $gnd;
my $DB = $gnd .'/.data';
my $thumbprefix = '.t_';

my %db;
my %pd;
my %pages;
open(DB, $DB) || die "Cannot read $DB: $!\n";
$/ = '';
while(<DB>) {
	my ($pg, $id, $rest) = split(/\s+/, $_, 3);
	next if $pg eq 'unsorted' && $id =~ /^\d+$/;
	$rest =~ s/\n+$//;
	die "DB entry $pg $id defined again!\n" if $db{"$pg $id"};
	$db{"$pg $id"} = $rest;
	next if $pg eq '_';
	$pages{$pg} = 0;
	$pd{$1} = $pg if $id =~ /^\d+$/ && $rest =~ /^(\S+)/;
	}
close(DB);

my @miscimg;

opendir(D, ".") || die "Cannot opendir .: $!\n";
for my $p (readdir(D)) {
	my $pg = $pd{$p};
	if (defined($pg)) {
		$pages{$pg} = 1;
		delete $pd{$p};
		}
	elsif ($p =~ /\.(jpg|jpeg|gif|png)/) {
		push(@miscimg, $p);
		}
	}
closedir(D);

my %pagemiss;
for my $i (sort (keys %pd)) {
	my $pg = $pd{$i};
	$pagemiss{$pg} = '' unless defined($pagemiss{$pg});
	$pagemiss{$pg} .= ' ' . $i;
	}

my @pagehave;
my @pagenot;
my @pagepart;
for my $pg (sort (keys %pages)) {
	if ($pages{$pg}) {
		if (defined($pagemiss{$pg})) {
			push(@pagepart, $pg);
			}
		else {
			push(@pagehave, $pg);
			}
		}
	else {
		push(@pagenot, $pg);
		}
	}

print join(" ", "Has", @pagehave), "\n\n" if $#pagehave > -1;
print join(" ", "Not", @pagenot), "\n\n" if $#pagenot > -1;
for my $pg (@pagepart) {
	print "$pg except", $pagemiss{$pg}, "\n";
	}
