#!/usr/bin/perl
#
#  program to delete image files by moving them to a temp directory
#
use File::Basename;
$dir = "../2*";
$garbage = "/cygdrive/c/htdocs/birds/garbage";
{
	while (){
		%files = ();
		print "Type in file name:";
		$in = <STDIN>;
		chomp($in);
		exit if (!$in);
		if ($in =~ /^n/) {
			print "exit\n";
			exit;
		}
		$name = '"*' . $in . '*"';
		@array = `find $dir -name $name`;
		if (!@array) {
			print "File not found: $in\n";
		}
		&create_hash;
		$number = keys %files;
		print "Number of directories = $number\n";
		while (($k, $v) = each %files) {
			print "Delete for $k:";
			$ans = <STDIN>;
			chomp ($ans);
			if ($ans =~ /y/) {
			   @f = split/:/, $files{$k};
			   foreach  $ff  (@f) { 
			       chomp ($ff);
			       if ($ff =~ /Full/) {
			            $i = `mv $ff $garbage`;
			       	    print "mv $ff $garbage\n";
			       }else{
				    $i = `rm $ff`;
			       	    print "rm $ff\n";
			       }
			       if ($i) {
				       print "Failed: mv $ff $garbage\n";
			       }
			   }
			}
                }
	}
}
sub create_hash {
#  I want only the year and month for the hash key

	foreach (@array) {
		@items = split (/\//);
			# put in "..", year and month
		$key = $items[1] . "/" . $items[2];
		$files{$key} = $files{$key} . $_  . ":";
	}
}
