#!/usr/bin/perl

use lib '/web/home.physics.ucla.edu/webroot/cgi-bin/rooms/';	# Added to find the libraries
require "cgi-lib.pl";
require "room_calendar.pl";
require "room_lib.pl";
require "comment.pl";

#OHMNEW
$doc_root = "/web/home.physics.ucla.edu/webroot/html/calendar/rooms/db";

local (%month) = ("January",1,"February",2,"March",3,"April",4,"May",5,"June",6,"July",7,"August",8,"September",9,"October",10,"November",11,"December", 12); 

local (@monthlist) = ("January","February","March","April","May","June","July","August","September","October","November","December"); 
local($idx);
local($year);

# mset=0 or null) --> initial 3 months
# mset=1          --> next 3 months 
$mset = shift @ARGV; 

if ($mset==0){ $idx = (localtime)[4]; }
if ($mset==1){ $idx = (localtime)[4] + 3; }
if ($mset==2){ $idx = (localtime)[4] + 6; }
if ($mset==3){ $idx = (localtime)[4] + 9; }
if ($mset==4){ $idx = (localtime)[4] + 12; }
if ($mset==5){ $idx = (localtime)[4] + 15; }
if ($mset==6){ $idx = (localtime)[4] + 18; }
if ($mset==7){ $idx = (localtime)[4] + 21; }


#OHMNEW
$year = (localtime)[5] + 1900;


if   ($idx==12) {$idx=0; $year++;}
if   ($idx==13) {$idx=1; $year++;}
if   ($idx==14) {$idx=2; $year++;}
if   ($idx==15) {$idx=3; $year++;}
if   ($idx==16) {$idx=4; $year++;}
if   ($idx==17) {$idx=5; $year++;}
if   ($idx==18) {$idx=6; $year++;}
if   ($idx==19) {$idx=7; $year++;}
if   ($idx==20) {$idx=8; $year++;}
if   ($idx==21) {$idx=9; $year++;}
if   ($idx==22) {$idx=10; $year++;}
if   ($idx==23) {$idx=11; $year++;}
if   ($idx==24) {$idx=0; $year=$year+2;}
if   ($idx==25) {$idx=1; $year=$year+2;}
if   ($idx==26) {$idx=2; $year=$year+2;}
if   ($idx==27) {$idx=3; $year=$year+2;}
if   ($idx==28) {$idx=4; $year=$year+2;}
if   ($idx==29) {$idx=5; $year=$year+2;}
if   ($idx==30) {$idx=6; $year=$year+2;}
if   ($idx==31) {$idx=7; $year=$year+2;}
if   ($idx==32) {$idx=8; $year=$year+2;}
if   ($idx==33) {$idx=9; $year=$year+2;}

#&ReadParse(*input);
print &PrintHeader;
print "<html>\n";
&style1;

print "<head><title>Physics & Astronomy Conference Room Reservation</title></head>\n";
print "<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#6666cc\" vlink=\"#6666cc\">\n";
print "<base target=\"view\">\n";
&comment;


&room_check(@monthlist[$idx],$year);
print "<br clear=all>\n";
print "&\#160<br>\n";

$idx++;
if ($idx==12) {$idx=0; $year++;}

&room_check(@monthlist[$idx], $year);
print "<br clear=all>\n";
print "&\#160<br>\n";

$idx++;
if ($idx==12) {$idx=0; $year++;}

&room_check(@monthlist[$idx], $year);
print "<br clear=all>\n";
print "&\#160<br>\n";

print "</base>\n";

print "<base target=\"cal\">\n";
print "<center><font color=000000 face=verdana font size=-2>";


if($mset>=7){
   $number_m= $mset -1;
 print "<a href=/cgi-bin/rooms/room_check.pl?$number_m target=\"_self\">Previous 3 Months </a> &\#160";
       }
else
       {
      $number_m = $mset +1;
 print "<a href=/cgi-bin/rooms/room_check.pl?$number_m target=\"_self\">Next 3 Months</a> &\#160";
 }


if(($mset>=1) && ($mset<7)){
   $number_mb= $mset - 1;
 print "<a href=/cgi-bin/rooms/room_check.pl?$number_mb target=\"_self\">Previous 3 Months</a> &\#160 <br> </center>";
       }


print "</font></center>\n";
print "</base>\n";

print &HtmlBot;
#------------------------------------------------------------
sub room_check { # extracts room data and prints day or month
        local ($month,$year)= @_;
	local($room_data,@room_data);
	$room_data = &room_read($doc_root);
	if ($input{'day'}) {
		&check_date($room_data); # prints the reservation for day
	} else {
		&room_calendar($month,$year,$room_data); # prints entire month
	}
}
#------------------------------------------------------------
sub check_date {
	local($room_data) = $_[0];
	local($no_schedule,@record,$record,$month,$day,$date) = 1;
	local (%months) = ("January",1,"February",2,"March",3,"April",4,"May",5,"June",6,"July",7,"August",8,"September",9,"October",10,"November",11,"December",12);
	$req_date = "m$months{$input{'month'}}d$input{'day'}";
	print "<h3>Room $input{'room'} Schedule on $input{'month'} $input{'day'}</h3>\n";
	@record = split(/\n/,$room_data);
	foreach $record (@record) {
		@field = split(/#/,$record);
		$field[1] =~ s/-/d/;
		$field[1] = "m$field[1]";
		if ($req_date eq $field[1]) {
			print "<b>Time:</b> $field[2]<br>\n<b>By:</b> $field[3]<br>\n<b>Activity:</b> $field[4]<p>\n";
			$no_schedule = 0;
		}
	}
	($no_schedule == 1) && (print "No reservations made.<br>\n");
}


