#!/usr/pkg/bin/perl
#
#    CleanupGallery v0.1 for BINS
#    Copyright (C) 2003 by Jochen Schaeuble <psionic@psionic.de>
#
#    $Id: bins_cleanupgallery,v 1.3 2003/04/08 18:29:46 jerome Exp $
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    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; see the file COPYING.  If not, write to
#    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#    Boston, MA 02111-1307, USA.

use File::Glob ':glob';

@knownImageExtentions = ("jpg", "jpeg", "gif", "png", "tiff",
			"bmp", "tga", "ps", "eps", "fit", "pcx",
			"miff", "pix", "pnm", "rgb", "im1", "xcf",
			"xwd", "xpm", "avs", "dcm", "dcx", "dib",
			"dps", "dpx", "epdf", "epi", "ept", "fpx",
			"icb", "mat", "mtv", "pbm", "pcd", "pct",
			"pdb", "ppm", "ptif", "pwp", "ras", "thm",
			);
			
$imageExts = join('|', @knownImageExtentions);

@webFormats = ("jpg", "jpeg", "gif", "png");
$webExts = join('|', @webFormats);


sub ProcessDir
{
	local *DIR;
	(local $curdir, local $dstdir, local $srcdir) = @_;

	opendir(DIR, "$dstdir/$curdir");
	while ($file = readdir(DIR)) {
		$file = "$curdir/$file";

		if (-d "$dstdir/$file" && $file !~ m/\.\.?$/ && $file !~ m/static\.\w*$/) {
			if (-d "$srcdir/$file") {
				# Directory exists in src and dest, process it
				&ProcessDir($file, $dstdir, $srcdir);
			} else {
				# Directory only available in dest remove it
				print "## Removing dir: $dstdir/$file\n";
				`rm -Ri \"$dstdir/$file\"`;
			}
		} elsif ((-f "$dstdir/$file" || -l "$dstdir/$file") &&
				$file =~ m/.*\.($webExts)$/i) {

			my @srcfiles = bsd_glob("$srcdir/$curdir/*",
						GLOB_NOCASE |
						GLOB_QUOTE |
						GLOB_TILDE);
			$filefound = 0;
			FILESEARCH:
			foreach $curfile (@srcfiles) {
				# remove trailing .xml
				if ($curfile =~ m/.*\.xml$/i) {
					$curfile =~ s/(.*)\.xml$/$1/i;
				}

				# remove file extension
				$curfile =~ s/.*\/([^\/]*)\.($imageExts)$/$1/i;
				if (($curfile ne "") && ($file =~ m/$curfile.*\.($webExts)$/i)) {
					$filefound = 1;
					last FILESEARCH;
				}
			}
			if ($filefound == 0) {
				$file =~ s/(.*)_\w+\.($webExts)$/$1/i;
				print "## Removing file: $dstdir/$file*\n";
				`rm -v \"$dstdir/$file\"*`;
			}
		}
	}
	closedir(DIR)
}

sub usage
{
	print "CleanupGallery v0.1 (c) 2003 by Jochen Schaeuble <psionic\@psionic.de\n";
	print "\n";
	print "This is free software with ABSOLUTELY NO WARRANTY.\n";
	print "See COPYING file for details.\n\n";
	print "Usage:\n\n";
	print "cleanupgallery <Gallery Source Directory> <Destination Directory>\n";
	print "\n";
	exit(0);
}

if ($#ARGV != 1 || !-d $ARGV[0] || !-d $ARGV[1]) {
	&usage;
}
($srcdir, $dstdir) = @ARGV;

ProcessDir("", $dstdir, $srcdir);
