#!/usr/bin/perl
# a script to test "svn:use-commit-times"

use FindBin;
use File::Spec;
use URI::file;
use File::Path qw(remove_tree);

# get current directory
my $rootDir = $FindBin::Bin;
$rootDir = File::Spec->rel2abs($rootDir);

# set command path to use special version of tools
my $svnmucc  = File::Spec->catfile( $rootDir, "svnmucc");
my $svn      = File::Spec->catfile( $rootDir, "svn");
my $svnadmin = File::Spec->catfile( $rootDir, "svnadmin");

# repository
my $repos  = File::Spec->catfile( $rootDir, "repos");

# working copy
my $wc1  = File::Spec->catfile( $rootDir, "wc1");
my $wc2  = File::Spec->catfile( $rootDir, "wc2");
my $wc3  = File::Spec->catfile( $rootDir, "wc3");

# File Path to repository URL
my $url = URI::file->new($repos);
print $url, "\n";

# remove directory to make sure
remove_tree( $repos );
remove_tree( $wc1 );
remove_tree( $wc2 );
remove_tree( $wc3 );

$cmd = "$svnadmin create $repos";
print "$cmd\n";
system $cmd;

if( $^O =~ /MSWin32/ )
{
	$script = <<SCRIPT;
exit 0
SCRIPT
	print "$script";
	open OUT, ">$repos/hooks/pre-revprop-change.bat";
	print OUT $script;
	close OUT;
}
else
{
	$script = <<SCRIPT;
#!/bin/sh
exit 0
SCRIPT
	print "$script";
	open OUT, ">$repos/hooks/pre-revprop-change";
	print OUT $script;
	close OUT;

	$cmd = "chmod +x $repos/hooks/pre-revprop-change";
	print "$cmd\n";
	system $cmd;
}

$cmd = "echo test   > test.txt";
print "$cmd\n";
system $cmd;

$cmd = "$svnmucc put test.txt $url/test.txt -m \"add test.txt\"";
print "$cmd\n";
system $cmd;

$cmd = "$svn co $url $wc1";
print "$cmd\n";
system $cmd;

$cmd = "$svnmucc propset \"svn:use-commit-times\" \"\" $url/test.txt -m \"set property\"";
print "$cmd\n";
system $cmd;

$cmd = "$svn propset --revprop svn:date -r 1 \"2001-01-01T02:03:04.392152Z\"  $url";
print "$cmd\n";
system $cmd;

$cmd = "$svn propset --revprop svn:date -r 2 \"2001-01-01T02:03:04.392152Z\"  $url";
print "$cmd\n";
system $cmd;

$cmd = "$svn co $url $wc2";
print "$cmd\n";
system $cmd;

$cmd = "$svn export $url $wc3";
print "$cmd\n";
system $cmd;

print_file_timestamp( File::Spec->catfile( $wc1, "test.txt" ) );
print_file_timestamp( File::Spec->catfile( $wc2, "test.txt" ) );
print_file_timestamp( File::Spec->catfile( $wc3, "test.txt" ) );

unlink File::Spec->catfile( $wc2, "test.txt" );

$cmd = "$svn update $wc2";
print "$cmd\n";
system $cmd;

print_file_timestamp( File::Spec->catfile( $wc2, "test.txt" ) );

sub print_file_timestamp
{
	my $file = shift;
	my ($sec, $min, $hour, $day, $mon, $year) = localtime((stat("$file"))[9]);
	$year = $year + 1900;
	$mon  = $mon + 1;
	printf "$file: %04d/%02d/%02d %02d:%02d:%02d\n", $year, $mon, $day, $hour, $min, $sec;
}
