BEGIN {
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
}


use strict;
use util;


my $source_directory = "Z:\\";
my $target_directory = "C:\\mdewsnip\\CIC\\Images.new\\";


sub main
{
    # Find all the files in the source directory
    if (!opendir(DIR, $source_directory)) {
	die "Error: Couldn't read directory $source_directory\n";
    }
    my @directory_files = readdir(DIR);
    closedir(DIR);

    foreach my $directory_file (@directory_files) {
	next if ($directory_file eq "." || $directory_file eq "..");

	my $directory_file_path = &util::filename_cat($source_directory, $directory_file);
	print "$directory_file_path ";
	if (-f $directory_file_path) {
	    print "(File)\n";
	}
	else {
	    print "(Directory)\n";
	    my $images_revised_directory = &util::filename_cat($directory_file_path, "images REVISED");
	    if (-e $images_revised_directory) {
		&copy_directory_to_local_drive($images_revised_directory);
	    }
	}
    }
}


sub copy_directory_to_local_drive
{
    my ($directory_file_path) = (@_);

    my $destination_file_path = $directory_file_path;
    my $source_directory_regexp = $source_directory;
    $source_directory_regexp =~ s/\\/\\\\/g;
    $destination_file_path =~ s/^$source_directory_regexp/$target_directory/;
    print " Destination: $destination_file_path\n";

    my $destination_directory_path = $destination_file_path;
    $destination_directory_path =~ s/[^\\]+$//;
    if (!-e $destination_directory_path) {
        &util::mk_all_dir($destination_directory_path);
    }

    &util::cp_r($directory_file_path, $destination_directory_path);
}


main(@ARGV);
