#!/apps/bin/perl
#
# $Id: update.pl,v 1.3 2004/04/16 18:34:28 marki Exp $
##########################################################################
#   Brief Discription:
#
#   execution:  update
#
#   cgi script for entering that maintenance has been performed
#
#   created:  Fri Apr 17 20:22:24 EDT 1998
#
#   author of the script: arne 
#   
##########################################################################


use Time::CTime;
use Time::JulianDay;
use CGI qw(:all);
use DBI;

for ($i=1; $i<32; $i++) {
    $day_num[$i] = $i;
}
%month_labels = (
		 '0' =>'Jan',
		 '1' =>'Feb',
		 '2' =>'Mar',
		 '3' =>'Apr',
		 '4' =>'May',
		 '5' =>'Jun',
		 '6' =>'Jul',
		 '7' =>'Aug',
		 '8' =>'Sep',
		 '9' =>'Oct',
		 '10' =>'Nov',
		 '11' =>'Dec'
		 );
$cgi_query = new CGI;

print $cgi_query->header();
print $cgi_query->start_form();
print $cgi_query->start_html(-title=>'Hall D Maintenance Update Form',
				 -BGCOLOR=>'#bbbbbb');
				 
$time_format="\%c";

$now = time();
$todays_day  =  strftime('%d',(localtime($now)));
$todays_month=  strftime('%m',(localtime($now)))-1;
$todays_year =  strftime('%Y',(localtime($now)));
if (param()) {
# header has already been filled out, loop through ALL the values 
# and see if any were marked....

    $password = $cgi_query->param('passwd');
    $task = $cgi_query->param('task');
    $task =~ s/\\s/ /g;

    if ($password ne 'keepitrunning') {
	print $cgi_query->p("Maintenance Update form for <BR> $task <BR>");
	print $cgi_query->p("please enter the date on which the scheduled maintenance was 
performed, the password and then click on the submit button<BR>");
	print $cgi_query->p("Thank You");

	print $cgi_query->hidden(-name=>'task',
				 -default=>$task);

	print $cgi_query->p("When was the last maintenance performed?",
			popup_menu(-name=>'last_day',
				   -values=> \@day_num,
				   -default=>$todays_day),
			"-",
			popup_menu(-name=>'last_month',
				   -values => [sort numerically keys(%month_labels)],
				   -default =>$todays_month,
				   -labels  => \%month_labels),
			"-",
			popup_menu(-name=>'last_year',
				   -values=> ['2013','2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028'],
				   -default=>$todays_year));

	print $cgi_query->p("Enter the Hall D Maintenance  password",
			password_field(-name=>'passwd',
				  -size=>20,
				  -maxlength=>40));
	print $cgi_query->p(submit("SUBMIT"), reset("RESET FORM"));


    } else {

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

	$sql_cmd = "SELECT period FROM maintenance WHERE task=\'$task\'";

	do_sql();

	$period = $sth->fetchrow;

	$last_day = $cgi_query->param('last_day');
	$last_month = $cgi_query->param('last_month');
	$last_year = $cgi_query->param('last_year');
	$long_task = $cgi_query->param('long_task');

	$period_type=1;

# convert last maintenance date into seconds since epoch
	$last_maintenance = jd_timelocal(0,0,8, $last_day, $last_month, $last_year);

	if ($last_maintenance > $now ) {
	    print "Working in the future again are we????? <BR>";
	    print "Please enter a date in the present";
	} else {	   
# calulate next maintenance date
	    $next_maintenance = $last_maintenance + $period;

# create the SQL query and send it

	    $sql_cmd = "UPDATE  maintenance  ";
	    $sql_cmd .= "set last_maintenance=$last_maintenance, ";
	    $sql_cmd .= "next_maintenance=$next_maintenance where task=\'$task\'";

	    do_sql();

	    &THANKS($task, $period);
	}

    }

} else { # give them the form to fill out

    print $cgi_query->p("Please enter the UNIQUE task name",
			textfield(-name=>'task',
				  -size=>20,
				  -maxlength=>20));

    print $cgi_query->p(submit("SUBMIT"), reset("RESET FORM"));
}

print $cgi_query->endform;

sub numerically { $a >= $b; }

sub THANKS {
    local($task, $period) = @_;
    $days = $period/86400.;
    print "Thanks for taking the time to perform the maintenance associated with task: <BR> ";
    print "<hr> <BR> $task <BR> <hr> <BR>";
    print "See you back in $days  days!!!<BR>";
    print "<a href=status.pl> Back to Hall D maintenance status page </a> <BR>";
}
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;
}
