#!/usr/bin/env perl

use strict;
use warnings;

use Cwd;
use File::Basename qw(basename);
use Getopt::Long qw(GetOptions);

sub usage {
    my $reason = shift;
    $reason .= "\n" if $reason && $reason !~ /\n$/s;
    (my $progname = $0) =~ s,.*[/\\],,;
    $reason .= "Usage: $progname [options...]" . <<'_EOSTR_';

The supported options are

    --help|-h|-?        print this message
    --verbose|-v        more noise when running
_EOSTR_
    die $reason;
}

sub showHelp {
    my($option) = @_;
    eval { usage(); };
    print $@;
    exit;
}

my $verbose;
Getopt::Long::Configure(qw(no_auto_abbrev));
GetOptions(
	   'help|h'		=> \&showHelp,
	   'verbose|v'		=> \$verbose,
	   ) || usage("Invalid options");

if (`p4 where` !~ m@^//depot/main/Camel/src/libs/scineplex/main/...\s@) {
    die "Usage: Run from the Camel/src/libs/scineplex/main directory";
}

my @depot_names = map {(split)[4]} split(/\n/, `p4 filelog ./... | grep 'branch from'`);
foreach my $depot_name (@depot_names) {
    my $depot_name_adj = $depot_name;
    $depot_name_adj =~ s/\#\d+(?:,\#\d+)*$//;
    my $local_name = basename($depot_name_adj);
    my $res = `p4 print -q $depot_name_adj | diff - $local_name`;
    if (length $res) {
	print "p4 integ $depot_name_adj $local_name\n";
	if ($verbose) {
	    print "$res\n";
	}
    }
}
