#!/usr/athena/bin/perl
# $Id: ledit,v 1.24 2010/01/08 16:45:09 ngb Exp $

$D = "../data/";
if ($ENV{'HOST'} =~ m/^(penguin-lust|rtfm)/) {
    $RCSPREFIX = "/usr/athena/bin/";
} else {
    $RCSPREFIX = "/afs/athena.mit.edu/project/gnu/sun4bin/g";
}
$long = undef;
$check = undef;
$usage = "Usage: $0 [ -c ] [ -l ]\n";

$ENV{'PATH'} = $ENV{'PATH'} . ":/usr/athena/bin";

die "Must set EDITOR environment variable to use ledit."
    if (!$ENV{'EDITOR'});

while ($_ = $ARGV[0], /^-/) {
    shift;
    if (/^-l$/) {
	$long++;
	next;
    }
    elsif (/^-c/) {
	$check = "-check";
	next;
    }
    else {
	die "Unknown option $_.\n$usage";
    }
}

die $usage if (@ARGV);

chdir("/afs/sipb.mit.edu/project/periodic-postings/src") ||
    die "chdir(/afs/sipb.mit.edu/project/periodic-postings/src):\n\t$!.\n";

if (system("(cd ${D}; ${RCSPREFIX}co -l -q list)")) {
    open(RLOG, "${RCSPREFIX}rlog -h ${D}list|") || die "Executing grlog: $!.\n";
    { local($/) = undef; $rlog = <RLOG>; }
    close(RLOG) || die "Closing grlog: $!.\n";
    die "Grlog exited with non-zero status.\n" if ($?);
    if ($rlog =~ /\nlocks: strict\n\s+$ENV{'USER'}:/ ||
	$rlog =~ /\nlocks:\s+$ENV{'USER'}:/) {
	print STDERR "The database appears to already be locked by you.  Edit it? ";
	die "EOF on stdin\n" if (! defined($_ = <STDIN>));
	die "Aborting because check-out failed.\n" if (! /^[Yy]/);
	if (-f "${D}list.long") {
	    if ((-M "${D}list.long") < (-M "${D}list")) {
		if ($long) {
		    print STDERR "Use existing merged \"list.long\" file? ";
		    die "EOF on stdin\n" if (! defined($_ = <STDIN>));
		    goto edit if (/^[Yy]/);
		}
		else {
		    print STDERR" A merged \"list.long\" file already exists.  Do you want to switch into long\n(-l) mode and edit it (if you say no, it will be ignored)? ";
		    die "EOF on stdin\n" if (! defined($_ = <STDIN>));
		    if (/^[Yy]/) {
			$long++;
			goto edit;
		    }
		}
	    }
	    unlink("${D}list.long.old");
	    rename("${D}list.long", "${D}list.long.old");
	}
    }
    else {
	die "Aborting because check-out failed.\n";
    }
}

if ($long) {
    print STDERR "Merging main and auxiliary database files...\n";
    die "Aborting because database merge failed.\n"
	if (system("./merge-db.pl -out ${D}list.long ${D}list"));
}

edit:

print STDERR "Editing...\n";

if ($long) {
    if (system("$ENV{'EDITOR'} ${D}list.long")) {
	print STDERR "Edit command ($ENV{'EDITOR'} ${D}list.long) failed.\nEdit again? ";
	die "EOF on stdin\n" if (! defined($_ = <STDIN>));
	die "Aborting because edit failed.\n" if (! /^[Yy]/);
	goto edit;
    }
    print(STDERR "Splitting main and auxiliary database files",
	  $check ? " (and checking data)" : "", "...\n");
    if (system("./split-db.pl $check -in ${D}list.long ${D}list")) {
	print STDERR "Database split failed.  Edit again? ";
	die "EOF on stdin\n" if (! defined($_ = <STDIN>));
	die "Aborting because database split failed.\n" if (! /^[Yy]/);
	goto edit;
    }
}
else {
    if (system("$ENV{'EDITOR'} ${D}list")) {
	print STDERR "Edit command ($ENV{'EDITOR'} ${D}list) failed.\nEdit again? ";
	die "EOF on stdin\n" if (! defined($_ = <STDIN>));
	die "Aborting because edit failed.\n" if (! /^[Yy]/);
	goto edit;
    }

    if ($check) {
	print STDERR "Checking format of database...\n";
	if (system("./check-db.pl ${D}list")) {
	    print STDERR "Format check failed.  Edit again? ";
	    die "EOF on stdin\n" if (! defined($_ = <STDIN>));
	    die "Aborting because format check failed.\n" if (! /^[Yy]/);
	    goto edit;
	}
    }
}

chdir(${D}) || die "chdir(${D}): $!.\n";
system("${RCSPREFIX}rcsdiff -u list");
$exit_status = system("${RCSPREFIX}ci -u list");

unlink("${D}list.long") if (! $exit_status);

exit($exit_status);
