#!/usr/bin/perl 

# cgi-bin/packawhallop/calendar.pl 

use strict; 
use CGI; 
use Time::Local; 

# Perl doesn't have a trim or strip function. So we need to write a quicky to 
# do the deed. 
sub trim { 
	my $str = shift; 
	$str =~ s/^\s+|\s$//g; 
	return $str; 
} 

# Set HTTP path to application. This will be used to map to CSS and JavaScript.
my $http_root = "http://www.gulfofmaine.org/admin/hrsc";  
# Initialize CGI. 
my $cgi = new CGI; 
# Initialize $page. This is where we'll draw our calendar's markup before 
# drawing it in the the web page. 
my $page = ''; 
# To make nicely formatted markup easily readable when viewing source through 
# the web browser use '$grokable = 1;' here. Otherwise to not bother with tabs
# and carriage returns but just draw the markup use '$grokable = 0;' here 
# instead. 
my $grokable = 0; 
# On UNIX systems cal is the program that'll draw a calendar. On Mac OS X (for
# example) start Terminal and type 'cal' and press RETURN. Neat huh? So cal 
# gives us a calendar without our needing to construct one in PHP. Which is 
# extremely cool. Naturally we need cal on the system to be able to use it. 
my $calbin = "/usr/bin/cal"; 
if (-e $calbin && -x $calbin) { 
	my %get = {}; 
	if (defined($cgi->param('m')) && defined($cgi->param('y'))) { 
		if (defined($cgi->param('o'))) { 
			my $m = ($cgi->param('m') - 1) + $cgi->param('o'); 			
			my $y = $cgi->param('y');
			if ($cgi->param('m') == 1 && $cgi->param('o') == -1) { 
				$m = 11; 
				$y -= 1; 
			} 
			if ($cgi->param('m') == 12 && $cgi->param('o') == 1) { 
				$m = 0; 
				$y += 1; 
			} 
			my $ts = timelocal(0, 0, 0, 1, $m, $y); 
			$get{'m'} = (localtime($ts))[4] + 1;
			$get{'y'} = (localtime($ts))[5] + 1900;
		} 
	# See the comments above regarding arguments cal takes. If nothing's passed
	# via the querystring then the cal will just draw a calendar for the current
	# month. 
	} else { 
		$get{'m'} = (localtime(time))[4] + 1; 
		$get{'y'} = (localtime(time))[5] + 1900;
	} 
	# 
	my $grid = `$calbin $get{'m'} $get{'y'}`; 
	# Processing to get the calendar grid returned by cal into a shape we can use
	# in the web page. There's not a whole lot to say about this. It should be
	# reasonably straightforward. 
	if ($grid ne "") { 
		my @rows = split("\\n", $grid); 
		if ($#rows+1 > 0) { 
			$page = '<div id="calendar">'."\n\t".'<div class="grid">'; 
			for (my $rowno = 0; $rowno < $#rows+1; $rowno++) { 
				$page .= "\n\t\t".'<div class="'. ($rowno == 0 ? 'month_year_' : ($rowno == 1 ? 'day_name_' : '')) .'row">';
				if (trim($rows[$rowno]) ne "") { 
					my @cells = split("\\s+", trim($rows[$rowno])); 
					if ($#cells+1 > 0) { 
						if ($cells[0] =~ /(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i) { 
							$page .= "\n\t\t\t".'<div class="tool_cell"><a href="?f='. $cgi->param('f') .'&m='. $get{'m'} .'&y='. $get{'y'} .'&o=-1">&lt;&lt;</a></div>';
						} 
						if ($rowno == 0) { 
							$page .= "\n\t\t\t".'<div class="cell" style="width: 120px;">'. $cells[0] ." ". $cells[1] .'</div>';
						} else { 
							if ($rowno == 2) { 
								if (7 - ($#cells+1) > 0) { 
									for (my $i = 0; $i < (7 - ($#cells+1)); $i++) { 
										$page .= "\n\t\t\t".'<div class="filler_cell">&nbsp;</div>';
									} 
								} 
							} 					
							foreach my $cell (@cells) { 
								if ($rowno == 1) { 
									$page .= "\n\t\t\t".'<div class="cell">'. $cell .'</div>';
								} else { 
									# If we get a field name, $_GET['f'], then we know the to 
									# include the JavaScript to set the field's value from 
									# whatever cell was clicked. 
									if (defined($cgi->param('f')) && $cgi->param('f') ne "") { 
										$page .= "\n\t\t\t".'<div class="cell"><a href="#" onclick="javascript:setDate(\''. $cgi->param('f') .'\',\''. $get{'y'} .'-'. $get{'m'} .'-'. $cell .'\');return false;">'. $cell .'</a></div>';
									} else { 
										$page .= "\n\t\t\t".'<div class="cell">'. $cell .'</div>';
									} 
								} 
							} 
						} 
						if ($cells[0] =~ /(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i) { 
							$page .= "\n\t\t\t".'<div class="tool_cell" style="float: right;"><a href="?f='. $cgi->param('f') .'&m='. $get{'m'} .'&y='. $get{'y'} .'&o=1">&gt;&gt;</a></div>';
						} 
					} 
				} 
				$page .= "\n\t\t".'</div>';
			} 			
			$page .= "\n\t\t".'<div class="row" style="clear:left">'."\n\t\t\t".'<div class="footer" style="width:100%;text-align:center"><a href="javascript:window.close();">Close</a></div>'."\n\t\t".'</div>'."\n";
			$page .= "\n\t".'</div>'."\n".'</div>'."\n"; 
			# Rip out tabs, carriage returns and newline characters if we're not 
			# concerned about making easy to read markup. 
			if (! $grokable) { 
				$page =~ s/\t|\r|\n//g; 
			} 
		} else { 
			$page = '<em>Whoops. Something went wrong trying to fetch the calendar.</em>';
		} 
	} 
} else { 
	$page = '<em>Whoops. Can\'t draw a calendar. Reason: `cal` doesn\'t exist at `'. $calbin .'`. Maybe you need to open a shell and run `whereis cal`?</em>';
} 
print $cgi->header(); 
print <<EOHTML;
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title></title>
<style type="text/css" title="text/css" media="screen">
/* <![CDATA[ */
\@import url('$http_root/css/packawhallop/calendar.css');
/* ]]> */
</style>
<script type="text/javascript" language="javascript" charset="utf-8" src="$http_root/js/packawhallop/calendar.js"></script>
<!--[if IE 6]>
<style type="text/css" title="text/css" media="screen">
\@import url('$http_root/css/packawhallop/calendar_ie6.css');
/* ]]> */
</style>
<![endif]-->
<script type="text/javascript" language="javascript" charset="utf-8" src="$http_root/js/packawhallop/calendar.js"></script>
</head>

<body>
EOHTML
print $page; 
print "</body></html>"; 
exit 0;