#!/usr/athena/bin/perl

# $Header: /afs/gza.com/development/mail-server/RCS/server.plin,v 1.29 1994/02/23 17:22:52 jik Exp $

push(@INC, '/rtfm/mserv');

require 'mservlib.pl';
require 'syslog.pl';
require 'sysexits.ph';

$queuefile = &lib'make_file_name("data");
$lockfile = &lib'make_file_name("data-lock");
($progname = $0) =~ s,.*/,,;

umask(007);

$SIG{'HUP'} = 'lib\'cleanup';
$SIG{'INT'} = 'lib\'cleanup';
$SIG{'QUIT'} = 'lib\'cleanup';
$SIG{'TERM'} = 'lib\'cleanup';

while ($_ = $ARGV[0], /^-/) {
    shift;
    if (/^-queuedir$/) {
	die "Missing argument to \"-queuedir\" option.\n" if (@ARGV == 0);
	$lib'queuedir = shift;
	next;
    }
    if (/^-logname$/) {
	die "Missing argument to \"-logname\" option.\n" if (@ARGV == 0);
	$progname = shift;
	next;
    }
    if (/^-logservice$/) {
	die "Missing argument to \"-logservice\" option.\n" if (@ARGV == 0);
	$lib'logservice = shift;
	next;
    }
    die "Unknown option \"$_\".\n";
}

&openlog($progname, '', $lib'logservice);

if (! &lib'get_lock($lockfile, 1)) {
    warn "Can't get lock!  Try again.\n";
    exit &EX_TEMPFAIL;
}

open(QUEUE, ">$lib'queuedir/$queuefile") || die;

while (<STDIN>) {
    print QUEUE $_ || die "Writing to $lib'queuedir/$queuefile: $!.\n";
}

close(QUEUE);

&syslog('info', "queued $queuefile");

&lib'clear_lock($lockfile);

&lib'cleanup;
