#!/usr/bin/perl
# -*- Mode: Perl -*-
# 1. the Description line shows up in the Admin GUI
#    on the Robot | Crawl Settings -> Script to Launch menu
#
# description=email message 
#
# Set this if you don't want me guessing..
#
#$email = "admin\@domainname.com";


umask 022;


$echo = "/bin/echo";
@mailers = ("/usr/ucb/Mail",	# solaris
	    "/usr/sbin/Mail",	# Irix
	    "/bin/Mail",	# AIX
	    );
$mailer = "/usr/ucb/Mail";	# default
foreach (@mailers) {
    if (-e $_) {
	$mailer = $_;
	last;
    }
}
## Get the date info
@lc = localtime;
$time = sprintf("%02d:%02d",$lc[2],$lc[1]);
$date = $lc[3];
$mon = (Jan,Feb,Mar,Apr,May,June,July,Aug,Sept,Oct,Nov,Dec)[$lc[4]];
$year = 1900 + $lc[5];
$day = (Sun,Mon,Tues,Wed,Thur,Fri,Sat,Sun)[$lc[6]];

### extract the desired contact email address out of the 
### robot.conf file
$tmp = "config/robot.conf";
open(FILE,$tmp) or die "can't open robot.conf ($tmp) file";
undef $idle;
while(<FILE>) {
    # skip it if the user
    $email = $1 if (m/^\s*email\s*=\s*\"*([^"]+)\"*/i);
    $idle  = $1 if (m/^\s*oncompletion\s*=\s*\"*([^"]+)\"*/io);
    if (m/\s*csid\s*=\s*\"*(.+):\/\/(.+)\/([^"]+)\"*/io) {
	$server = $2;
	$instance  = $3;
    }
}
close(FILE);	       

## Construct a message

$idle        = "completed" unless $idle;
$date_string = "Robot $idle on $day $mon $date, $year at $time";
$subject     = "$instance robot $idle";

$msg=<<END;
This is a message from the Search Server robot running from 
instance $instance at server $server.

$date_string


END

## Spew the message out via whatever mailer we could find
if (!$email) {
    print "No email address.\n";
}
else {
    system("$echo \"$msg\" | $mailer -s \"$subject\" $email");
}

exit;





