#!/usr/local/bin/perl -w

=head1 NAME

p4.t - testing of vcp p4 i/o

=cut

use strict ;

use Carp ;
use File::Spec ;
use POSIX ':sys_wait_h' ;
use Test ;
use VCP::TestUtils ;

# use eq_or_diff from Test::Differences if available,
# else just use ok.
eval "use Test::Differences";
sub ok_or_diff {
   goto &eq_or_diff if defined &eq_or_diff;
   goto &Test::ok;
}


my @vcp = ( vcp_cmd, "vcp:-" );

my $t = -d 't' ? 't/' : '' ;

# what change number to start incremental export at
my $first_import_1_change;

## Repositories to read from
my $p4root_0 = "${t}p4root_0";
my $p4root_1 = "${t}p4root_1";

## where to put the destination repository
my $cvsroot = tmpdir "cvsroot";

my $state_location = tmpdir "vcp_state";

## where to write to in the destination repo
my $cvs_module = "p4_t_module";

my $cvs_spec = "cvs:$cvsroot:$cvs_module/";


my @tests = (
##
## p4->cvs->revml bootstrap
##
sub {
   eval {
      run \@vcp, \<<VCP_FILE;
Source:      p4:revml2p4\@$p4root_0://depot/foo/... --run-p4d

Destination: $cvs_spec --init-cvsroot --delete-cvsroot --state-location=$state_location

Map:
## ASSumes directories under //depot/foo/ are the main and branch
## dirs.
        */(...)<(*)>  \$1<\$2>
VCP_FILE
   };

   ok $@ || '', '';
},

sub {
   return skip "previous test failed", 1 if $@;

   my $infile  = $t . "test-cvs-in-0.revml" ;
   my $in = slurp $infile ;
   my $out = get_vcp_output $cvs_spec, qw( -r 1.1: ) ;

   s_content  qw( rep_desc time user_id ),   \$in, \$out ;
   s_content  qw( rev_root ),                \$in, $cvs_module ;
   rm_elts    qw( cvs_info change_id source_change_id mod_time ), \$in ;
   rm_elts    qw( label ), qr/vcp_.*/, \$out ;
#
#      $out =~ s{<rev_id>1.}{<rev_id>}g ;
#      $out =~ s{<base_rev_id>1.}{<base_rev_id>}g ;
#      $out =~ s{((id="|_id>)?[^>]*#)1\.}{$1}g;
#
   $in =~ s{(id="|_id>)/+ignored}{$1/$cvs_module}g;
#
#      $in =~ s{(<rev id=.*)main-branch-1/branched#(\d+)}{\1main/branched#1.2.

   ok_or_diff $out, $in;
},

# determine next change number that p4d will use this will become the
# change number of the first change in t/test-p4-in-1.revml when it is
# imported.
sub {
   my $p4_options = launch_p4d "p4_", { repo_dir => $p4root_0 };

   # get counter change
   run_p4 [ qw( counter change ) ],
      \undef,
      \$first_import_1_change,
      $p4_options ;

   chomp $first_import_1_change ;
   die "Invalid change counter value: '$first_import_1_change'"
      unless $first_import_1_change =~ /^\d+$/ ;
   ++$first_import_1_change ;

   $p4_options->{p4d_handle}->kill_kill;

   ok 1;
},



##
## p4->cvs->revml incremental export
##
sub {
   eval { run \@vcp, \<<VCP_END; } or die $@;
Options: -d .*

Source:       p4:revml2p4\@$p4root_1://depot/foo/...\@$first_import_1_change,#head --run-p4d

Destination:  $cvs_spec --state-location=$state_location

Map:
## ASSumes directories under //depot/foo/ are the main and branch
## dirs.
        */(...)<(*)>  \$1<\$2>
VCP_END

   ok 1;
},



sub {
   my $infile  = $t . "test-cvs-in-1.revml" ;
   my $in = slurp $infile ;
   my $out = get_vcp_output $cvs_spec, "-r", "ch_$first_import_1_change:" ;
   
   s_content  qw( rep_desc time user_id ),   \$in, \$out ;
   s_content  qw( rev_root ),                \$in, $cvs_module ;
   rm_elts    qw( cvs_info change_id mod_time ), \$in ;
   rm_elts    qw( label ), qr/vcp_.*/, \$out ;
   
#      $out =~ s{<rev_id>1.}{<rev_id>}g ;
#      $out =~ s{<base_rev_id>1.}{<base_rev_id>}g ;
#      $out =~ s{((id="|_id>)?[^>]*#)1\.}{$1}g;
#
   $in =~ s{(id="|_id>)/+ignored}{$1/$cvs_module}g;

   ok 0;
#   ok_or_diff $out, $in;
},

);  # end @tests.


plan tests => scalar @tests, todo => [ 5 ];

my $p4d_borken = $ENV{P4BORKEN}  || p4d_borken ;
my $cvs_borken = $ENV{CVSBORKEN} || cvs_borken;


my $why_skip ;
$why_skip .= "p4 command not found\n"  unless ( `p4 -V`  || 0 ) =~ /^Perforce/ ;
$why_skip .= "$p4d_borken\n"           if $p4d_borken ;
$why_skip .= "$cvs_borken\n"           if $cvs_borken ;

$why_skip ? skip( $why_skip, '' ) : $_->() for @tests ;
