#!/usr/bin/perl

# $Id: //depot/cad/tools/1.0/icp4-1.1/bin/p4correct#3 $
#
# CSWITCH CORPORATION CONFIDENTIAL PROPRIETARY
# 
# This file contains information which is the
# proprietary property of Cswitch Corporation.
# This file is confidential and its contents may
# not be disclosed without the expressed written
# consent of Cswitch Corporation.


use strict;
use warnings;

use FindBin qw($Bin);
use lib "$Bin/../lib";
# use Icp4;

use Getopt::Std;
use File::Basename;

# $P4HOME = &Icp4::p4client();

our (%opt, $Program);

MAIN: {

    my ($id);
    $Program = $FindBin::Script;

    getopts('hDn', \%opt);

    if ($opt{h} || $#ARGV < 0) {
	print "Usage: $Program [-h] [-n] id+\n";
	print "   -h: help usage\n";
	print "   -n: only display p4 revert commands without running\n";
	print "   id: p4 change number(s)\n";
	exit;
    }

    foreach $id (@ARGV) {
	correct($id);
    }
}

sub correct {
    my($id) = @_;
    my($f, $cmd);

    print "\nINFO: Correcting change # $id :\n";

    open (P4, "p4 submit -c $id 2>&1 |");

    while (<P4>) {
	# format:
	# open for read: /spare/tmp/ryu/p4/chips/griffin/1.0/ic/griffin.proj/ryu_lib.lib/default.group/layout.views/test_demo/test_demo.iccel_0:
	# No such file or directory
	if (($f) = /^open for read: (\S+): No such file or directory/) {
	    $cmd = "p4 revert $f";
	    print "$cmd\n";
	    if (! $opt{n}) {
		system "$cmd";
	    }
	}
    }
    close P4;
}