#!/usr/athena/bin/perl
#
#

####################################################
# Configuration parameters
####################################################

$Home_dir		=	"/usr/local/lib/milestones";

####################################################
# Other global variables
####################################################

$Error_log		=	"$Home_dir/ERROR_LOG";
$Digest			=	"$Home_dir/digest";
$Postlog		=	"$Home_dir/postlog";
$Lockfile		=	"$Home_dir/lockfile";
$tmplock 		=	"$Home_dir/lock$$";

while (&get_lock($Lockfile) != 0) {
	sleep 1;
}

open (Digest, ">>$Digest") || die "Can't open digest file\n";
open (Postlog, ">>$Postlog") || die "Can't open postlog file\n";
print Postlog "\001\001\001\001\n";

$state = 0;
$from = "The unknown sender";
$subject = "Unspecified subject";
while(<>) {
	print Postlog $_;
	chop;
	if ($state == 0) {
		if (/^date:/i) {
			s/^date: //i;
			$date = $_;
			next;
		}
		if (/^from:/i) {
			s/^from: //i;
			$from = $_;
			next;
		}
		if (/^subject:/i) {
			s/^subject: //i;
			$subject = $_;
			next;
		}
		if (/^$/) {
			print Digest "Date: $date\n";
			print Digest "From: $from\n";
			print Digest "Subject: $subject\n\n";
			$state = 1;
			next;
		}
	} elsif ($state == 1) {
		if (/^$/) {
			next;
		} else {
			print Digest "$_\n";
			$state = 2;
			next;
		}
	} elsif ($state == 2) {
		if (/^$/) {
			$state = 3;
			next;
		} 
		print Digest "$_\n";
	}

}

print Digest "------------------------------------\n";
close(Digest);
print Postlog "\001\001\001\001\n";
close(Postlog);
unlink($Lockfile);
unlink($tmplock);

# This is called to obtain a lock using a specified filename.  Returns 1
# if it is unable to obtain the lock.
#
# do get_lock($lock)
#
sub get_lock
{
        local ($lock) = @_;
        local ($locktime);
        local ($ESRCH) = (3);                   # Error return from kill....

	open (f, ">$tmplock") || die "Can't open temp lock file\n";
	print f "$$\n";
	close (f);

        # Try to lock the host by linking $lock to link to the standard
        # lock file name.  Since linking is atomic, this will succeed only
        # if the standard lock file doesn't already exist.  If the lock
        # file *does* exist, read the pid from it and send a null signal
        # to see if the process that created it is still alive - and remove
        # it if not.

        $locktime = 0;
        while (link("$tmplock", "$lock") == 0) {
                # couldn't link; see if the lock is valid and clobber
                # it if so
                if (open (f, "$lock")) {
                        $pid = <f>;
                        chop($pid);
                        close (f);
                        if (($pid <=0) ||
                            (($kr = (kill(0, $pid)) == 0) &&
                                        (($err = $!) == $ESRCH))) {
                                print stderr " lock_invalid($pid, $err)";
                                unlink($lock);
                        } else {
                                print stderr " busy($pid, $kr, $err)\n";
                                return(-1);
                        }
                } else {
                        print stderr " bad_link";
                }
                if ($locktime++ > 3) {
                        # We should never get here.
                        open(error, ">>$Error_log");
                        ($sec, $min, $hour, $day, $mon, $year)=localtime(time);                        printf error ("%02d/%02d/%02d %02d:%02d:%02d %s %s\n",
                                $mon, $day, $year, $hour, $min, $sec,
                                "Link lockup on", $lock);
                        close(error);
                        return(-1);
                }
        }
        0;
}
