#!/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 $DB = '.data';
my $DBNEW = '.data.new';
my $DBOLD = '.data.bak';
my $thumbprefix = '.t_';

my %nuke;

for my $ino (@ARGV) {
	my $iname = "img_" . $ino . ".jpg";
	my $tname = $thumbprefix . $iname;
	$nuke{$iname} = 1;
	$nuke{$tname} = 1;
	}

unlink (keys %nuke);

open(DB, $DB) || die "Cannot read $DB: $!\n";
open(DBNEW, ">$DBNEW") || die "Cannot write $DBNEW: $!\n";
$/ = '';
while(<DB>) {
	my ($pg, $id, $rw1, $rest) = split(/\s+/, $_, 4);
	next if defined($id) && $nuke{$id};
	next if defined($rw1) && $nuke{$rw1};
	die "Cannot write $DBNEW: $!\n" unless print DBNEW;
	}
close(DB);
die "Cannot close $DBNEW: $!\n" unless close(DBNEW);
rename($DB, $DBOLD) || die "Cannot rename $DB to $DBOLD: $!\n";
rename($DBNEW, $DB) || die "Cannot rename $DBNEW to $DB: $!\n";
exit(0);
