#!/usr/bin/perl

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

my %layouts = ();
my %schematics = ();
my %locked_layouts = ();
my %new_layouts = ();
my %new_schematics = ();
my %opt = ();

MAIN: {

    my $Program = $FindBin::Script;

    getopts('hDavj', \%opt);

    if ($opt{"h"}) {
	print "Usage: $Program [-h] [-a] [-v] [-j]\n";
	print "   -h: help usage\n";
	print "   -a: all users\n";
	print "       (default: just you)\n";
	print "   -v: verbose, show project, lib, group, and cell\n";
	print "       (default: show lib and cell)\n";
	print "   -j: print out p4ci hints for layouts; schematics not yet supported\n";
	print "       (default: not printed)\n";
	exit;
    }

    my $all = $opt{"a"} ? "-a" : "";

    # open (P4, "p4 opened $all ${P4HOME}/.../ic/...views/... |");
    # filter out only the attr files
    open (P4, "p4 opened $all ... | grep attr |");

    my ($key, $proj, $lib, $group, $view, $cell, $user);

    while (<P4>) {

	if (($proj, $lib, $group, $view, $cell, $user) = /.*\/(\S+)\.proj\/(\S+)\.lib\/(\S+)\.group\/(\S+)\.views\/([\$\w]+)\/.* by (\S+@\S+)/) {
	    $key = $proj . ":" . $lib . ":" . $group . ":" . $cell;
	    if ($view eq "logic") {
		if (! $schematics{$key}) {
		    $schematics{$key} = $user;
		}
		print "SCHEMATIC: $proj, $lib, $group, $cell editted by $user\n" if ($opt{"D"});
	    } elsif ($view eq "layout") {
		if (! $layouts{$key}) {
		    $layouts{$key} = $user;
		}
		print "LAYOUT: $proj, $lib, $group, $cell editted by $user\n" if ($opt{"D"});
	    } else {
		die "ERROR: Unknown view \"$view\" associated with proj=$proj, lib=$lib, group=$group, cell=$cell\n";
	    }
	    next;
	}

	if (($proj, $lib, $group, $view, $cell) = /.*\/(\S+)\.proj\/(\S+)\.lib\/(\S+)\.group\/(\S+)\.views\/([\$\w]+)\//) {
	    $key = $proj . ":" . $lib . ":" . $group . ":" . $cell;
	    if ($view eq "logic") {
		if (! $schematics{$key}) {
		    $schematics{$key} = "local";
		}
		print "$proj, $lib, $group, $view, $cell editted locally\n" if ($opt{"D"});
	    } elsif ($view eq "layout") {
		if (! $layouts{$key}) {
		    $layouts{$key} = "local";
		}
		print "$proj, $lib, $group, $view, $cell editted locally\n" if ($opt{"D"});
	    } else {
		die "ERROR: Unknown view \"$view\" associated with proj=$proj, lib=$lib, group=$group, cell=$cell\n";
	    }
	    next;
	}
    }

    close P4;

    if ($opt{"v"}) {
	print "Checked-out SCHEMATICS:\n";
	printf "    %-12s %-16s %-12s %-40s %-40s\n", "Project", "Library", "Group", "Cell", "Client";
	printf "    %-12s %-16s %-12s %-40s %-40s\n", "=======", "=======", "=====", "====", "======";
	foreach $key (sort keys %schematics) {
	    ($proj, $lib, $group, $cell) = split(":", $key);
	    printf "    %-12s %-16s %-12s %-40s %-40s\n", $proj, $lib, $group, $cell, $schematics{$key};
	}
	print "\nChecked-out LAYOUTS:\n";
	printf "    %-12s %-16s %-12s %-40s %-40s\n", "Project", "Library", "Group", "Cell", "Client";
	printf "    %-12s %-16s %-12s %-40s %-40s\n", "=======", "=======", "=====", "====", "======";
	foreach $key (sort keys %layouts) {
	    ($proj, $lib, $group, $cell) = split(":", $key);
	    printf "    %-12s %-16s %-12s %-40s %-40s\n", $proj, $lib, $group, $cell, $layouts{$key};
	}
    } else {
	print "Checked-out SCHEMATICS:\n";
	printf "    %-16s %-40s %-40s\n", "Library", "Cell", "Client";
	printf "    %-16s %-40s %-40s\n", "=======", "====", "======";
	foreach $key (sort keys %schematics) {
	    ($proj, $lib, $group, $cell) = split(":", $key);
	    printf "    %-16s %-40s %-40s\n", $lib, $cell, $schematics{$key};
	}
	print "\nChecked-out LAYOUTS:\n";
	printf "    %-16s %-40s %-40s\n", "Library", "Cell", "Client";
	printf "    %-16s %-40s %-40s\n", "=======", "====", "======";
	foreach $key (sort keys %layouts) {
	    ($proj, $lib, $group, $cell) = split(":", $key);
	    printf "    %-16s %-40s %-40s\n", $lib, $cell, $layouts{$key};
	}
    }

    if ($opt{"j"}) {

	# FIXME schematics not yet supported.

	my $first;
	open (P4, "p4local -W . 2>&1 |");

	while (<P4>) {
	    if (($cell) = /.*\/(\w+)\/.*\.lck/) {
		if (! $locked_layouts{$cell}) {
		    $locked_layouts{$cell} = 1;
		}
		print "LOCKED $cell\n" if ($opt{"D"});
		next;
	    }
	    if (($proj, $lib, $group, $cell) = /.*\/(\S+)\.proj\/(\S+)\.lib\/(\S+)\.group\/layout\.views\/([\$\w]+)\/.*\.attr/) {
		if (!$new_layouts{$cell}) {
		    $new_layouts{$cell} = 1;
		}
		print "NEW $proj, $lib, $group, $cell editted locally\n" if ($opt{"D"});
		next;
	    }
	}
	close P4;

	print "\np4ci Hints:\n";
	print   "==========\n";

	print "    Locked Layouts:\n";
	$first = 1;
	foreach $cell (sort keys %locked_layouts) {
	    if ($locked_layouts{$cell} == 1) {
		print "\t$cell\n";
	    }
	}
	print "\n";

	print "    Checked-out Layouts:\n";
	$first = 1;
	foreach $key (sort keys %layouts) {
	    ($proj, $lib, $group, $cell) = split(":", $key);
	    if ($layouts{$key} eq "local") {
		if ($first) {
		    print "\tp4ci $cell";
		    $first = 0;
		} else {
		    print " $cell";
		}
	    }
	}
	print "\n" if ($first == 0);
	print "\n";

	print "    New Layouts:\n";
	$first = 1;
	foreach $cell (sort keys %new_layouts) {
	    if ($new_layouts{$cell} == 1) {
		if ($first) {
		    print "\tp4ci $cell";
		    $first = 0;
		} else {
		    print " $cell";
		}
	    }
	}
	print "\n";
    }
}