#!/usr/bin/perl
use strict;
use warnings;
use SVK::Test;
plan tests => 10;

# create remote repository
my ( $xd_remote, $svk_remote ) = build_test('remote');
our $output;
my ( $copath_remote, $coapath_remote ) = get_copath('remote');
my ( $repospath_remote, undef, $repos_remote ) = $xd_remote->find_repos('/remote/', 1);
$svk_remote->checkout('/remote/', $copath_remote);

# create tree structure
mkdir "$copath_remote/Project";
mkdir "$copath_remote/Project/A";
overwrite_file("$copath_remote/Project/A/foo", "foobar\n");
$svk_remote->add("$copath_remote/Project");
$svk_remote->commit('-m', 'project branch A', $copath_remote);

# create local repos
my ( $xd_local, $svk_local ) = build_test();
my ( $copath_mirror, $coapath_mirror ) = get_copath('mirror');
my ( $repospath_local, undef, $repos_local ) = $xd_local->find_repos('//', 1);

$svk_local->mkdir('-m', 'mirrored repos', '//mirror');
$svk_local->mkdir('-m', 'local branches repos', '//local');

# mirror the remote
$svk_local->mirror("file://$repospath_remote/Project", '//mirror/Project');
$svk_local->sync('//mirror/Project');
$svk_local->checkout('//mirror', $copath_mirror);

# 
my ( $copath_non_mirrored, undef ) = get_copath('non-mirrored');
$svk_remote->checkout('/remote/', $copath_non_mirrored);

# delete foo
$svk_local->delete("$copath_mirror/Project/A/foo");
$svk_local->commit('-m', 'deleted foo', "$copath_mirror/Project");

# make it a dir
$svk_remote->update($copath_remote);
ok ( !-e "$copath_remote/Project/A/foo", 'foo is gone' );
mkdir "$copath_remote/Project/A/foo";
overwrite_file("$copath_remote/Project/A/foo/bar", "hello world\n");
$svk_remote->add("$copath_remote/Project/A/foo");
$svk_remote->commit('-m', 'added dir foo and foo/bar', $copath_remote);
ok ( -e "$copath_remote/Project/A/foo", 'foo exists' );
ok ( -d "$copath_remote/Project/A/foo", 'foo is a dir' );
ok ( -e "$copath_remote/Project/A/foo/bar", 'foo/bar exists' );

# non-mirrored checkout path works just fine
$svk_remote->update($copath_non_mirrored);
ok ( -e "$copath_non_mirrored/Project/A/foo", 'foo exists' );
ok ( -d "$copath_non_mirrored/Project/A/foo", 'foo is a dir' );
ok ( -e "$copath_non_mirrored/Project/A/foo/bar", 'foo/bar exists' );

# mirrored checkout path should work, too
$svk_local->update('-s', "$copath_mirror/Project");
ok ( -e "$copath_mirror/Project/A/foo", 'foo exists' );
ok ( -d "$copath_mirror/Project/A/foo", 'foo is a dir' );
ok ( -e "$copath_mirror/Project/A/foo/bar", 'foo/bar exists' );

1;
