#!/usr/bin/perl

use		CGI qw(:standard);
use		CGI::Carp(fatalsToBrowser);

BEGIN {
	if ($ENV{"SERVER_NAME"} =~ m"localhost") {
		unshift @INC, "c:/prog/perl/elsnet/cgi-bin/lib";
	} else {
		unshift @INC, "lib";
	}
	require SSI2;
	require Date;
	require paths2;
	require Elsnet::Subs;
	import SSI2;
	import Date;
	import paths2;
	import Elsnet::Subs;
}

&checkQUERY();

print header();

# original: my $file			=	"$cgi_directory/html/deadlines_calendar.html";
# sk 09-06-2005: include date received
my $file			=	"$cgi_directory/html/deadlines_calendar-sk.html";
my $key				=	"deadline";
my $minimum_date	=	countDate(thisDate());

my %data			=	%{ load_database($key, $database, $minimum_date) };
print ssi($file, $root_directory, \%data);

sub load_database {
	my $key			=	$_[0];
	my $database	=	$_[1];
	my $minimum		=	$_[2];
	my %data		=	();

	open(DATABASE, "$database") || cgiDie("<p>No entries found.</p>");

	SWITCH: {
		%data = %{ startdate_key($minimum) },	last SWITCH	if $key eq "startdate";
		%data = %{ received_key($minimum) },	last SWITCH	if $key eq "received";
		%data = %{ deadline_key($minimum) },	last SWITCH	if $key eq "deadline";
		%data = %{ category_key() },			last SWITCH	if $key eq "category";
		%data = %{ file_key() },				last SWITCH	if $key eq "file";
		%data = %{ subject_key() },				last SWITCH	if $key eq "subject";
	}

	close DATABASE;
	return \%data;
}

sub startdate_key {
	my $minimum		=	$_[0];
	my $event		=	0;	# unieke id voor elke event
	
	while (<DATABASE>) {
		chomp;
		my ($file,$cat,$subj,$rcvd,$startdate,$deadline,$visible) = split /\t/ , $_;
		if ($visible) {
			if ($startdate =~ /^\d{8}$/ && $startdate >= $minimum && correct_cat($cat)) {
				$event++;
				$startdate		=~	/(....)(..)(..)/;
				my ($year, $month, $day)	=	($1, $2, $3);
				
				$data{$year}{$month}{$day}{$event}{"startdate"}	=	date_to_words($startdate);
				$data{$year}{$month}{$day}{$event}{"category"}	=	$cat;
				$data{$year}{$month}{$day}{$event}{"deadline"}	=	date_to_words($deadline);
				$data{$year}{$month}{$day}{$event}{"file"}		=	$file;
				$data{$year}{$month}{$day}{$event}{"subject"}	=	$subj || "*** No subject ***";
				$data{$year}{$month}{$day}{$event}{"received"}	=	date_to_words($rcvd);
			}
		}
	}	# end while
	return \%data;
}	

sub deadline_key {
	my $minimum		=	$_[0];
	my $event		=	0;	# unieke id voor elke event
	
	while (<DATABASE>) {
		chomp;
		my ($file,$cat,$subj,$rcvd,$startdate,$deadline,$visible) = split /\t/ , $_;
		if ($visible) {
			if ($deadline =~ /^\d{8}$/ && $deadline >= $minimum && correct_cat($cat)) {
				$event++;
				$deadline		=~	/(....)(..)(..)/;
				my ($year, $month, $day)	=	($1, $2, $3);
				
				$data{$year}{$month}{$day}{$event}{"startdate"}	=	date_to_words($startdate);
				$data{$year}{$month}{$day}{$event}{"category"}	=	$cat;
				$data{$year}{$month}{$day}{$event}{"deadline"}	=	date_to_words($deadline);
				$data{$year}{$month}{$day}{$event}{"file"}		=	$file;
				$data{$year}{$month}{$day}{$event}{"subject"}	=	$subj || "*** No subject ***";
				$data{$year}{$month}{$day}{$event}{"received"}	=	date_to_words($rcvd);
			}
		}
	}	# end while
	return \%data;
}	

sub correct_cat {
	my $cat	=	lc($_[0]);
	my %cats	=	qw(e-cfp 1 e-conf 1 e-announce 1 e-sschool 1);
	return $cats{$cat};
}

sub date_to_words {
	my $date	=	$_[0];
	return "" unless $date	=~	/(....)(..)(..)/;
	my ($year, $month, $day)	=	($1, $2, $3);
	$month		=	month_to_words($month);
	if ($day eq "00") {
		$day	=	"";
	}
	return (join " ", ($day, $month, $year));
}

sub month_to_words {
	my $month	=	$_[0];
	my %month	=	qw(	01 Jan 02 Feb 03 Mar 04 Apr 05 May 06 Jun 
						07 Jul 08 Aug 09 Sep 10 Oct 11 Nov 12 Dec);
	$month	=	$month{$month} || "";
	return $month;
}

sub print_hash {
	my %hash	=	%{ $_[0] };
	for (keys %hash) {
		print "$_ is $hash{$_}<br>\n";
		if ($hash{$_} =~ /HASH/) {
			print_hash( $hash{$_} );
		}
	}
}
