#!/usr/athena/bin/perl -w
#
# mkds_mail -- add support for sending mail to a discuss meeting,
#              assuming a particular qmail configuration
#
# mhpower@mit.edu, 21 April 1998
#

use Fcntl qw(:flock);
umask(022);

$saw_a_or_d = 0;
$saw_none = 0;

if ($#ARGV < 1)
 {
   print "Insufficient arguments\n";
   print "Examples:   mkds_mail -d eve\n";
   print "            mkds_mail --none new_meetings\n";
   print "            mkds_mail -A -s 20 test\n";
   exit 1;
 }

if ($ARGV[$#ARGV] =~ /^\/usr\/spool\/discuss\//)
  {
    print "do not include /usr/spool/discuss/ in meeting name\n";
    exit 1;
  }

# e.g., shortname,mailbox
if ($ARGV[$#ARGV] =~ /\,/)
  {
    @dnames = split(/\,/, $ARGV[$#ARGV], 2);
    $shortname = $dnames[0];
    $mailbox = $dnames[1];
  }
else
  {
    $shortname = $ARGV[$#ARGV];
    $mailbox = $ARGV[$#ARGV] . "-mtg";
  }

if (substr($shortname, 0, 1) eq "-" || substr($mailbox, 0, 1) eq "-")
  {
    print "name should not begin with '-'\n";
    exit 1;
  }

$mailbox =~ tr/A-Z/a-z/;

$a = "=" . $mailbox . ":daemon:1:1:/var/mailspool/" .
   $mailbox . ":::\n";
$mspool = "/var/mailspool/" . $mailbox;
$meeting = "/usr/spool/discuss/" . $shortname;

#print "(DEBUG) assign line  $a";
#print "(DEBUG) mspool=\"$mspool\"\n";
#print "(DEBUG) meeting=\"$meeting\"\n";

$qm = "| preline /usr/local/bin/dsmail";
for ($i = 0; $i < $#ARGV; ++$i)
  {
    $thisa = $ARGV[$i];
    if ($thisa eq "-d" || $thisa eq "-A")
      {
        $saw_a_or_d = 1;
      }
    elsif ($thisa eq "--none")
      {
        $saw_none = 1;
        next;
      }
    if ($thisa =~ /\s/)
      {
        $thisa = "'" . $thisa . "'";
      }
    $qm .= " ";
    $qm .= $thisa;
  }
$qm .= " ";
$qm .= $meeting;
$qm .= "\n";

#print "(DEBUG) qm = \"$qm\"\n";

if (! $saw_a_or_d && ! $saw_none)
  {
    print "include -d or -A, or else include --none if you really " .
          "want no headers preserved\n";
    exit 1;
  }

#print "(DEBUG) ", $#ARGV, "\n";

$e = open(THELOCK, ">/var/qmail/users/assign.lock");
if (!defined($e))
 {
   die "could not open assign.lock, $!,";
 }
$e = flock(THELOCK, LOCK_EX|LOCK_NB);
if ($e == 0)
{
   print "/var/qmail/users/assign.lock is already locked\n";
   exit 1;
}

$e = open(NASSIGN, ">/var/qmail/users/assign.new");
if (!defined($e))
  {
    die "could not open assign.new for write, $!,";
  }
$e = open(ASSIGN, "/var/qmail/users/assign");
if (!defined($e))
  {
    die "could not open assign for read, $!,";
  }
$alineseen = 0;
while (defined($aline = <ASSIGN>))
  {
     if ($aline eq ".\n")
       {
          $alineseen = 1;
          last;
       }
     $e = print NASSIGN $aline;
     if ($e == 0)
       {
         print "could not write line to assign.new\n";
         exit 1;
       }
  }
if ($alineseen != 1)
  {
     print "did not find . on a line by itself in assign\n";
     exit 1;
  }

$e = print NASSIGN $a;
if ($e == 0)
  {
    print "could not write line to assign.new\n";
    exit 1;
   }

$e = print NASSIGN ".\n";
if ($e == 0)
  {
    print "could not write line to assign.new\n";
    exit 1;
  }

close(NASSIGN) || die "could not close assign.new, $!,";

$e = mkdir($mspool, 0755);
if ($e != 1)
  {
     die "could not mkdir $mspool, $!,";
  }
chown 1, 1, $mspool;
$mspool .= "/.qmail";

$e = open(QMAILF, ">$mspool");
if (!defined($e))
  {
     die "could not open $mspool for write, $!,";
  }
$e = print QMAILF $qm;
if ($e == 0)
  {
     print "could not write to $mspool\n";
     exit 1;
  }
close(QMAILF) || die "could not close .qmail, $!,";
chown 1, 1, $mspool;

rename("/var/qmail/users/assign.new", "/var/qmail/users/assign");

$e = open(LOCALU, ">>/etc/localusers");
if (!defined($e))
  {
    die "cannot open localusers for append, $!,";
  }
$e = print LOCALU $mailbox;
if ($e == 0)
  {
     print "could not write to localusers\n";
     exit 1;
  }
$e = print LOCALU "\n";
if ($e == 0)
  {
     print "could not write to localusers\n";
     exit 1;
  }
close(LOCALU) || die "could not close localusers, $!,";

$e = open(AHISTORY, ">>/var/qmail/users/assign.history");
if (!defined($e))
  {
    die "cannot open assign.history for append, $!,";
  }
($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
  localtime(time);
$mon += 1;
$mday += 1;
$year += 1900;

# for -w
$isdst += 0;
$wday += 0;
$yday += 0;

$aentry = "<" . $mon . "/" . $mday . "/" . $year . " " . $hour . ":" .
  $min . ":" . $sec . ">   \"" . $mailbox . "\"  " . $qm;
$e = print AHISTORY $aentry;
if ($e == 0)
  {
     print "could not write to assign.history\n";
     exit 1;
  }
close(AHISTORY) || die "could not close assign.history, $!,";

system("/var/qmail/bin/qmail-newu");

flock(THELOCK, LOCK_UN);
close(THELOCK);
