#!/usr/bin/perl

# $Id: //depot/cad/tools/1.0/icp4-1.1/bin/p4get#4 $
#
# 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);

MAIN: {

    my ($nflag, $cell, $file, $cmd, $islayout, $view);
    $Program = $FindBin::Script;

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

    if ($opt{h} || !$opt{c} || $#ARGV < 0) {
	print "Usage: $Program [-h] [-n] -c change_number cell+\n";
	print "   -h: help usage\n";
	print "   -n: show operation only\n";
	print "   -c: change number\n";
	print " cell: cell name(s)\n";
	exit;
    }
    $nflag = ($opt{n}) ? "-n" : "";
    if ($islayout = &Icp4::is_layout_view()) {
	$view = "layout";
    } else {
	$view = "logic";
    }

    foreach $cell (@ARGV) {
	if (-d $cell) {
	    open(FILES, "p4 files \@${opt{c}} |") or die "ERROR: cannot run \"p4 files\".";
	    $cmd = "p4 sync $nflag ";
	    while (<FILES>) {
		if (($file) = /^($view.views\/$cell\/\S+\#\d+)\s+/) {
		    $cmd = $cmd . " $file ";
		    next;
		}
		if (($file) = /^($view.views\/$cell\.\S+\#\d+)\s+/) {
		    $cmd = $cmd . " $file ";
		    next;
		}
	    }
	    print "INFO: Getting change $opt{c} of cell $cell ...\"\n";
	    system "$cmd\n";
	} else {
	    print "WARNING: $cell not found\n";
	}
    }

    exit 0;
}