#!/usr/bin/perl

# $Id: //depot/cad/tools/1.0/icp4/bin/p4ci#8 $
#
# 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;

our (%opt, $Program);
our $attr_sfx = ".Ic_cell_template.attr";
our $cell_sfx = ".iccel_[0-9]+";

our $temp_file = "/var/tmp/p4verify.$$";

MAIN: {

    my $nflag;
    my ($cell, $cells);
    my ($cell_attr, $ic_pat);
    my ($openStat, $expectedStat);

    $Program = $FindBin::Script;

    getopts('hDnik:', \%opt);

    if ($opt{h} || $#ARGV < 0) {
	print "Usage: $Program [-h] [-i] cell+\n";
	print "   -h: help usage\n";
	print "   -i: ignore pending changes (dangerous!)\n";
	print "   -n: show operation only\n";
	print " cell: cell name(s)\n";
	print "This program checks that your .attr is not different from the head of the tree.\n";
	exit;
    }

    $nflag = "-n" if ($opt{n});


    $cells = "";

    foreach $cell (@ARGV) {

	if (-d $cell) {
	    $cell_attr = $cell . $attr_sfx;
	    $ic_pat    = $cell . $cell_sfx;

	    if (-f "$cell/$cell_attr") {
		$openStat = &Icp4::openStat("$cell/$cell_attr");
		$expectedStat = &Icp4::p4_stat_constant("NOT_OPENED");
		if ($opt{D}) {
		    print "##  open stat=$openStat  expected $expectedStat\n";
		}
		
		if ($openStat != &Icp4::p4_stat_constant("NOT_OPENED")) {
		    print "### File $cell/$cell_attr may be opened by someone\n";
		    print "###  \&Icp4::openStat returned code: $openStat. Skipping.\n";
		    next;
		}

		if ($opt{n}) {
		    print "p4 print -q -o $temp_file $cell/$cell_attr; diff -q $cell/$cell_attr $temp_file\n";
		} else {
		    system("p4 print -q -o $temp_file $cell/$cell_attr; diff -q $cell/$cell_attr $temp_file");
		}
		

	    } else {
		print "### $cell directory does not have file $cell_attr\n";
		next;
	    }
	}
    }

    unlink $temp_file;

    exit 0;
}