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

# Script for cleaning up image directories created with copycf
# version 0.1
# 2007-02-12
# Copyright (C) 2007 Norman Walsh
# Released under the GPL license
# http://www.gnu.org/copyleft/gpl.html

# NO WARRANTY: This works for me. I'm paranoid about losing images, so
# I think it's pretty conservative. But I don't claim it'll work for
# you. Maybe it will. Maybe it won't. Maybe it'll lose images. Maybe
# it'll cause your hair to fall out. I don't know and I'm not
# responsible.

use strict;
use English;

my $usage = "Usage: $0 [dir]\n";

my $ROOT = shift @ARGV || ".";

my @dirs = ();
my %previews = ();
opendir (DIR, $ROOT);
while (my $name = readdir(DIR)) {
    next if $name =~ /^\.\.?$/;
    push (@dirs, $name) if -d $name && $name ne 'edited';
    next if ! -f $name;
    next if $name !~ /\.jpe?g$/;
    $name =~ s/\.[^\.]+$//;
    $previews{$name} = 1;
}
closedir (DIR);

my @del = ();
foreach my $dir (@dirs) {
    opendir (DIR, "$ROOT/$dir");
    while (my $name = readdir(DIR)) {
	my $file = "$ROOT/$dir/$name";
	next if ! -f $file;
	$name =~ s/\.[^\.]+$//;
	push (@del, $file) if ! exists $previews{$name};
    }
    closedir (DIR);
}

if (@del) {
    print "Archiving discarded photos...\n";
    system ("zip -mrp $ROOT/cleanup-photodir.$$.zip @del");
} else {
    print "Nothing to archive.\n";
}


