#!/bin/perl
# config-test,v 1.4 1996/09/17 17:41:51 cwilson Exp
# configuration test for majordomo
# provided with majordomo, modifications by darren stalder <torin@daft.com>
# more mods by Vince Skahan <vince@atc.boeing.com>
#
# execute this by cd to your majordomo dir, then 'wrapper config-test'
#

@requires = ( "ctime.pl",
	     "majordomo_version.pl",
	     "majordomo.pl",
	     "shlock.pl",
	     "config_parse.pl",
	      );


&header("Obvious things:");

&header("environment variables");
foreach $e (sort keys %ENV) {
        print "   $e=$ENV{$e}\n";
}

&header("euid/egid checks");

$euid_name=getpwuid($>);
push(@egid_group_numbers,(split(' ',$) )));   # it switches groups...
foreach $groupnum (@egid_group_numbers) {
	$name = getgrgid($groupnum);
	push(@egid_names,$name);
}
print "   effective user  = $euid_name (uid $>)\n";
print "   effective group = @egid_names (gid $) )\n";

&header("uid/gid checks");

$uid_name=getpwuid($<);
push(@gid_group_numbers,(split(' ',$( )));   # it switches groups...
foreach $groupnum (@gid_group_numbers) {
	$name = getgrgid($groupnum);
	push(@gid_names,$name);
}
print "   real      user  = $uid_name (uid $<)\n";
print "   real      group = @gid_names (gid $( )\n";

&header('');
print "\n\tNon obvious things that cause headaches:\n\n";
&header('');

foreach (@requires) {
    if (require $_) {
	&good("found $_ okay.");
    } else {
	&bad("failed to find $_ in \@INC");
    }
}

print "\n";

$cf = $ENV{'MAJORDOMO_CF'};

if (require $cf) { 
    &good("\"require\"d $cf okay.");    
} else {
    &bad("unable to open $cf: $!");
}

print "\n--==> Majordomo home directory is $homedir.\n";

unshift(@INC, $homedir);

&header("Include directories");
foreach (@INC) {
    print "\t$_\n";
}

&header("Home");

if (chdir($homedir)) {
    &good("changedir to $homedir succeeded.");
} else {
    &bad("changedir to $homedir failed, $!");
}
if (open(TEST, ">cftest.$$")) {
    &good("Created a mock lock file.");
    close(TEST);
    unlink("cftest.$$");
}
else {
    &bad("Couldn't create a mock lock file.\n      \$homedir ($homedir) needs to be writable.");
}

&header("temp directory");
if (! defined $TMPDIR) {
    &bad("\$TMPDIR not defined by majordomo.cf");
}
elsif (-d $TMPDIR) {
    if (open(TEST, ">$TMPDIR/cftest.$$")) {
	&good("Created a temp file in \$TMPDIR ($TMPDIR).");
	close(TEST);
	unlink("$TMPDIR/cftest.$$");
    }
    else {
	&bad("Couldn't create a file in $TMPDIR.");
    }
}
else {
    &bad("\$TMPDIR ($TMPDIR) does not exist.");
}

&header("list directory");

if (-d $listdir) {
    if (-r $listdir && -w $listdir && -x $listdir) {
	&good("list directory $listdir has good permissions.");
    } else {
	&bad("list directory $listdir has bad permissions");
    }
} else {
    &bad("list directory $listdir doesn't exist or isn't a directory.");
}

&header("log");

if ( ! -e $log ) {
    print "Logfile $log didn't exist, trying to create...";
    if (open(A, ">$log")) {
	print "okay.\n";
	chmod (A, 664) || &bad( "but chown on $log failed, $!");
    } else {
	print "nope!\n";
    }
}

if ( -r $log && -w $log) {
    &good("logfile $log exists and is writeable.");
} else {
    &bad("logfile $log exists, but is not writeable.");
}

&header ("Mailers");
if ($mailer) {
    print "You have defined a mailer for delivery.\n";
    if ($mailer =~ /sendmail.*\s-t/i) {
	print "Whoa!  You have given the \"-t\" option to sendmail.  This can cause mail\n";
	print "loops when used for outbound delivery.\n";
	$BAD++;
    }
    ($x = $mailer) =~ s/\s.*$//;  # Remove everything after and including
                                  # the first space
}
elsif ($sendmail_command) {
    print "You haven't defined a \$mailer to be used for delivery, but you have\n";
    print "defined \$sendmail_command.  Majordomo will use\n";
    print "$sendmail_command -f\\\$sender\n";
    print "to deliver mail to the list unless you define list-specific cases.\n";
    $x = $sendmail_command;
}
else {
    print "You have defined neither \$mailer, nor \$sendmail_command.\n";
    print "Majordomo will use\n";
    print "/usr/lib/sendmail -f\\\$sender\n";
    print "to deliver mail to the list.\n";
    $x = "/usr/lib/sendmail";
}
    
print "Attempting to verify that this is a valid mailer...";
if ( -x $x ) {
    print "looks okay.\n";
} else {
    print "nope, $x is not executable\n";
    $BAD++;
}

if ($bounce_mailer) {
    print "You have defined a mailer for delivering administrative messages.\n";
    ($x = $mailer) =~ s/\s.*$//;  # Remove everything after and including
                                  # the first space
}
elsif ($sendmail_command) {
    print "You haven't defined a \$bounce_mailer to be used for delivering\n";
    print "administrative messages, but you have defined \$sendmail_command.\n";
    print "Majordomo will use\n";
    print "$sendmail_command -f\\\$sender -t\n";
    print "to deliver administrative mail.\n";
    $x = $sendmail_command;
}
else {
    print "You have defined neither \$mailer, nor \$sendmail_command.\n";
    print "Majordomo will use\n";
    print "/usr/lib/sendmail -f\\\$sender -t\n";
    print "to deliver administrative mail.\n";
    $x = "/usr/lib/sendmail";
}
    
print "Attempting to verify that this is a valid mailer...";
if ( -x $x ) {
    print "looks okay.\n";
} else {
    print "nope, $x is not executable\n";
    $BAD++;
}


&header("end of tests");

print "\n\n";

if ($BAD) {
    print "$BAD bad ", $BAD == 1 ? "thing was" : "things were", " found.\n";
    print "Please fix before attempting to run Majordomo.\n";
} else {
    print <<"ZOT";
Nothing bad found!  Majordomo _should_ work correctly.

If it doesn't, check your configuration file 
	($cf) 
closely, and if it still looks okay, consider asking the majordomo-users
mailing list at "majordomo-users\@greatcircle.com" for assistance.  Be sure
and fully specify what your problems are, and what type of machine (and
operating system) you are using.

Enjoy!

ZOT
}    


######################################################################
sub good { print "Good: $_[0]\n"; }

sub bad { print "BAD:  $_[0]\n"; $BAD++;}

sub header {
    print '-' x ( ( 60 - (length($_[0]) + 2) ) / 2),
    " $_[0] ", '-' x ( ( 60 - (length($_[0]) + 2) ) / 2), "\n";
}

#
# that's all folks

