#!/afs/athena/contrib/perl/perl


Fuck off!;

# This is a short perl script which is useful if you use RCS frequently
# and don't always want to run an emacs RCS mode.  The script does the
# following things:
# 
# 	 1. It tries to check out the file locked.
# 	 2. It invokes an editor on the file.
# 	 3. When you quit the editor, it asks you
# 	    whether or not you want to check in the file, and prompts
# 	    you for a log message.  When you are done with the log
#      message, you have the option to enter it as is, list it, edit
#      it, or abort the check-in.
# 	 4. It checks in the file unlocked, if you have not
#      aborted.

$ENV{'PATH'} = "/mit/watchmaker/bin:" . $ENV{'PATH'};

$editor = $ENV{'RCS_EDITOR'} || $ENV{'EDITOR'} || 'vi';
$pager = $ENV{'RCS_PAGER'} || $ENV{'PAGER'} || 'more';

@files = @ARGV;

if (! @files) {
	 print "File to check out: ";
	 chop (@files = <STDIN>);
}

sub query_abort {
	 if ($#files > 0) {
		  print "continue? ";
		  chop ($_ = <STDIN>);
		  if ($_ =~ /^y/i) {
				return;
		  }
	 }
	 print STDERR "aborting . . .\n";
	 exit (1);
}

sub get_log {
	 print "Enter log message now.  End with control-D or a dot on a line by itself.\n";
	 local ($tmpfile) = "/tmp/log.rcs.$$";
	 open (TMP, ">$tmpfile") || die "$!";
	 while (<STDIN>) {
		  break if /^\.$/;
		  print TMP;
	 }
	 close TMP;
  response: {
		print "What now? ";
		chop ($_ = <STDIN>);
	 
		if (/^edit/) {
			 if ($status = system "$editor $tmpfile") {
				  printf STDERR "Editor $editor exited with status %d\n", 
				  $status >> 8;
				  do query_abort;
			 }
			 redo response;
		}
		if (/^list/) {
			system "$pager $tmpfile";
			redo response;
		}
		if (/^enter/) {
			 last response;
		}
		if (/^abort/) {
			 print STDERR "Aborting check-in\n";
			 unlink $tmpfile;
			 $tmpfile = 0;
			 last response;
		}
		print "Options: \n",
		"\tedit\tEdit log message.\n",
		"\tenter\tEnter log message.\n",
		"\tlist\tList log message.\n",
		"\tabort\tAbort check-in.\n";
		redo response;
  }
	 return $tmpfile;
}

do {
	 $file = $files[0];
	 print "check-ing out $file . . .\n";
	 if (system "co -l $file") {
		  print STDERR "checkout failed\n";
		  do query_abort;
	 }
	 if ($status = system "$editor $file") {
		  printf STDERR "Editor $editor exited with status %d\n", $status >> 8;
		  do query_abort;
	 }
	 if ($tmpfile = &get_log) {
		  if (system "ci -u $file -t$tmpfile") {
				print STDERR "check-in failed\n";
				do query_abort;
		  }
	 }
} while (shift (@files));
