#!/usr/bin/perl -w
# perform functions on new files to be used under rcs.
# usage: [options] [files ...] [-Xstyle files ...]
# for each arg
#     if option is -Xstyle
#         set keyword style based on X and continue
#     if arg is a directory
#        explode dir back into arg list and continue
#     if no keyword style has been set
#	  imply keyword style from filename extension
#     if no keywords already in file
#         append appropriate RCS boilerplate yet retain each file's mod date;
#     if -ci
#         perform an initial rcs checkin too (and leave a copy if -cu)

# just print usage if no args
&usage() if (@ARGV == 0);

# set the boilerplate common for all keyword string types
$dne = "For RCS Only -- Do Not Edit";
$ids = "@(#) \$RCSfile\$ \$Date\$ \$Revision\$ \$Name\$";

# set several benign forms for keyword strings for different basic file types
$cid = "/* $dne */\nstatic char *rcsid[2] = {(char *)rcsid, \"$ids\"};";
$hid = "/* $dne\n * $ids\n */";
$pid = "# $dne\n# $ids";
$wid = "<! $dne >\n<! $ids >";
$bid = "! $dne\n! $ids";
$xid = "XCOMM $dne\nXCOMM $ids";

# no fixed id unless see -Xstyle arg
$setid = undef;

# scan each arg (option, file or dir)
while ($_ = $ARGV[0]) {
    shift;

    if (/^-/) {
	# crack option
	OPSWITCH: {
	/-cstyle/	&& do { $setid = $cid;  last OPSWITCH; };
	/-hstyle/	&& do { $setid = $hid;  last OPSWITCH; };
	/-pstyle/	&& do { $setid = $pid;  last OPSWITCH; };
	/-wstyle/	&& do { $setid = $wid;  last OPSWITCH; };
	/-bstyle/	&& do { $setid = $bid;  last OPSWITCH; };
	/-xstyle/	&& do { $setid = $xid;  last OPSWITCH; };
	/-nostyle/	&& do { $setid = undef; last OPSWITCH; };
	/-c[iu]/	&& do {
	    # toggles
	    if ($ciargs) {
		$ciargs = undef;
	    } else {
		# basic ci options: quiet; initial; file's date; name; title
		$ciargs = "-q -i -d -nRev0 -t-\"Initial checkin\"";
		# append flag to leave copy checked out if see -cu
		$ciargs .= " -u" if (/-cu/);
	    }
	    last OPSWITCH;
	    };
	&usage();
	}

	# next arg
	next;
    }

    # explode a dir into files and continue
    if (-d) {
	chomp (@tmp = (`find $_ -type f -print`));
	@ARGV = (@tmp, @ARGV);
	next;
    }

    # everything should be a file by now
    if (! -f) {
	print "NonFile: $_\n";
	next;
    }

    # silently skip existing RCS files
    next if (/.*,v/);

    # set id to keyword style: use permanent if set, else divine from suffix
    if ($setid) {
	$id = $setid;
    } else {
	# imply keyword style from suffix
	$id = undef;
	IDSWITCH: {
	/Imakefile$/		&& do { $id = $xid; last IDSWITCH; };
	/Makefile$/		&& do { $id = $pid; last IDSWITCH; };
	/Makefile.smple$/	&& do { $id = $pid; last IDSWITCH; };
	/Make\..*/		&& do { $id = $pid; last IDSWITCH; };
	/README$/		&& do { $id = $bid; last IDSWITCH; };
	/INSTALL$/		&& do { $id = $bid; last IDSWITCH; };
	/\..*html$/		&& do { $id = $wid; last IDSWITCH; };
	/\.ad$/			&& do { $id = $bid; last IDSWITCH; };
	/\.awk$/		&& do { $id = $pid; last IDSWITCH; };
	/\.c$/			&& do { $id = $cid; last IDSWITCH; };
	/\.cfg$/		&& do { $id = $bid; last IDSWITCH; };
	/\.csh$/		&& do { $id = $pid; last IDSWITCH; };
	/\.edb$/		&& do { $id = $pid; last IDSWITCH; };
	/\.h$/			&& do { $id = $hid; last IDSWITCH; };
	/\.hlp$/		&& do { $id = $pid; last IDSWITCH; };
	/\.pl$/			&& do { $id = $pid; last IDSWITCH; };
	/\.sh$/			&& do { $id = $pid; last IDSWITCH; };
	/\_db$/			&& do { $id = $pid; last IDSWITCH; };
	/\_sites$/		&& do { $id = $pid; last IDSWITCH; };
	}

	if (!$id) {
	    print "Unknown: $_\n";
	    next;
	}
    }

    # now $_ contains the file to effect, $id the keyword style to use.

    # append RCS keywords, retaining file mod date, if none already
    if (system "grep -q RCSfile $_") {
	($x,$x,$x,$x,$x,$x,$x,$x,$at,$mt,$x) = stat();
	open FH,">>$_" and print FH "\n$id\n" and close FH and utime $at,$mt,$_
						or print "editErr: $_ $!\n";
    }

    # checkin if desired, making RCS dir if necessary
    if ($ciargs) {
	$dir = $_; $dir = "." if (!($dir =~ s#/[^/]*$##));
	(-d "$dir/RCS" or mkdir "$dir/RCS",0744) and !system "ci $ciargs $_"
			    and print "Checkin: $_\n" or print "ciError: $_\n";

    }
}

exit 0;

# print usage message and exit.
sub usage {
    print "Usage: [files|dirs...] [-?style files|dirs...]\n";
    print "Purpose: add keywords to, and optionally check in, new RCS files.\n";
    print "Options:\n";
    print "  -ci: go ahead and perform the ci (toggles on and off)\n";
    print "  -cu: like ci but leaves unlocked copy behind\n";
    print "  -cstyle:  force .c style:         static char rcsid[] = ... \n";
    print "  -hstyle:  force .h style:         /* .. */\n";
    print "  -pstyle:  force pound style:      # ...\n";
    print "  -wstyle:  force html style:       <! ... >\n";
    print "  -bstyle:  force bang/text style:  ! ...\n";
    print "  -xstyle:  force Imakefile style:  XCOMM ...\n";
    print "  -nostyle: return to auto style mode\n";
    print "Options can be anywhere and apply to files to their right until\n";
    print "changed. Keywords are not added if already present. Directories\n";
    print "are exploded in-line as encountered. Style of comments for\n";
    print "keywords is divined automatically from filename or can be forced.\n";

    exit 1;
}

# For RCS Only -- Do Not Edit
# @(#) $RCSfile: rcsnew,v $ $Date: 2003/04/15 20:47:55 $ $Revision: 1.1.1.1 $ $Name:  $
