#!/usr/bin/perl

use lib '/web/home.physics.ucla.edu/webroot/cgi-bin/rooms/';
require "cgi-lib.pl";
require "room_schedule_lib.pl";

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

&ReadParse(*input);

if($input{'day'} && $input{'month'} && $input{'year'}){
  $day=$input{'day'};
  $month=$input{'month'};
  $year=$input{'year'};
}
else {
  $day=(localtime)[3];
  $month=(January,February,March,April,May,June,July,August,September,October,November,December)[(localtime)[4]];

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

}

$sch_database="$database_root$year$month";

print &PrintHeader;

@update_s=&update;

@class_data = &read_classes($sch_database);
print &schedule_head;

print &print_schedule(@class_data);
print &schedule_tail;

#----------------------------------------------------
sub unique_time { # prints time slots 
	local ($start,$end,$length,$half) = @_;
	$half = "";
	($start =~ /\.5/) && ($half = ":30");
	$start =~ s/\.5//;
	if($start >= 13) {
		$start -= 12;
		$start .= "$half pm";
	} elsif($start < 12) {
		$start .= "$half am";
	} else {
		$start .= "$half pm";
	}
	$half = "";
	($end =~ /\.5/) && ($half = ":30");
	$end =~ s/\.5//;
	if($end >= 13) {
		$end -= 12;
		$end .= "$half pm";
	} elsif($end < 12) {
		$end .= "$half am";
	} else {
		$end .= "$half pm";
	}
	$length = "<br>$start - $end";
	return $length;
}
#----------------------------------------------------
sub print_schedule { # prints the room schedule for the date selected 
   local (@class_data,$time) = @_;
   local (@rooms,$start,$end,$room,$record,@record,$has_record);
   local ($length);
   #@rooms = ("2-222 Knudsen","3-121 Knudsen","4-134 Knudsen","6-129 Knudsen","2-707A PAB","3-703 PAB","4-330 PAB","4-708 PAB","PAB Patio");
   #@rooms = ("2-222 Knudsen","3-121 Knudsen","4-134 Knudsen","6-129 Knudsen","2-707A PAB","Astro Reading Room","3-703 PAB","4-330 PAB","4-708 PAB","PAB Patio");
   @rooms = ("2-222 Knudsen","3-121 Knudsen","4-134 Knudsen","6-129 Knudsen","2-707A PAB","Astro Reading Room","3-703 PAB","4-330 PAB","4-708 PAB","4-740 PAB","PAB Patio");

   print "<center>\n";
   print "<table cols=1 bgcolor=ffffff width=95% border=1>\n";
   print "<tr valign=center align=center><td colspan=12 bgcolor=\#999999>";
   print "<font face=verdana size=+1 color=ffffff>
		<b>Physics & Astronomy Conference Rooms</b></br>
		$day $month $year<br>\n";
   print "</td></tr>\n";
   print "<tr valign=center align=center>\n";
   print "<td  colspan=1 bgcolor=\#999999>&\#160</td>\n";
   foreach $room (@rooms) { 
     print "<td colspan=1 valign=\"top\" align=\"center\" bgcolor=\#999999><font face=verdana size=-1 color=ffffff>$room</td>\n";
   }
   print "</tr>\n";

   for ($time = 8; $time < 20; $time=$time+0.5) { # start at 8am thru 8pm 
     ($start,$end) = &schedule_time($time);

     # print the start and end times
     print "<tr valign=center align=center>\n";
     print "<td  colspan=1 height=50 bgcolor=\#999999><font color=ffffff size=-1 face=verdana>"; 
     print "$start<br>$end"; 
     print "</font></td>\n"; 

     foreach $room (@rooms) { # scan thru the data by room
	$has_record = 0;
	foreach $record (@class_data) {
           @record = split(":",$record);
           $length = undef;

           if ( ($record[0] eq $room) &&
               (($record[2] == $time) || 
               (($record[2] <  $time) && ($record[3] > $time)))) {

              #(($time + 1) != $record[3]) && 
              #                ($length = &unique_time($record[2],$record[3]));
              $length = &unique_time($record[2],$record[3]);

              if ($record[1] == $day && $has_record==0){
                 print "<td colspan=1 bgcolor=\#ffffcc>";
                 print "<font size=-2 face=verdana>\n";
                 print "$record[5]<br>$record[4]$length";
                 print "</font>\n";
                 print "</td>\n";
                 $has_record = 1;
              }

           } # if room & time

        } # foreach $class_data

        if ($has_record==0) {
           print "<td colspan=1>&\#160</td>\n";
           #print "<td colspan=3></td>\n";
        }

     } # foreach @room
     print "</tr>\n";

   } # for time

   return;
}

#----------------------------------------------------
sub schedule_tail { #closes out the table and html page
        print "</table>\n";
        print "</center>\n";
        print "</body>\n";
        print "</html>\n";
	return;
}
#----------------------------------------------------
sub schedule_head {	# prints the table header
	print "<html><head><title>Physics & Astronomy Conference Reservation</title></head>";
	print "<body bgcolor=ffffff>";
	return;
}
