#!/apps/bin/perl
#
# $Id: maintain_email,v 1.8 2004/09/26 14:13:55 marki Exp $
##########################################################################
#   Brief Discription:
#
#   execution:  notify
#
# sorts through the maintenance data base table and send EMAIL  to the
# maintainers if a task is due for work
#
#   created:  Fri Apr 17 22:00:20 EDT 1998
#
#   author of the script: arne 
#   
##########################################################################
use lib ("/site/perlmod/Time-modules-2003.1126/lib/");
use Time::CTime;
use DBI;

# Switched to localhost by M. Wise, 3/4/2015 after switch to halldweb.jlab.org
# $hostname = "halldweb1.jlab.org";
$hostname = "halldweb.jlab.org";
#$hostname = "localhost";
$database = "maintenance";
$user = "maintainer";
$password = "";
$dbh = DBI->connect("DBI:mysql:$database:$hostname", $user, $password);

$time_fmt = '%D';

$sql_cmd = "SELECT task, email1, email2, next_maintenance, last_maintenance, period, long_task  FROM maintenance ORDER BY next_maintenance";

do_sql();

$ntasks = 0;
while (@column = $sth->fetchrow_array) {
    $task[$ntasks] = $column[0];
    $email1[$ntasks] = $column[1];
    $email2[$ntasks] = $column[2];
    $next_maintenance[$ntasks] = $column[3];
    $last_maintenance[$ntasks] = $column[4];
    $period[$ntasks] = $column[5];
    $long_task[$ntasks] = $column[6];
    $ntasks++;
}

$now = time();
$fdate = strftime($time_fmt,(localtime($now)));
for ($i=0; $i<$ntasks; $i++) {
    $last_day = strftime($time_fmt,(localtime($last_maintenance[$i]))); 
    $next_day = strftime($time_fmt,(localtime($next_maintenance[$i]))); 

    if ($now+86400 > $next_maintenance[$i]) {

	&SEND_EMAIL($task[$i], $next_day, $last_day, $email1[$i], $email2[$i],$long_task[$i], $fdate);
    }
}


sub SEND_EMAIL {
    local($task, $n_day, $l_day, $em1, $em2, $lt, $fmt_date) = @_;

    $subject = "Maintenance task: $task";
    $real = 1;
    if ($real) { # send real mail
	open(MAIL, "|mail -s \"$subject\" $em1 $em2"); 
    } else { # send only to hovanes
	open(MAIL, "|mail -s \"$subject\" hovanes\@jlab.org"); 
    }
    print MAIL "Dear $em1  and $em2  on $fmt_date\n\n";
    print MAIL "TASK: $task \n\nis due or was due for maintenance on $n_day \n";
    print MAIL "the previous maintenance was performed on $l_day \n";
    print MAIL "\n";
    print MAIL "Long Description:\n\n $lt\n\n";
    print MAIL "In order to stop receiving these messages update the maintenance date\n";
    # Changed 3/4/2015 by M. Wise to reflect new host name (halldweb vs. halldweb1)
	# print MAIL "for task: $task at \n    http://halldweb1.jlab.org/cgi-bin/maintenance/cgi/status.pl\n\n";
    print MAIL "for task: $task at \n    http://halldweb.jlab.org/cgi-bin/maintenance/cgi/status.pl\n\n";
    print MAIL "Thanks---the maintenance keeper\n";

    close(MAIL);

}

sub do_sql {
    $sth = $dbh -> prepare($sql_cmd)
        or die "Can't prepare $sql_cmd: $dbh->errstr\n";
    $rv = $sth -> execute
        or die "Can't execute the query $sql\n error: $sth->errstr\n";
    return;
}
