####################################################################
#
#This software is released under the GPL.  Please
#see the included LICENSE file.
#
#Copyright (C) 2001  Michael Spiceland
#Copyright (C) 2001  Jay Kline
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#Use this script at your own risk!  I make no 
#warranties that it is hack proof!
#
# This script is part of the Fuzzymonkey Perl Script Archive at
# www.fuzzymonkey.net/perl/
#
#
####################################################################


#Common subroutines used in may of my scripts..

$version = 2.7;

sub get_appointments ($){
	my $app_day;
	my $app;
	my %apps;
	if (open(INFILE, "<$_[0]")){
		while(<INFILE>){
			unless(/^#/){ # ignore comments
				chomp;
				#($app_day,$app) = split(/\t/,"$_");
				#$apps{"$app_day"} = "$app";
				
				#push(@{$appointments{"$month-$day-$year"}},"$appointment_on_day");
	
				($date,$text,$id) = split(/\t/,$_);
				push(@{$apps{"$date"}},"$text");
			}
		}
		close INFILE;
	}

	return %apps;
}

sub add_appointments($%){
	my ($file,%apps) = @_;

	$return .= "adding from $file<br>";

	if(-e "$file"){
		if(open(APPOINTMENTS,"<$file")){
			flock(2,APPOINTMENTS);
			while(<APPOINTMENTS>){
				unless(/^#/){ # ignore comments
					chomp;
					($date,$text,$id) = split(/\t/,$_);
					push(@{$apps{"$date"}},"$text");
				}
			}
			flock(8,APPOINTMENTS);
			close(APPOINTMENTS);
		}
	}else{
		#printpage("Error: $file does not exist but I was asked to add appointments from
		#	this file to this calendar.  Email support\@fuzzymonkey.org or join their mailinglist for
		#	more help!");
		#exit(3);
	}
	foreach $key (keys %apps){
		#$return .= "$key<br>";
	}

	return %apps;
	
}

sub dienice {
	my($msg) = @_;
	print <<HTML;
	<br>
<center><table width="400">
	<tr bgcolor="#FC3C3C"><td align="middle"><b><big>!! Oops !!</big></b></td></tr>
	<tr bgcolor="lightgrey"><td>$msg</td></tr>
</table></center>
HTML
	printendhtml();
	exit;
}

sub printpage($){
	my $content = $_[0];
	$content .= "<br><center><font class=smalltext>Powered by My Calendar $version. Copyright 2002-2006 FuzzyMonkey.net.<br>
Created by the scripting wizards at <a href=http://fuzzymonkey.net>FuzzyMonkey.net</a>.</font></center>";
	open (TEMPLATE, "template.html")||dienice("Content-type: text/html\n\nCould not find template");
	while (<TEMPLATE>){
		$page .= $_;
	}
	$page =~ s/<!-- ?content ?-->/$content/ig;
	$page =~ s/<head>/<head>\n<link rel=\"stylesheet\" href=\"$csslink\" type=\"text\/css\">/ig;
	close (TEMPLATE);
	print "Content-type: text/html\n\n$page";

}

sub printstarthtml 
{
	my $backgroundhtml = '';
	if ($background) {$backgroundhtml = "background=\"$htmlroot/$background\""};
	my $logohtml = '';
	
print <<HTMLHEADER;
Content-type: text/html


<html>
<head>
<STYLE>
.JUSTIFY {text-align : justify}
A {color : #0000FF; text-decoration : none}
</STYLE>
    <title>$title</title>
</head>
<body $backgroundhtml bgcolor="$bgcolor" link="#0000FF" vlink="#000080" alink="#FF0000" LEFTMARGIN=0 TOPMARGIN=0 MARGINHEIGHT=0 MARGINWIDTH=0>
HTMLHEADER

	if (open(INFILE, $header )){
		while($tmp = <INFILE>){
			# $tmp = <INFILE>;
			# $tmp =~ s{/\<\?..*?\?\>}{}gs; 
			if ($tmp =~ /^\<\?/i) {
					
			} else {
				print $tmp;
			}
		}
		close INFILE;
	}
	print "\n\n";
}


sub printendhtml {
 
        if (open(INFILE, $footer )){
                while($tmp = <INFILE>){
                        # $tmp = <INFILE>;
                        # $tmp =~ s{/\<\?..*?\?\>}{}gs;
                        if ($tmp =~ /^\<\?/i) {
 
                        } else {
                                print $tmp;
                        }
                }
                close INFILE;
        }
        print "\n\n";

print <<HTMLEND
</body>
</html>
HTMLEND

}

sub prettytime {
	#converts the first argument as the number of seconds since Jan1,1970 into a readable string
	# and returns it.
	@days = ("Sun","Mon","Tues","Wed","Thur","Fri","Sat","Sun");
	@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	($sec,$min,$hr,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($_[0]);
	$longyr = $year + 1900;
	$fixmo = $mon + 1;
	
	if($hr > 12)  {
		$hr = $hr - 12;
		$ampm = "PM";
	} else {
		$ampm = "AM";
	}
	
	$yr2 = substr($longyr,2,2);
	
	$time = sprintf("%02s %02d, %04d",
                          $months[$mon],$mday,$longyr);
	return $time;
	
}

sub email ($$$$) 
{ 
    my ($subject, $to, $from, $body) = @_;

    open (MAIL, "| $sendmail -n -t" ) || dienice("could not open mail");
    print MAIL <<MAIL_MESSAGE;
Subject:$subject
To:$to
Reply-to:$from
From:$etitle
$body
MAIL_MESSAGE
     close MAIL;
}


return 1;
