#!/usr/bin/perl
require "/home/todd/toddweb.env";
require "weblib.lib";
&ReadParse;
$dir="/home/todd/www/reviews";
$ENV{PATH_INFO}=~s#/\.\.##;
$file=substr($ENV{PATH_INFO},1);
@file=split(m#/#,$file);
if($in{review}){
	pop(@file);
	push(@file,$in{review});
	$file=join("/",@file);
}
$style=<<EOF;
body	{font: 10pt verdana,sans-serif;}
.review	{font: 9pt verdana,sans-serif; width: 600px; margin-left: 3em; text-align: justify; clear: left;}
h1	{font: 12pt verdana, sans-serif;}
EOF
%type=("rev","Review","com","Commentary","set","Set List");
if(-f "$dir/$file"){
	@file=split(m#/#,$file);
	$file=pop(@file);
	$subdir=join('/',@file);
	open(FILE,"$dir/$subdir/$file");
	$file=~/^(.*)-[^\.]+\.(rev|com|set)$/;
	$root=$1;
	$type=$2;
	&getparam;
	$pos=tell(FILE);
	$title=$param{Title};
	$title=~s/<br>/ - /;
	$title=$type{$type} unless $title;
	$author="<i>$type{$type} by $param{Author}</i>" if $param{Author};
	$date="<i>$param{Date}</i><p>";
	&htmlopen("$title|","style=$style");
	print qq{<a href="http://TRConnection.com"><img src="http://trconnection.com/pictures/trcsm.gif" border="0" align="left"></a>\n};
	print "<h1>$title</h1>\n";
	opendir(DIR,"$dir/$subdir");
	@files=sort grep /^$root-.*\.$type/,readdir DIR;
	closedir(DIR);
	if($type ne "set"){
		$jump=qq{<form action="$ENV{SCRIPT_NAME}" method="POST" style="display: inline">\n};
		$jump.=qq{<select name="review" onChange="submit();return true;"><option value="">Select Next $type{$type}</option>\n};
		foreach $filename(sort @files){
			open(FILE,"$dir/$subdir/$filename");
			&getparam;
			$selected=$filename eq $file?" selected":"";
			$jump.=qq{<option value="$subdir/$filename"$selected>$param{Author}</option>\n};
		}
		$jump.="</select></form>\n";
	}
	open(FILE,"$dir/$subdir/$file");
	seek(FILE,$pos,0);
	print $author if $author;
	if($type ne "set"){print " (Switch to $jump)";}
	print "<br>" if $author & $date;
	print "<p>" if $author & !$date;
	print $date if $date;
	print qq{<div class="review">\n};
	while(<FILE>){
		if($april1){
			s/Todd/Ted/g;
			s/Rundgren/Runger/g;
			s/Utopia/Tedtopia/g;
		}
		print "<p>" if /^\s?\r?$/;
		print;
	}
	print "</div>\n";
	print "<hr>\n";
	if(-f "$dir/$subdir/title"){
		open(FILE,"$dir/$subdir/title");
		&getparam;
		close(FILE);
		$param{GroupTitle}=$param{Title};
	}
	if(-f "$dir/$subdir/$root.title"){
		open(FILE,"$dir/$subdir/$root.title");
		&getparam;
		close(FILE);
	}
	if($param{Article}){print "Other <a href=$weblib::URL/$subdir/$1>$param{Title}</a><br>\n";}
	else{print qq{Other reviews for <a href="$weblib::URL/$subdir/$1">$param{Title}</a><br>} if $1;}
	print "Other reviews for overall <a href=$param{URL}>$param{GroupTitle}</a>\n"if $param{URL};
}
else{
	@file=split(m#/#,$file);
	$root=pop(@file);
	$subdir=join('/',@file);
	if(-d "$dir/$subdir"){
		opendir(DIR,"$dir/$subdir");
		@files=sort grep /^$root-.*\.(rev|com)/,readdir DIR;
		closedir(DIR);
		if(@files){
			if(-f "$dir/$subdir/$root.title"){
				open(FILE,"$dir/$subdir/$root.title");
				&getparam;
				close(FILE);
			}
			elsif(-f "$dir/$subdir/title"){
				open(FILE,"$dir/$subdir/title");
				&getparam;
				close(FILE);
			}
			if($param{Title}){
				$title=$param{Title};
				$title=~s/<br>/ - /;
				if($param{Article}){&htmlopen($title,"$param{Title}","style=$style");}
				else{&htmlopen($title,"Fan reviews for $param{Title}","style=$style");}
				if(-f "$dir/$subdir/index.html"){
					open(FILE2,"$dir/$subdir/index.html");
					print <FILE2>;
					close(FILE2);
				}
				print "<ul>\n";
				$htmlopened=1;
			}
			foreach $f(@files){
				$f=~/\.(rev|com)$/;
				$type=$1;
				open(FILE,"$dir/$subdir/$f");
				&getparam;
				close(FILE);
				unless($htmlopened){
					$title=$param{Title};
					$title=~s/<br>/ - /;
					&htmlopen($title,"Fan reviews for $param{Title}","style=$style");
					if(-f "$dir/$subdir/index.html"){
						open(FILE2,"$dir/$subdir/index.html");
						print <FILE2>;
						close(FILE2);
					}
					print "<ul>\n";
					$htmlopened=1;
				}
				print "<li><a href=$weblib::URL/$subdir/$f>$param{Author}</a> ($type{$type})";
				print " ($param{Date})" if $param{Date};
				print "\n";
			}
			print "</ul>";
		}
		else{
			if(-f "$dir/$subdir/title"){$title=`cat "$dir/$subdir/title"`;}
			else{$title="Reviews";}
			&htmlopen("$title+","style=$style");
			if(-f "$dir/$subdir/index.html"){
				open(FILE2,"$dir/$subdir/index.html");
				print <FILE2>;
				close(FILE2);
			}
			&htmlclose;
		}
	}
	else{
		&htmlopen("Reviews+","style=$style");
		print "+++++";
		&htmlclose;
	}
}
print $PAGETAIL;
&htmlclose;
sub getparam{
	while(<FILE>){
		last if /^$/;
		chop;
		if($april1){
			s/Todd/Ted/g;
			s/Rundgren/Runger/g;
			s/Utopia/Tedtopia/g;
		}
		@param=split(/: /);
		$param{$param[0]}=$param[1];
	}
}
