#!C:/Perl/bin

# Script to compare catalog files in local dev to local staging and copy newer ones or added ones to staging
#                Compares only down to subfolders but not subsubfolders. Skips texts folders so these need to be called explicitly such as kt/d/texts
use File::stat;
use File::Compare;
use File::Copy;
use Cwd;

# Development Home and Staging Home Locations 
$devHome = 'C:\wamp\tomcat\webapps\cocoon\texts\catalogs\xml\\'; # Change this to your local development repo's catalog\xml location
$stagingHome = 'C:\thl-repos\staging\texts\catalogs\xml\\'; # Change this to your local staging repo's catalog\xml location

$home = cwd();

if($#ARGV == -1 || $#ARGV > 0 || $ARGV[0] eq 'help') {           
	print "\n*************MoveModifiedFilesToStaging.pl***************\n";
	print "Description: \n\tMoves Kangyur Tengyur subfolder development files to staging \n";
	print "\tif there is a difference between them\n";
	print "Setup: \n\tYou must first set the variables \$devHome and \$stagingHome in this script  \n";
	print "\tto the absolute locations for your cocoon\\texts\\catalogs\\xml folders \n\ton Development and Staging respectively\n";
	print "Usage: \n\tMoveModifiedFilesToStaging.pl subdirectory-path\n";
	print "Examples: \n\tMoveModifiedFilesToStaging.pl kt\\q\n\tMoveModifiedFilesToStaging.pl kt\\d\\texts\n";
	print "Note: \n\tFor editions that have digitally input texts the text folder has to be done  \n";
	print "\tseparately from bibl records so run once with 'kt\\d' and once with 'kt\\d\\texts'.\n";
	print "Settings: \n\t \$devHome = $devHome \n\t \$stagingHome = $stagingHome\n";
	exit 0;
}

$startdir = shift(@ARGV); # first param is relative location of start folder in {repo}\texts\cocoon\catalogs\xml

# Calculate Absolute Source and Destination Directories.
$srcloc = $devHome . $startdir;
$destloc = $stagingHome . $startdir;

# Open source directory and change working directory to it
print "Source Dir: $srcloc \n";
opendir (SRCROOTDIR, $srcloc) or die "Cannot open source root dir, $srcloc: $!\n";
chdir $srcloc;

# Set counting variables
$fct = 0;
$cpct = 0;

# Iterate through source directory
while( my $item = readdir SRCROOTDIR ) {
	if(substr($item,0,1) ne ".") { # if item does not begin with "."
	$itempath = $srcloc . "\\" . $item;
		if( -d $itempath) { # if it is a directory that is not called "texts"
			print "Doing directory: $item\n"; # compare items in the directory
			$swd = $srcloc . "\\" . $item; 
			$dwd = $destloc . "\\" . $item;
			opendir(SRCDIR, $swd) or die "Cannot open specific source dir, $swd: $!\n";
			chdir $swd;
			while( my $file = readdir SRCDIR) {
				$res = compareFile($file, $swd, $dwd); # calls the compare function with file, source dir, and destination dir
				@cts = split(':',$res);
				$fct += $cts[0];
				$cpct += $cts[1];
			}
			closedir SRCDIR;
		} elsif( -f $itempath) { # if it's a file in the directory compare it
			print "Doing file:  $item\n";
			$res = compareFile($item, $srcloc, $destloc); # calls the compare function with file, source dir, and destination dir
			@cts = split(':',$res);
			$fct += $cts[0];
			$cpct += $cts[1];
		}
	}
}

# Print results and return "home"
print "\nTotal files scanned: $fct\n";
print "Total files copied: $cpct\n";
chdir $home;
# The End

# Compare File Subroutine
sub compareFile {
	my $file = shift @_; # first arg: file name
	my $srcdir = shift @_; # second arg: source directory
	my $destdir = shift @_; # third arg: destination directory
	$ct = 0;
	$ct2 = 0;
	if($file =~ /\.xml/) {
		$ct++;
		my $sfile = $srcdir . "\\" . $file; # absolute source file
		my $dfile = $destdir . "\\" . $file; # absoluet destination file
		if(!onStaging($file,$destdir) || compare($sfile,$dfile) != 0) { # if it doesn't compare or isn't on staging
			print "copying $sfile to $dfile\n";
			copy($sfile,$dfile) or die "Cannot copy $sfile to $dfile: $!\n"; # copy file
			$ct2++;
		} 
	}
	return $ct . ":" . $ct2;
}

# onStaging Subroutine: tests if file is on staging
sub onStaging {
	my $fn = shift @_; # file name
	my $ddir = shift @_;# destination dir on staging
	chdir $ddir; # change to that dir
	$resp = 0;
	if(-e $fn) { $resp = 1; } # test if file exists
	return  $resp; # 0 = doesn't exist; 1 = exists
}
