#!/usr/bin/perl --
# Test message script for debian's Smail distribution.
# Copyright 1994 Ian Jackson.  There is NO WARRANTY.
# See /usr/doc/smail/copyright or type smail -bC for more information.

$mayremote=1;
$|=1;

$_= shift(@ARGV);
if ($_ eq '--allowremote') { $mayremote=1; }
elsif ($_ eq '--localonly') { $mayremote=0; }
else { die "usage: smailtest --localonly | --allowremote\n"; }

$halvestrand='linux-counter@uninett.no';

$visiblename='';
if (open(C,"/etc/smail/config")) {
    while (<C>) {
        next unless s/^visible_name=//;
        s/\s+$//; s/^\s+//;
        $visiblename= $_;
        last;
    }
}

if ($mayremote) {
    $msg= length($visiblename)
        ?
"
Would you like to send a test message now ?  Select one of:

 Return or Y  Yes, test offsite delivery with a message to Harald Alvestrand's
               Linux Counter project - you should then receive an autoreply
               to postmaster\@$visiblename.

              Do not use this option until mail routing to your machine has
               been properly enabled, please !

     L        Just a local message from root\@$visiblename
               to postmaster\@$visiblename.

     N        No, thanks.  (You can do this later using /usr/sbin/smailtest.)

Send a test message now ?  (y/l/n)    "
        :
"
Would you like to send a test message now ?  Select one of:

 Return or Y  Yes, test offsite delivery with a message to Harald Alvestrand's
               Linux Counter project - you should then receive an autoreply.

              Do not use this option until mail routing to your machine has
               been properly enabled, please !

     L        Just a local message to \`root' from \`postmaster'.

     N        No, thanks.  (You can do this later using /usr/sbin/smailtest.)

Send a test message now ?  (y/l/n)    ";
    $accept= 'yln';
} else {
    $msg=
"Would you like to send a test message from `root' to `postmaster' ?
Send a test message now ?  (y/n)     ";
    $accept= 'yn';
}

do {
    print $msg;
    $_= <STDIN>;
    s/\s+$//; s/^\s+//;
} while (!m/^[$accept]?$/i);

exit 0 if m/^n/i;

chop($d=`date`);

if (!$mayremote || m/^l/i) {
    $message=
"From: root
To: postmaster
Subject: Smail system installation test message

This is the test message you were promised.
It was sent at $d.

If you want to send a remote test message run \`smailtest' again.
";
} else {
    chop($host=`hostname --fqdn`);
    $message=
"From: postmaster
To: $halvestrand
Subject: Linux mail system installation auto-registration/test

This message was sent at $d
to test this system's mailer and to register its use of
Smail under a Debian Linux System.

//echo
//machine
name: $host
distribution: Debian 1.1
mailer: Smail 3.2-3
//end
";
}

open(P,"|/usr/sbin/sendmail -oem -odi -t -oi") || die "pipe to smail: $!\n";
print(P $message) || die "write to smail: $!\n";
$!=0; close(P); $? && die "smail failed $?/$!\n";

print "
Test message sent.

";

exit 0;
