#!/usr/local/bin/perl
require "/home/todd/toddweb.env";
require "weblib.lib";
use DB_File;
$style=<<EOF;
table {margin-left: auto; margin-right: auto;}
td {font: 8pt/10pt Verdana, Sans Serif; width: 110px; vertical-align: top; min-width: 2.7em;}
th {min-width: 2.7em;}
.month {font: 8pt/10pt Verdana, Sans Serif;}
.caption {font: 10pt/12pt Verdana, Sans Serif;}
EOF
($sec,$min,$hour,$mday,$mon,$year,$wday)=localtime(time);
@month=(January,February,March,April,May,June,July,August,September,October,November,December);
@monthlen=(31,28,31,30,31,30,31,31,30,31,30,31);
$monthnum=0;
foreach $month (@month){$month{$month}=$monthnum++;}
$month=$month[$mon];
$months=join(":",@month);
dbmopen(%CAL,"$PAGEHOMEDIR/db/utopiacal",0644);
&ReadParse;
foreach $date(keys %in){
	next unless $in{$date};
	($key)=($date=~/^D(\d+)/);
	if($in{$date} eq " "){delete $CAL{$key};}
	else{$CAL{$key}=$in{$date};}
}
$edit=($ENV{PATH_INFO}=~s#/edit##);
if($ENV{PATH_INFO}){
	($junk,$mon,$year)=split(m#/#,$ENV{PATH_INFO});
	if($mon>12){$year=$mon;$full=1;$monbegin=0;$monend=11;}
	else{$mon=$mon-1;$monbegin=$mon;$monend=$mon}
	$year=$year>1900?$year-1900:$year;
}
else{$monbegin=$mon;$monend=$mon;}
$fullyear=$year+1900;
$caption="$month[$mon] $fullyear";
$full?&htmlopen("$fullyear|","robots=none","style=$style"):&htmlopen("$caption|","robots=none","style=$style");
print "<form action=$weblib::URL/edit$ENV{PATH_INFO} method=POST>" if $edit;
for($mon=$monbegin;$mon<=$monend;$mon++){
	$caption=qq{<div class="caption">$caption</div>};
	if($full){$caption=qq{<hr><center class="month"><a href="$weblib::URL/}.($mon+1).qq{/$fullyear">$month[$mon]</a></center>\n};}
	else{
		$caption.=qq{<hr><center class="month">| };
		for($month=1;$month<13;$month++){$caption.=qq{<a href="$weblib::URL/$month/$fullyear">$month[$month-1]</a> | };}
		$caption.= "</center>\n";
	}
	$limit=$monthlen[$mon];
	$limit+=1 if $mon==1&&leap($fullyear);
	$wday=dow($fullyear,$mon+1,1);
	for($i=0;$i<$limit;$i++){
		$key=sprintf("%02d%02d",$mon+1,$i+1);
		if($edit){$value[$i]=qq{<textarea style="font-size: 7pt;" name=D$key cols=25 rows=6 wrap=soft>$CAL{$key}</textarea>};}
		else{
			@events=split(/;/,$CAL{$key});
			foreach $event(@events){
				($printyear,$event)=split(/:/,$event,2);
				if($printyear<100){$printyear+=1900;}
				$event="$printyear:$event";
			}
			@events=sort @events;
			$value=join("<hr>",@events);
			$value[$i]="$value";
		}
	}
	$cal=&htmlcal($wday,$limit,@value);
	if($monbegin==$monend){print $cal;}
	else{push(@cal,$cal);}
}
if(@cal){
	print &htmltable($fullyear);
	for($i=0;$i<@cal;$i+=3){print &htmlrowdata(@cal[$i..$i+2]);}
	print &htmltableend;
}
if($edit){print "<input type=submit></form>\n";}
print $PAGETAIL;
&htmlclose;
sub htmlcal{
	local($wday,$limit,@value)=@_;
	local($i,$j,$k,@week);
	local($cal);
	$cal=&htmltable("$caption","border");
	$cal.=&htmlrowhead("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	opendir(GIFS,"$PAGEHOMEDIR/www/pictures/calicons");
	@img=grep(!/^\./,readdir(GIFS));
	closedir(GIFS);
	srand;
	for($i=0;$i<6;$i++){
		@week=@week2=();
		for($j=0;$j<7;$j++){
			$k=$i*7+$j-($wday-1);
			if($k<1||$k>$limit){
				$imgindex=int(rand($#img+1));
				$img=$img[$imgindex];
				splice(@img,$imgindex,1);
				$img=$edit?"":"<center><img src=/$HTMLPATH/pictures/calicons/$img></center>";
				push(@week,"");
				push(@week2,$img) unless $full;
			}
			else{
				push(@week,"<b>$k</b>");
				push(@week2,$value[$k-1]) unless $full;
			}
		}
		$cal.=&htmlrowdata(@week);
		$cal.=&htmlrowdata(@week2);
		last if $k>=$limit;
	}
	$cal.=&htmltableend;
	return $cal;
}
sub dow{
	local($y,$m,$d)=@_;
	(int(23*$m/9)+$d+4+($m<3?$y--:$y-2)+int($y/4)-int($y/100)+int($y/400))%7;
}
sub leap{
	local($year)=@_;
	($year%4==0&&($year%100!=0||$year%400==0));
}
