#!/usr/bin/perl

my $cmd = "convert";
my $switches = "-auto-orient -resample 15 -quality 60";
my $run = "$cmd $switches ";

&main;
sub main {
	# Make sure the path has a trailing / or \
	my $from = "/cygdrive/C/Documents and Settings/Admin/Desktop/New Pics/"; 
	my $to = "/cygdrive/C/thumbs/";
	
	my $skipExisting = 1;
	my $suppressSkippedMsg = 1;
	my $suppressConvertMsg = 0;
	my $suppressDirMsg = 0;

	doDir($from, $to, $skipExisting, $suppressSkippedMsg, $suppressConvertMsg, $suppressDirMsg);
	print "\n*****************\n*****  DONE!  *****\n*****************\n";
}

sub doDir {
	my($from, $to, $skipExisting, $suppressSkippedMsg, $suppressConvertMsg, $suppressDirMsg) = @_;
	opendir DIR, $from;
	local @dirList = readdir(DIR);
	closedir DIR;
	mkdir $to;
	
	foreach (@dirList) {
		#print "$_\n"; 
		# Skip hidden files, . and .. i.e. anything starting with a '.'
		next if (m/^\./);

		# Get only file name; no path data
		# $_ =~ m/^.*\/(.*)$/;
		$input = $from . $_;
		# print "$input\n";
		$out = $to . $_;

		# Get extension
		m/\.(.*?)$/;
		# Only process jpg or jpeg and check its a normal file
		next if ( ( ! (lc $1 eq "jpg" || lc $1 eq "jpeg") ) && -f $input );
		
		
		if (-f $out && $skipExisting) {
			print "Skipping $out - already exists\n" if (! $suppressSkippedMsg);
		} elsif (-f $input) {
			print "Converting to $out\n" if (! $suppressConvertMsg);
			system("$run \"$input\" \"$out\"");
		} elsif (-d $input) {
			print " -> $from$_\n" if (! $suppressDirMsg);
			doDir ($from . $_ . "/", $to . $_ . "/", $skipExisting, $suppressSkippedMsg, $suppressConvertMsg, $suppressDirMsg);
		}
	}
}
