#!/usr/bin/perl -Pdw
#
#    ciall [-c "Checkin comment string"] [file]...
#
# Check in all the files that are in the ClearCase directory.  If  the
# caller  doesn't  give  us  a  list  of files on the command line, we
# attempt to check in everything in the current directory.

require "checkedout.pl";
#include "dbg.pl";

$V = $ENV{"VOB"} || "/vobs/nm/cmagent";
$S = $ENV{"COB"} || "/vobs/nm/cmagent/src";
@d = ("$V/src", "$V/doc", "$V/sh",);
$C = "-c 'No comment'";	# Checkin comment.
&checkedout();			# Get checked-out list.

for ($i = 0; $i < $#ARGV; $i++) {
	$a = $ARGV[$i];
	D "Arg $i: \"", $a, "\"" D1;
	if ("$a" eq '-c') {
		$C = "-c '" . $ARGV[$i+1] . "'";
		splice(@ARGV, $i, 2);
		--$i;
		D "Comment \"$C\"" D1;
	} else {
		D "Arg $i: \"$a\" ignored." D4;
	}
}
@ARGV = split(/\s/,`ls *.[bht] Makefile`)
#ARGV = split(/\s/,`ls *`)
	if (!@ARGV);	# Remaining args are file list.
file: for $f (@ARGV) {	# One file at a time ...
	D "File: $f" D2;
	dir: for $d (@d) {
		$F = "$d/$f";			# Name of possible ClearCase element.
		D "Path: $F" D2;
		if ($f && -f $F ) {		# Is file in the ClearCase src directory?
			D "File: $S/$f exists." D2;
			if ($checkedout{$F}) {
				D "File: $f checked out; check it in ..." D2;
				$cmd = "cleartool ci -ide $C $F";
				if (system $cmd) {
					D "File: $f checkin command returned $?." D2;
					$checkedout{$F} = 0;
				} else {
					D "File: $f checked in." D1;
				}
			} else {
				D "File: $f not checked out." D2;
			}
			next file;
		}
	}
	D "File: $f not an element; mkelem." D2;
	$cmd = "cleartool mkelem $C $f";
	system $cmd;
	$cmd = "cleartool ci -ide $C $f";
	system $cmd;
}
