#!/nau/share/bin/perl
#    Last change:  GEK  11 Jan 98    7:29 pm
#


$|=1;


#http://www.440.com/twtd/t080898.html

#require '../cgi-bin/wwwgrab.pl';
#require '../cgi-bin/getTime.pl';

use LWP::UserAgent;

srand(time^($$ + $$<<15));

$countFile = '/home/gek/For_janHP/janHP_count';
$quoteFile = '/home/gek/For_janHP/quotes';
$revised = scalar(localtime((stat($0))[9]));

my $sky_wx = 0;
my $temp = 1;
my $dewPoint = 2;
my $relativeHumid = 3;
my $wind = 4;
my $pressure = 5;



%days = (
		sun => Sunday,
		mon => Monday,
		tue => Tuesday,
		wed => Wednesday,
		thu => Thursday,
		fri => Friday,
		sat => Saturday
		);
		
%months = (
			jan => January,
			feb => February,
			mar => March,
			apr => April,
			may => May,
			jun => June,
			jul => July,
			aug => August,
			sep => September,
			oct => October,
			nov => November,
			dec => December
		);
		

@daysKeys = keys %days;
@monthsKeys = keys %months;

open (COUNTTEXT, $countFile) || die "Cant open $countFile: $!\n";

$count = <COUNTTEXT>;
$count++;

close (COUNTTEXT);

open (COUNTTEXT, ">$countFile") || die "Cant open $countFile: $!\n";
flock(COUNTTEXT, 2);
print COUNTTEXT $count;
flock(COUNTTEXT, 8);
close (COUNTTEXT);


open (QUOTES, $quoteFile) || die "Cant open $quoteFile: $!\n";
@quotes = <QUOTES>;
close(QUOTES);

$theQuote = $quotes[rand @quotes];

undef @quotes;

$theQuote =~ s/^(.*)<i>/"$1"  <i>/;

my($mday,$mon,$year) = (localtime(time))[3,4,5];
$mon++;

$gYear = $year+1900;

$mday = "0$mday" if $mday < 10;
$mon = "0$mon" if $mon < 10;
my($yearDigits) = $year =~ /(\d\d)$/;
my($documentURL) = "http://www.440.com/twtd/t$mon$mday$yearDigits.html";


$theTime = &getTime;

$theTime =~ s|^(.*, )(.*?),|$1<A HREF="$documentURL" title="Those Were the Days, Today in History">$2</A>,|;


#$theReport = &grabURL;

my($userAgent) = new LWP::UserAgent;
my($request) = new HTTP::Request 'GET' => "http://iwin.nws.noaa.gov/pub/data/text/ASUS45/KPHX.TXT";
my($resource) = $userAgent->request($request);
if($resource->is_success) {
   $theReport = $resource->content;
   } else {
      print "Can't get resource\n\n";
      exit;
   }

# http://enc.org/text/thisweek/news/educationheadlines/



my($date) = $theReport =~ /(.*$gYear)/;
$date =~ s/00 /:00 /;
$date =~ s/ AM / a\.m\. /;
$date =~ s/ PM / p\.m\. /;
$date =~ s/MST/MST -/;

@results = grep($date =~ /$_/i, @daysKeys);
$date =~ s/$results[0]/$days{$results[0]},/i;

@results = grep($date =~ /$_/i, @monthsKeys);
$date =~ s/$results[0]/$months{$results[0]}/i;

$date =~ s/( $gYear)/,$1/;

($theDay) = $date =~ /(\d+), $gYear/;

$end = &theOrdinal($theDay);

$theDay = "$theDay$end";

$date =~ s/(\d+), $gYear/$theDay, $gYear/;



my(@items) = $theReport =~ /flagstaff +([a-z]+ ?[a-z]+?) +(-?\d+) +(-?\d+) +(\d+) +(\w+) +(\S+)/i;

if($date) {
	$date = "at $date";
} else {
	$date = 'is being updated.';
}



if(@items < 6) {
	undef @items;
	for(0..5) {
		push(@items, 'Updating');
	}
}


@backgrounds = `ls Graphics/Backgrounds`;
$background = $backgrounds[rand ($#backgrounds+1)];


print "Content-type: text/html\n\n";


print <<fin;
<HTML>
<HEAD>
   <TITLE>Gary E. Karcz</TITLE>

</HEAD>
<BODY bgcolor="F3EDB1">



   <FONT SIZE="7">Gary E. Karcz</FONT>
   
   <br><FONT SIZE="3" face="Arial, Helvetica, sans-serif">Locally: $theTime</FONT>
    <center><HR>
   <FONT SIZE="2"> Flagstaff Weather $date<br><STRONG>Sky/WX</STRONG>: $items[$sky_wx]; <STRONG>Temp:</STRONG> $items[$temp]&#176;<FONT SIZE="-1">F</FONT>; <STRONG>Dew Pt:</STRONG> $items[$dewPoint]&#176;<FONT SIZE="-1">F</FONT>; <STRONG>Rel. Humidity:
   </STRONG> $items[$relativeHumid]%;<STRONG>Wind:</STRONG> $items[$wind]; <STRONG>Barometer:</STRONG> $items[$pressure]</FONT>
	<HR>
    </center>
  
   <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=6 WIDTH="50%">

   <TR>
   <TD>

<IMG SRC="GKarcz.jpg" alt="A picture of Gary E. Karcz">

   </TD>

      <TD nowrap valign=top aligh=left>
         
         
         <P>
         Faculty<br>
         <a href="http://coe.nau.edu/" title="College of Education's Home Page">College of Education</a>
         <br>
         Northern Arizona University<br>
         Box 5774<BR>         
         Flagstaff, AZ 86011-5774<BR>
         
         Office: 928.523.8228
      </TD>
      
      <TD nowrap valign=top aligh=left>
       
      </TD>
      
      
      </TR>
</TABLE>
<center>   
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=2 WIDTH="80%">
      <TR>
      <TD>
      <FONT SIZE=2><CENTER>$theQuote</CENTER></FONT>
      </TD>
      </TR>
</TABLE>

<BR>
   <HR>
</center>
 <table width="100%" border="0">
  <tr>
    <td nowrap><CITE><FONT SIZE="1">Revised: $revised - $count visits since 2/22/97</FONT></CITE></td>
    <td align=right nowrap><font size="1">
   <a href="http://www.nau.edu/"  title="Northern Arizona University's Home page">Northern Arizona University</a>
   </font></td>
  </tr>
</table>
   
</BODY>
</HTML>
fin



sub theOrdinal {
       my($number) = @_;
       my($ending);
       my($hunds) = $number % 100;
       my($tens) = $number % 10;
       if ($hunds < 10 || $hunds > 13) {
               if ($tens == 1) {
                       $ending = 'st';
               } elsif ($tens == 2) {
                       $ending = 'nd';
                       } elsif ($tens == 3) {
                       $ending = 'rd';
               } else {
                       $ending = 'th';
               }
       } else {
               $ending = 'th';             # Catch "eleventh, twelveth, thirteenth" etc.
       }
}



sub getTime {

	my($weekday) = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)[(localtime)[6]];
	my(@month) = ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September','October', 'November', 'December');
	my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	my($theMonth) = $month[$mon];
	
	my($meridian) = "p.m.";
	if ($hour < 12) {
		$meridian = "a.m.";
	}
	my($suffix) = &ordinal($mday);
	$hour = &hourchange($hour);
	$year += 1900;
	my($formattedTime) = sprintf "%s, $theMonth %2.0d%s, $year at %0.0d:%0.2d:%0.2d %s",$weekday,$mday,$suffix,$hour,$min,$sec,$meridian;

	$formattedTime;
	
	
	#  Change the hour format to that of 12 hour
	#
	sub hourchange {
		my($h) = @_;
		if ($h > 12) {
			$h = ($h - 12);
		}
		if ($h == 0) {
			$h = 12;
		} else {
			$h = $h;
		}
	}
	
	
	
	#  Define the ending for numbers (i.e. 20th, 13th, 3rd, etc.)
	#
	sub ordinal {
		my($number) = @_;
		my($hunds) = $number % 100;
		my($tens) = $number % 10;
		if ($hunds < 10 || $hunds > 13) {
			if ($tens == 1) {
				my($ending) = "st";
			} elsif ($tens == 2) {
				$ending = "nd";
				} elsif ($tens == 3) {
				$ending = "rd";
			} else {
				$ending = "th";
			}
		} else {
			$ending = "th";             # Catch "eleventh, twelveth, thirteenth" etc.
		}
	}
}




