#!/usr/local/bin/perl
require "/home/todd/toddweb.env";
require "$PAGEHOMEDIR/weblib.lib";
#use DB_File;
srand(time);
$orig=0;
do "$PAGEHOMEDIR/local.inc";
chop($ENV{PATH_INFO}) if $ENV{PATH_INFO}=~m#/$#;
if($ENV{PATH_INFO} eq ""){
	&defuser($PAGEADMIN,@PAGEADMINADDR);
	($pageadmin,$domain)=split(/@/,$PAGEADMIN);
	($access,$accessnum,$totalvisitors,$totalaccess)=&trpagestats("$PAGEHOMEDIR/db/trconn");
	foreach $vote (keys %VOTE){
		$v{$vote}=1 if $access =~ /:vote$vote:|:vote$vote$/;
	}
	&webmail($PAGEADMIN,$PAGENAME,$access,$totalvisitors,$totalaccess)
		if(!$HTTPUSER{$pageadmin});
}
$description=<<EOF;
description=The Todd Rundgren Connection has just about anything you might like
to know about Todd Rundgren, including lyrics, pictures, biography and concert
schedules.
EOF
$fontsize=&getcookie("TRConn Font Size");
$fontsize="" unless $fontsize eq "big" || $fontsize eq "bigger" || $fontsize eq "small" || $fontsize eq "smaller";
$style="http://www.rocemabra.com/trc/trc$fontsize.css";
&htmlopen("$PAGENAME|","keywords=Todd Rundgren,Rundgren,Todd,TR-i",$description,"style=$style",
	"rss=RSS - TR News Feed:http://TRConnection.com/news/rss");
#print qq{<center><a href="http://www.PetitionOnline.com/toddrund/"><img src="http://TRConnection.com/banners/todd_rundgren_induct1.gif" width="468" height="60"></a></center>\n};
($sec,$min,$hour,$day,$month,$year)=localtime(time);
$checkexpire=sprintf("%4d%02d%02d%02d%02d%02d",$year+1900,$month+1,$day,$hour,$min,$sec);
($sec,$min,$hour,$day,$month,$year)=localtime(time-86400*14);
$checknew=sprintf("%4d%02d%02d%02d%02d%02d",$year+1900,$month+1,$day,$hour,$min,$sec);
($sec,$min,$hour,$day,$month,$year)=localtime(time-86400);
$checkhot=sprintf("%4d%02d%02d%02d%02d%02d",$year+1900,$month+1,$day,$hour,$min,$sec);
$artpath="$PAGEHOMEDIR/articles";
open(INDEX,"$artpath/index");
while(<INDEX>){
	chop;
	if(tr/:/:/ >= 3){
		($artname,$interest,$expire,$header)=split(/:/,$_,4);
		$expire="99999999999999" if $expire eq "";
	}
	else{
		$expire="99999999999999";
		($artname,$header)=split(/:/,$_,2);
	}
	if($april1){
                $header=~s/Todd/Ted/g;
                $header=~s/Rundgren/Runger/g;
        } 
	next if $expire<$checkexpire || !-f "$artpath/$artname";
	@stat=stat("$artpath/$artname");
	$INDEX{$artname}="$stat[9]:$header";
}
&getparams;
open(INDEX,"$PAGEHOMEDIR/trconn.dat");
while(<INDEX>){
	chop;
	last if /^EOD$/;
	($tag,$articles)=split(/\t/);
	$articles{$tag}=$articles;
	$articlessave=$articles,$tagsave=$tag if "/$tag" eq $ENV{PATH_INFO} || $param{section} eq $tag;
}
while(<INDEX>){
	if($april1){
                s/Todd/Ted/g;
                s/Rundgren/Runger/g;
        } 
	($tag,$desc)=split(/\t/);
	($tag,$title)=split(/#/,$tag);
	$title{$tag}="$title#$desc";
	push(@tags,$tag);
}
if($articlessave){
	opendir(GIFS,"$PAGEHOMEDIR/www/pictures/pointers");
	@img=grep(!/^\./,readdir(GIFS));
	closedir(GIFS);
	$img=$img[(time%($#img+1))];
	($title,$desc)=split(/#/,$title{$tagsave});
	print "<center><h1>$title</h1></center>\n$desc\n";
	print <<EOF;
<hr>
<a name=index href=#><h2>Index to this section</h2></a>
<table><tr><td><img src=/$HTMLPATH/pictures/pointers/$img></td>
<td>
<ul>
EOF
	foreach $artname(split(/,/,$articlessave)){
		next unless -f "$artpath/$artname";
		($time,$header)=split(/:/,$INDEX{$artname},2);
		($sec,$min,$hour,$day,$mon,$year)=localtime($time);
		$artdate=sprintf("%4d%02d%02d%02d%02d%02d",$year+1900,$mon+1,$day,$hour,$min,$sec);
		$new="";
		$new="<img src=/$HTMLPATH/pictures/new.gif alt=\"New! \">" if $artdate>$checknew;
		$new="<img src=/$HTMLPATH/pictures/hot.gif alt=\"Hot! \">" if $artdate>$checkhot;
		($time,$header)=split(/:/,$INDEX{$artname},2);
		($sec,$min,$hour,$day,$mon,$year)=localtime($time);
		$lm=sprintf("%s %d, %s",(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon],$day,$year+1900);
		$URL="$PAGEURL$ENV{PATH_INFO}";
		$URL.="#$artname";
		print "<li>$new<a href=#$artname>$header</a>  ($lm)\n";
		push(@articles,$artname);
	}
	print "</ul></td></tr></table>\n";
	foreach $article(@articles){
		&getart4("$artpath/$article","index");
	}
	&makelinks;
	print "<hr><center>Quick access to other sections</center><br>\n";
	print "<center><a href=/$CGIPATH/trindex>Master Index</a><br>$titlelist</center>";
}
elsif(length($ENV{PATH_INFO})>1){
	print <<EOF;
<font size=+2><center>
Please visit the Todd Rundgren Connection at <a href=$PAGEURL>$PAGEURL</a>
</center></font>
EOF
}
else{
	$totalvisitors=commify($totalvisitors);
	$totalaccess=commify($totalaccess);
	$accessnum=commify($accessnum);
	print $localinc;
#<hr>
#Other sites gracious enough to point to this page may be found
#<a href=/$CGIPATH/known>here</a>.
#EOF
#So far, $totalvisitors different visitors from a total of $totalaccess visits
#have been here.  This is visit #$accessnum from your host.
	#$needvote="";
	#foreach $vote (sort keys %VOTE){
		#($votename,$votetitle)=split(/:/,$VOTE{$vote},2);
		#if($v{$vote} != 1){
			#$needvote.="<li><A href=$PAGEURL2/articles=$votename.art>$votetitle</a>\n";
		#}
	#}
# print <<EOF if $needvote;
# <p>
# You haven't voted yet in these ongoing polls:
# <ul>
# $needvote
# </ul>
# Don't forget!
# EOF
#
	&setfeatures;
#	unless($VIEWER_FEATURE{TABLES}||$VIEWER_FEATURE{NOTABLES}){
#		print <<EOF;
#The following list of album titles is also formatted for viewers that support
#HTML3-compliant tables.
#If your viewer supports Tables, <a href=/$CGIPATH/comment2admin.pl>please let me know</a>
#and I'll fix it so you can get the table presentation.
#EOF
#	}
	$now=time;
print <<EOF;
<hr>
<b>
<center>A <a href=/$CGIPATH/trindex>Master Index</a> is available</center>
</b><br>
<img src=/$HTMLPATH/pictures/new.gif alt="New! "> indicates an article that has been updated in the last two weeks.<br>
<img src=/$HTMLPATH/pictures/hot.gif alt="Hot! "> indicates an article that has been updated in the last 24 hours.<hr>
EOF
	@colors=(purple,green,blue,teal,yellow,red,orange,magenta,pink,seagreen,
	tomato,silver,lawngreen,khaki,ivory,lightsalmon,turquoise,royalblue);
	$bordercolor=$colors[int(rand(@colors))];
	print "<table bgcolor=\"$bordercolor\"><tr><td>\n";
	#print &htmltable("Take a tour through Todd's albums to explore".
		#" The Todd Rundgren Connection",
		#"border=5 bordercolor=\"$bordercolor\" cellspacing=5 bgcolor=white");
	$hello= <<EOF;
<table border=5 bordercolor="$bordercolor" cellspacing=5 bgcolor=white>
<caption><center><h2>Take a tour through Todd's albums to explore
The Todd Rundgren Connection</h2></center></caption>
EOF
	if($april1){
		$hello=~s/Todd/Ted/g;
		$hello=~s/Rundgren/Runger/g;
	}
	print $hello;
	foreach $tag (@tags){
		($title,$text)=split(/#/,$title{$tag});
		$title="<a href=$PAGEURL/section=$tag>$title</a> ";
		$text.="<hr>Includes:<br>";
		foreach $article(split(/,/,$articles{$tag})){
			($time,$header)=split(/:/,$INDEX{$article},2);
			($sec,$min,$hour,$day,$mon,$year)=localtime($time);
			$new="";
			$new="<img src=/$HTMLPATH/pictures/new.gif alt=\"New! \">" if $time>$now-14*86400;
			$new="<img src=/$HTMLPATH/pictures/hot.gif alt=\"Hot! \">" if $time>$now-86400;
			$text.=$new;
			$text.="$header<br>";
		}
		#print &htmlrow($title,$text);
		print "<tr><th>$title</th><td>$text</td></tr>\n";
	}
	print "</table>\n";
	#print &htmltableend;
	print "</td></tr></table>\n";
	print $PAGETAILMAIN;
}
#print "<hr>Size control: ";
#print "<a href=/todd/trsize/smaller>Smallest Font</a>\n";
#print "<a href=/todd/trsize/small>Smaller Font</a>\n";
#print "<a href=/todd/trsize/normal>Normal Font</a>\n";
#print "<a href=/todd/trsize/big>Larger Font</a>\n";
#print "<a href=/todd/trsize/bigger>Largest Font</a>\n";
print $PAGETAIL;
&htmlclose;
sub makelinks{
	foreach $tag (@tags){
		($title,$text)=split(/#/,$title{$tag});
		$title=~s/ /&nbsp;/g;
		$title="<a href=$PAGEURL/section=$tag>$title</a> ";
		$titlelist="$titlelist$title | ";
	}
	chop($titlelist);
	chop($titlelist);
}
sub trpagestats{
	local($dbfile)=@_;
	local(@time,$today);
	#dbmopen(%OLDPAGESTATS,"$PAGEHOMEDIR/db/trdata.db",0644);
	dbmopen(%OLDPAGESTATS,"$PAGEHOMEDIR/db/trdata",0644);
	#dbmopen(%PAGESTATS,$dbfile.".db",0644);
	dbmopen(%PAGESTATS,$dbfile,0644);
	$key="$ENV{REMOTE_ADDR}:$ENV{REMOTE_HOST}";
	$access=$PAGESTATS{$key};
	if($access){
		@accessnum=split(/:/,$access);
	}
	else{
		$access=$OLDPAGESTATS{$ENV{REMOTE_ADDR}};
		@accessnum=split(/:/,$access);
		$accessnum[0]=0 unless $access;
		splice(@accessnum,1,0,0); # insert placeholder for time field
	}
	$accessnum[0]++ unless $ENV{PATH_INFO} eq "/nocount";
	$accessnum[1]=time;
	$accessnum=$accessnum[0];
	$access=$PAGESTATS{$key}=join(":",@accessnum);
	@time=localtime($accessnum[1]);
	$today=sprintf("DATECOUNT%%%%%4d%02d%2d",$time[5]+1900,$time[4]+1,$time[3]);
	$PAGESTATS{$today}++;
	$PAGESTATS{TOTALACCESS}+=1 unless $ENV{PATH_INFO} eq "/nocount";
	if($accessnum[0]==1){$PAGESTATS{TOTALVISITORS}+=1};
	$totalvisitors=$PAGESTATS{TOTALVISITORS};
	$totalaccess=$PAGESTATS{TOTALACCESS};
	dbmclose(%PAGESTATS);
	dbmclose(%OLDPAGESTATS);
	return($access,$accessnum,$totalvisitors,$totalaccess);
}
sub getart4{
	local($artname,$indexname)=@_;
	local(@art)=split(/\//,$artname);
	local($artdate,$new);
	$indexname=" href=\"#$indexname\"" if $indexname;
	open(ART,$_[0]);
	chop($_=<ART>);
	s/<\/?h3>//gi;
	print "<hr><table><tr><td valign=top width=100>";
	if($april1){
		s/Todd/Ted/g;
		s/Rundgren/Runger/g;
	}
	print "<h3><a$indexname name=\"$art[$#art]\">$_</a></h3>";
	print "</td>\n<td valign=top>";
	while(<ART>){
		$date="";
		if(/<new ([^>]+)>/){
			@new=split(/\s+/,$1);
			$artdate=shift @new;
			%new=map {split(/=/)} @new;
			if($expdate=$new{expires}){
				$expdate.="000000";
				$expdate=substr($expdate,0,14);
				next if $expired=($expdate < $checkexpire);
			}
			else{
				$expdate="";
			}
			$artdate.="000000";
			$artdate=substr($artdate,0,14);
			($year,$month,$day,$hour,$minute,$second)=unpack("a4a2a2a2a2a2",$artdate);
			$date=sprintf(qq{<div style="font: italic 10pt/12pt Times, Garamond, serif">Posted/Updated: %d/%d/%d %02d:%02d</div>},
				$month,$day,$year,$hour,$minute);
			$new="";
			$new="<img src=/$HTMLPATH/pictures/new.gif alt=\"New! \">"
				if $artdate>$checknew;
			$new="<img src=/$HTMLPATH/pictures/hot.gif alt=\"Hot! \">"
				if $artdate>$checkhot;
			s/<new [^>]+>/$new$date/;
		}
		if($expired){
			if(/<\/new>/){s/.*<\/new>//;$expired=0;}
			else{next;}
		}
		s/<\/new>//;
		s/\$WWWHOST/$WWWHOST/g;
		s/\$CGIPATH/$CGIPATH/g;
		s/\$HTMLPATH/$HTMLPATH/g;
		s/\$PAGEHOMEDIR/$PAGEHOMEDIR/g;
		if($april1){
			s/Todd/Ted/g;
			s/Rundgren/Runger/g;
		}
		print;
	}
	print "</td></tr>\n</table>\n";
	close(ART);
}
sub commify{
	my $text=reverse $_[0];
	$text=~s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
	return scalar reverse $text;
}
