#!/usr/bin/perl

# $Id: //depot/cad/tools/1.0/icp4-1.1/bin/p4local#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;
# use Cwd;

our (%opt, $Program);

#--------------------------------------------------------------------------

# get_lckfiles --
#	returns a list of *lck files
sub get_lckfiles {
    my($dir) = @_;
    my(@files) = ();

    open(FIND, "find $dir -name \*.lck\* -print |") or die "ERROR: cannot run find.";
    while (<FIND>) {
	chomp;
	push (@files, $_);
    }
    return(@files);
}


# get_icflist --
#	returns a list of files
sub get_icflist {
    my($dir) = @_;
    my(@files) = ();

    if ($opt{t}) {
	open(FIND, "find $dir -maxdepth 1 -type f -print |") or die "ERROR: cannot run find.";
    } else {
	open(FIND, "find $dir -type f -print |") or die "ERROR: cannot run find.";
    }
    while (<FIND>) {
	chomp;
	if ($opt{A}) {
	    # print;
	    push (@files, $_);
	} else {
	    if (&Icp4::is_icstudio_file($_)) {
		# print;
		push (@files, $_);
	    }
	}
    }
    return(@files);
}


MAIN: {

    # ($Program) = $0 =~ /([^\/]*)$/;
    $Program = $FindBin::Script;

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

    # -W is a hidden option

    if ($opt{h}) {
	print "Usage: $Program [-h] [-c] [-t] [-A] [-a] [-L log] [input]\n";
	print "   -h: help usage\n";
	print "   -c: skip checking\n";
	print "       (default: checking done)\n";
	print "   -t: top directory only\n";
	print "       (default: all subdirectories)\n";
	print "   -A: list all files unknown to perforce\n";
	print "       (default: list only icstudio files unknown to perforce)\n";
	print "   -a: add files into perforce\n";
	print "   -L: append to log file, for debugging\n";
	print "input: starting directory or file\n";
	exit;
    }

    # $dir = ($ARGV[0]) ? $ARGV[0] : ".";
    my $dir = ($ARGV[0]) ? $ARGV[0] : $ENV{'PWD'};

    if ($dir =~ /\$/) {
	$dir =~ s/\$/\\\$/g;
    }

    if ($opt{L}) {
	open(LOG, ">>$opt{L}") or die "ERROR: cannot create $opt{L}.";
    }

    $Icp4::debug = ($opt{D}) ? 1 : 0 ;

    my(%p4files);
    my($file);
    my(@files);

    if (!$opt{c}) {
	@files = &get_lckfiles($dir);
	if ($#files >= 0) {
	    if ($opt{W}) {
		print STDERR "WARNING: resolve these lock file(s):\n";
	    } else {
		print STDERR "ERROR: resolve these lock file(s):\n";
	    }
	    foreach $file (@files) {
		print STDERR "    $file\n";
	    }
	    if (! $opt{W}) {
		exit -1;
	    }
	}
    }

    @files = &get_icflist($dir);

    if ($opt{L}) {
	my ($hostname, $date, $pwd);
	$hostname = `hostname`;
	chop($hostname);
	$date = `date`;
	chop($date);
	$pwd = `pwd`;
	chop($pwd);
	print LOG "p4local:\n";
	#print LOG "    P4HOME = $ENV{P4HOME}\n";
	print LOG "    PWD    = $pwd\n";
	print LOG "    HOST   = $hostname\n";
	print LOG "    date   = $date:\n";
	print LOG "    find:\n";
	foreach $file (@files) {
	    print LOG "\t$file\n";
	}
	print LOG "    p4 add:\n";
    }

#    if ($opt{D}) {
#	foreach $file (@files) {
#	    print STDERR "DEBUG: $file\n";
#	}
#    }

    # list only icstudio files unknown to perforce

    # make sure p4 is configured
    if (&Icp4::where() eq 'error') {
	printf(STDERR "ERROR: Current directory not in client. Check P4 variables.\n");
	printf(STDERR "       No Perforce information available.\n");
	exit -1;
    }

    # get the p4 status of the files.
    %p4files = Icp4::fstat(@files);

    foreach $file (sort keys %p4files) {
#	if ($p4files{$file} eq '???') {
	if (($p4files{$file} eq '???') || ($p4files{$file} eq 'delete')) {
	    print "$file\n";
	    print LOG "\t$file\n" if ($opt{L});
	}
    }

    if ($opt{a}) {
	my($addfile) = $Program . ".added.$$";
	open(ADD, ">$addfile") or die "ERROR: cannot create $addfile.";
	foreach $file (sort keys %p4files) {
#	    if ($p4files{$file} eq '???') {
	    if (($p4files{$file} eq '???') || ($p4files{$file} eq 'delete')) {
		print ADD "$file\n";
	    }
	}
	close ADD;
	system "p4 -x $addfile add -f";
	unlink $addfile if (! $opt{D});
    }

    close ($opt{L}) if ($opt{L});
    exit 0;
}