#!/usr/local/bin/perl -w

# You may have to edit these.
# Althogh `ps' is available almost everywhere, it may need different
# switches. Use those that tell ps to display ALL processes.
# `whereis' looks for executables in binary paths.
# S_CONF is the most probable location of syslog.conf

$PS      = "ps ax";  # Linux
# $PS    = "ps -ef"; # IRIX
$WHEREIS = "whereis -b syslogd";
$S_CONF  = "/etc/syslog.conf";

#-----------------------------------------------------------------------

print "Tidy 1.0 Installation Test Utility\n\nChecking...\n";
$problems ='';

# Perl Version
if ( $] >= 5.0 ) {
	print "\nPerl $] is OK.\n";
	}
else {
	print "\nERROR: tidy NEEDS Perl v5. Please upgrade.\n"; exit 1;
	}

# syslogd running?
@_ = `$PS`;
$found = 0;
foreach (@_) {
	chop;
	if ( /syslogd/ ) {
		print "\nsyslogd is running (ps: $_)\n";
		$found = 1;
		}
	}
if ($found == 0) {
	print "\nWARNING: syslogd does not seem to be active\n";
	$problems .= "NOT_RUNNING ";
	$_ = `$WHEREIS`;
	if ( /\// ) { # LTS
		print "... but you seem to have it: $_\n";
		}
	else {
		print "\nERROR: syslogd not found\n";
		$problems .= "NO_SYSLOGD ";
		}
	}

if (-e "$S_CONF") {
	print "\nFound $S_CONF\n";
	if (-r "$S_CONF") {
		open ( Conf, "<$S_CONF" );
		print "\nReading syslog.conf...\n\n";
		while ( <Conf> ) {
			if (! /^\s*#/ ) {
				if ( /(.+?)\s+(.+)/ ) {
					$what = $1; $act = $2;
					foreach ( split ( ';', $what ) ) {
						if ( /(mail|\*)\.(\S+)/ ) {
							$fac = $1; $lev = $2;
							if ( "$act" =~ /(\/[\w.\/-]+)/ ) { # LTS
								print "syslog.conf: Facility $fac Level $lev is being logged to file $1. Good!\n             Try this file as the `logfile'\n";
								}
							else {
								print "syslog.conf: Facility $fac Level $lev has Action $act. Maybe bad.\n";
								$problems .= "ACTION ";
								}
							}
						}
					}
				}
			}
		close ( Conf );
		}
	else {
		print "\nWARNING: Cannot read syslog.conf. Do you have permission?\n";
		}
	}
else {
	print "\nERROR: syslog.conf not found\n";
	$problems .= "NO_CONF ";
	}

if ( $problems =~ /NOT_RUNNING/ ) {
	print <<"EOF";
Problem: syslogd is not running. Either you have to edit your syslog.conf
         and restart it or you don't have syslogd.
EOF
	}
if ( $problems =~ /NO_SYSLOGD/ ) {
	print <<"EOF";
Problem: syslogd not found in your binary paths. Maybe you have to add
         /usr/sbin or /etc or ... to your PATH environment. If you cannot
         find it on your system, try to get it via ftp and install it.
EOF
	}
if ( $problems =~ /NO_CONF/ ) {
	print <<"EOF";
Problem: $S_CONF not found. Either you do not have syslogd (see above)
         or you are using another syslog.conf. If this is the case, edit
         install-test and specify the correct path to syslog.conf an rerun
         this script.
EOF
	}
if ( $problems =~ /ACTION/ ) {
	print <<"EOF";
Problem: (possible) The messages from the mail (or all) facilities is not
         logged to a file but somewhere else. You may want to edit
         syslog.conf and specify a file, where the messages are logged to
         which will be processed by tidy. Refer to syslog.conf(5)
EOF
	}

