#!/usr/local/bin/perl
require "/home/todd/toddweb.env";
require "$PAGEHOMEDIR/weblib.lib";
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
&htmlopen("$PAGENAME|","keywords=Todd Rundgren,Rundgren,Todd,TR-i",$description);
($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);
	}
	next if $expire<$checkexpire || !-f "$artpath/$artname";
	@stat=stat("$artpath/$artname");
	$INDEX{$artname}="$stat[9]:$header";
}
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};
}
while(<INDEX>){
	($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=+4><center>
Please visit the Todd Rundgren Connection at <a href=$PAGEURL>http://www.rocemabra.com$PAGEURL</a>
</center></font>
EOF
}
else{
	print <<EOF;
$localinc
<hr>
Other sites gracious enough to point to this page may be found
<a href=/$CGIPATH/known>here</a>. (Checked and pruned 2/10/2001)<br>
So far, $totalvisitors different visitors from a total of $totalaccess visits
have been here.  This is visit #$accessnum from your host.
EOF
	$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 <<EOF;
<div style="border: 5px red ridge">
<center><h2>Take a tour through Todd's albums to explore
The Todd Rundgren Connection</h2></center>
EOF
	foreach $tag (@tags){
		($title,$text)=split(/#/,$title{$tag});
		$title="<a href=$PAGEURL/$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 "<div style='margin-left: 0px; border-top: 2px red inset'>$title</div><div style='margin-left: 200px'>$text</div>\n";
	}
	print "</div>\n";
	print $PAGETAILMAIN;
}
print $PAGETAIL;
&htmlclose;
sub makelinks{
	foreach $tag (@tags){
		($title,$text)=split(/#/,$title{$tag});
		$title=~s/ /&nbsp;/g;
		$title="<a href=$PAGEURL/$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>";
	print "<h3><a$indexname name=\"$art[$#art]\">$_</a></h3>";
	print "</td>\n<td valign=top>";
	while(<ART>){
		if(/<new (\d+)([^>]*)>/){
			$artdate=$1;
			if($2 =~ /\s+expires=(\d+)$/){
				$expdate=$1;
				$expdate.="000000";
				$expdate=substr($expdate,0,14);
				next if $expired=($expdate < $checkexpire);
			}
			else{
				$expdate="";
			}
			$artdate.="000000";
			$artdate=substr($artdate,0,14);
			$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/;
		}
		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;
		print;
	}
	print "</td></tr>\n</table>\n";
	close(ART);
}
