#!/usr/local/bin/perl

##### USAGE: age.pl 01/13/1974
##### USAGE: or age-<name>.pl, where <name> is a filename symbolically
#####        linked to age.pl, the person's name appearing after the
#####        hyphen.  It is split and compared against a table of 
#####        birthdays.  This is because sometimes there is no other way
#####        to pass parameters other than by filename.
##### OUTPUT: 22.54, where that is the age in years

require "../lib/constants.pl";

##### EXECUTION OPTIONS:
$MAKE_LINK=1;			#makes age given a link, if clicked on provides an explanation of the system.

##### WWW LOCATION OPTIONS:
$PATH_TO_THIS_CGI = "~clint/cgi/age";

##### BIRTHDATE TABLE:
%BIRTHDAYS = 
	("clint",   	 "01/13/1974",
	 "clint_2",	 "01/13/1974",
	 "britt",   	 "06/30/1979",
	 "dad",	    	 "03/01/1948",
	 "mom",	    	 "04/02/1947",
	 "carolyn", 	 "02/23/1976",
	 "carolyn_2", 	 "02/23/1976",
	 "relationship", "02/10/1992",
	 "marriage",     "02/10/2000",
	 "warlord",      "02/04/1978",
	 "warlord_2",    "02/04/1978",
	 "lacey",        "07/23/1978",
	 "smeg",	 "10/30/1974",
	 "smeg_2",	 "10/30/1974",
	 "y2k_2",	 "01/01/2000",
	 "nza",          "08/22/1976",
	 "matt_nelson",  "06/11/1999",
	 "kate",         "01/16/1978"
	);

##### EXTRA TABLES FOR WHEN WE WANT MORE THAN 2 DECIMALS:
%DECIMALS = 
	("clint_2",	8,
	 "carolyn_2",   8,
	 "warlord_2",	8,
	 "smeg_2",	8,
	 "y2k_2",	8,
	 "nza",         8,
	 "kate",        8,
	);
%BIRTHTIMES =	#hour and minute of day I was born
	("clint_2",	"20:00",
	 "carolyn_2",   "14:30",
	 "warlord_2",	"04:00",	#"try 4 am"
	 "smeg_2",	"01:00",
	 "y2k_2",	"00:00",
	 "nza",         "04:43",
	 "matt_nelson", "18:58",
	 "kate",        "23:59"
	);
%USE_REFRESH =	#set to refresh interval if you want auto-refresh
		#DO NOT USE THIS OPTION WHEN EMBEDDED IN A STATIC HTML!!
	("clint_2",	"2",
	 "carolyn_2",   "2",
	 "warlord_2",	"3",
	 "smeg_2",	"3",
	 "y2k_2",	"3",
	 "nza",		"5",
	 "kate",	"3"
	);

##### DEBUG OPTIONS:
#$DEBUG_PARAMETER=1;
#$DEBUG_AGE=1;

##### LIBRARY REQUIREMENTS:
require "../lib/date.pl";
require "../lib/time.pl";
require "../lib/header.pl";

##### PRINT HEADER:
print "Content-type: text/html\n\n";	#html was 'plain'

##### COMMAND-LINE ERROR-CHECKING (1):
if ("$ARGV[0]" eq "") {
	if ($DEBUG_PARAMETER) { $debugcount=0; }
	if ($DEBUG_PARAMETER) { print "\$parameter (",$debugcount++,") is \"$parameter\"\n"; }

	$current_directory=`pwd`;
	$parameter=$0;	#the filename		#/blah/blah/age-whatever.pl

	if ($DEBUG_PARAMETER) { print "\$parameter (",$debugcount++,") is \"$parameter\"\n"; }

	if ($parameter =~ /\//) {

		$parameter =~ s/.*\///g;	#/age-whatever.pl

		if ($DEBUG_PARAMETER) { print "\$parameter (",$debugcount++,") is \"$parameter\"\n"; }
	}#endif there's a slash in the filename

	$parameter =~ s/^\///;			#age-whatever.pl

	if ($DEBUG_PARAMETER) { print "\$parameter (",$debugcount++,") is \"$parameter\"\n"; }

	$parameter =~ s/\.pl$//;		#age-whatever

	if ($DEBUG_PARAMETER) { print "\$parameter (",$debugcount++,") is \"$parameter\"\n"; }
	
	if ($parameter eq "age") {
		print "[[ERROR: NO BIRTHDAY GIVEN]] [[$0]]\n";
		exit; 
	} elsif ($parameter =~ /-/) {
		($junk,$entity) = split(/-/,"$parameter");
		
		if    ($entity eq "explanation")  { &special_explanation; }
		elsif ($BIRTHDAYS{$entity} ne "") { 
			$ARGV[0] = $BIRTHDAYS{$entity};	
			$temp = &print_age($ARGV[0],$entity);
			print "$temp";
		} else {	
			print "[[ERROR: WHAT THE HELL IS \"$entity\"??]]\n";		
		}#endif

	} else {
		print "[[ERROR: WHAT THE HELL AM I SUPPOSED TO DO??]]\n";
		exit;
	}#endif
}#endif








##############################################################################################################################################
sub print_age {
my $birthday=$_[0];
my $entity  =$_[1];#oh
my $temp="";
my $NUM_DECIMALS=2;	#default number of decimal places
my $junk,$hour,$minute,$second="";
my $num_hours,$num_minutes,$num_seconds="";
my $MINUTE_PRECISION=0;
my ($retval)="";


##### USE REFRESH IF NEEDED:
$refresh_interval = $USE_REFRESH{$entity};
my $refresh_url="$MACHINEURL/$PATH_TO_THIS_CGI/age-" . $entity . ".pl";
if ($refresh_interval ne "") {
$retval .= <<__EOF__;
<HEAD><meta http-equiv="Refresh" 
content="$refresh_interval; $refresh_url"></HEAD>
<BODY BGCOLOR=000000 LINK=FF0000 VLINK=FF0000>
__EOF__
}#endif

if ($DECIMALS{$entity} ne "") {
	$MINUTE_PRECISION=1;
}#end

($birthmm,$birthdd,$birthyear)=split(/\//,"$birthday");

if ($birthyear !~ /..../) {
	$retval .= "[[ERROR: BIRTHYEAR MUST BE 4 DIGITS! (ie 12/31/1974)]]\n";
	exit;
}#endif

##### GET TODAY'S DATE INFORMATION:
($year,$yy,$mm,$dd)=&old_today;
if ($MINUTE_PRECISION) {
	($junk,$hour,$minute,$second)=&time_now;
}#endif

##### COMPUTE AGE:
$num_years  = $year - $birthyear;
$num_months = $mm   - $birthmm;
$num_days   = $dd   - $birthdd;
if ($MINUTE_PRECISION) {
	my $birthsecond=0;	#we don't record births to the second
	my $birthhour,$birthminute=split(/:/,"$BIRTHTIMES{$entity}");
	$num_hours   = $hour   - $birthhour;
	$num_minutes = $minute - $birthminute;
	$num_seconds = $second - $birthsecond;
}#endif


if ($DEBUG_AGE) {
	print "\$num_years  = \"$num_years\" \n";
	print "\$num_months = \"$num_months\" \n";
	print "\$num_days   = \"$num_days\" \n";
	print "\$num_months / 12  = \"", $num_months / 12,  "\" \n";
	print "\$num_days   / 365 = \"", $num_days   / 365, "\" \n";
	print "\$year = \"$year\" \n";
	print "\$mm   = \"$mm\" \n";
	print "\$dd   = \"$dd\" \n";
	print "\$birthyear = \"$birthyear\" \n";
	print "\$birthmm   = \"$birthmm\" \n";
	print "\$birthdd   = \"$birthdd\" \n";
#	print "\$ = \"$\" \n";
}#endif

##### Funny how things have a way of working out:
if (!$MINUTE_PRECISION) {
	$age = $num_years + ($num_months / 12) + ($num_days / 365);
} else {
	#525,600 minutes in a year, 31,536,000 seconds in a year
	$age = $num_years + ($num_months / 12) + ($num_days / 365) +
		($num_minutes / 525600) + ($num_seconds / 31536000);
}#endif


if ($DECIMALS{$entity} ne "") {
	$NUM_DECIMALS = $DECIMALS{$entity};
	$retval .= "<font size=5>";
}#endif

$age_for_printing = sprintf("%2." . "$NUM_DECIMALS" . "f",$age);

if ($MAKE_LINK==1) {
	$retval .= "<a target=\"_ageexpl\" onMouseOver=\"window.status='*** Click for age explanation ***';return true\" ";
	$retval .= "onMouseOut=\"window.status='OH!';return true\" ";
	$retval .= "href=\"$MACHINEURL/$PATH_TO_THIS_CGI/age-explanation.pl\">";
}#endif
if ($refresh_interval ne "") { $retval .= "<font color=\"#FF0000\">"; }
$retval .= "$age_for_printing";
if ($refresh_interval ne "") { $retval .= "</font>"; }
if ($MAKE_LINK==1) {
	$retval .= "</a>";
}#endif
$retval .= "\n";

if ($refresh_interval ne "") {
	$retval .= "</font></BODY>";
}#endif

return($retval);
}#endsub print_age
##############################################################################################################################################














############################################################################
sub special_explanation {
my %OUTPUT=();
my $key="";
my $temp="";

print <<__EOF__;
<body bgcolor="#000000" text="#DDDDDD" vlink="#FFFFFF" link="#FFFFFF">

<a href="#expl"><font size=2>[Press your browser's stop button, then click here to go directly to the technical explanation below]</font></a>
<BR>
<h1 align=center>** AGE LIST: **</h1>

__EOF__

print "<table cellpadding=1 border=1 align=center>";
foreach $ent (sort keys %BIRTHDAYS) {
	$temp  = "";
	$temp  = "<TR><TD align=right>";
	$temp .= "<a ";
	$temp .= "onMouseOver=\"window.status=";
	$temp .= "'*** Click for age of $ent entity ***';";
	$temp .= "return true\"; onMouseOut=\"window.status='OH!';return true\" ";
	$temp .= "href=\"$MACHINEURL/~clint/cgi/age/age-$ent.pl ";
	$temp .= "\">";
	$temp .= "$ent";
	$temp .= "</a></TD>";
	$temp .= "<TD align=center>";
	$temp .= $BIRTHDAYS{$ent};
	$temp .= "</TD>";
	$temp .= "<TD align=left>";
	$temp .= &print_age($BIRTHDAYS{$ent},$ent);
	$temp .= "</TD></TR>\n";	

	my ($yyyy,$mm,$dd,$key)="";
	($mm,$dd,$yyyy)=split(/\//,"$BIRTHDAYS{$ent}");
	$key = "$yyyy" . "$mm" . "$dd" . "$ent";
	#DEBUG: #print "Adding \$temp to output key is \"$key\"<BR>\n";
	$OUTPUT{$key}=$temp;
}#endforeach
foreach $key (reverse sort keys %OUTPUT) {
	print $OUTPUT{"$key"};
}#endforeach
print "</table>";

print <<__EOF__;

<a name="expl">
<h1 align=center>** DYNAMIC AGE EXPLANATION: **</h1>

All ages on my webpage are computed 
dynamically, so that they are accurate
whenever the user reads them.  Basically,
many of my hard-coded ages were at least
2 years off, and I realized I'd have to 
constantly be revising my webpage to have
accurate ages.  This must stop.<P>

To stop this, I wrote a perl script that
takes a birthdate as input, and outputs
the age.<P>

The tricky part was passing the parameter
(the date) to the script.  Since it was
not a webform, there was no get or post
form input.  I had to use a special syntax
(within my HTML) that inserts the output
of a CGI into that point in the HTML
file.  However, you could not pass
parameters using this method either.
They were ignored.  So how to pass
parameters then?  What methods remain?<P>

In order to get around the system, I
passed parameters "by filename".  I used
UNIX's symbolic linking to give my 
program, "age.pl", several different
logical filenames, like:
<UL><LI>age-clint.pl
<LI>age-carolyn.pl
<LI>age-relationship.pl
<LI>etc.
</UL><P>

When the program examines itself using
the "\$0" variable, it gets the name it
was called with (not "age.pl", but
"age-clint.pl" or "age-relationship.pl").  
I then remove the "age-" and the ".pl"
and end up with the entity name --
"clint", "carolyn", "mom", "dad",
"britt", "relationsihp", etc. etc.<P>

Once the entity is known, it is compared
against a list of birthdates.  Adding
new entities is as easy as adding a new
symbolic link, like age-newperson.pl,
 and then adding a new entity to the 
entity list along with it's birth date.<P>

The only exception are these words you
are reading right now, which are the entity 
"explanation" (filename age-explanation.pl).
That entity does not return a date but 
instead returns this page.<P>

Pretty nifty.<P>

__EOF__
exit;
}#endsub special_explanation
############################################################################

