#!/usr/local/bin/perl
require "/home/todd/toddweb.env";
require "weblib.lib";
require "$PAGEHOMEDIR/lyrics.lib";
use DB_File;
#require "DB_File.pm"; import DB_File;
dbmopen(%LYRICS,"$PAGEHOMEDIR/db/lyrics",0644);
#$BODYENHANCE=" background=/$HTMLPATH/pictures/note.gif bgcolor=#ffffff";
$style=<<EOF;
body {font: 8pt verdana,helvetica;}
EOF
if($ENV{PATH_INFO} eq "/full" or $ENV{PATH_INFO} eq "/fullest"){
	&loadtitles;
	&htmlopen("Lyrics by Album Title|","robots=none","style=$style");
	print qq{<dl>\n};
	foreach $artist(@artist){
		chop($artist{$artist});
	foreach $i(split(/:/,$artist{$artist})){
		next unless $LYRICS{"Song.$i.1"};
		&gettitleinfo($title[$i]);
		$title=$titleinfo[1];
		print "<dt>$title\n";
		$start=1;
		while(1){
			$key="Song.$i.$start";
			$start++;
			last unless $song=$LYRICS{$key};
			if($song=~s/^@//){$key="Song.$song";$song=$LYRICS{$key};}
			&getsonginfo($song);
			$song=$songinfo[1];
			$song.=" (Performed by $songinfo[8])" if $songinfo[8];
			$song.=" (As performed on $songinfo[9])" if $songinfo[9];
			print qq{<dd><a href="$weblib::URL/$key">$song</a>\n};
			if($ENV{PATH_INFO} eq "/fullest"){
				($head,$title,$song)=split(/\./,$key);
				&getsonginfo($LYRICS{$key});
				print qq{<h2><a href="#top" name="song$song">$songinfo[1]</a></h2>\n};
				print "<pre>\n";
				if($LYRICS{"Chunk.$title.$song.1"}=~/^@(\d+)\.(\d+)/){
					$title=$1;$song=$2;
				}
				$startchunk=0;
				while(1){
					$startchunk++;
					last unless $chunk=$LYRICS{"Chunk.$title.$song.$startchunk"};
					$chunk.="\n" unless substr($chunk,-1,1) eq "\n";
					print "$chunk\n";
				}
				print "</pre>\n";
			}
		}
	}
	}
	print "</dl>\n";
	$back=qq{<a href="$weblib::URL">Main Page</a>\n};
}
elsif($ENV{PATH_INFO} eq "/album"){
	&loadtitles;
	$lastartist="";
	$style.=<<EOF;
body {text-align: center;}
EOF
	&htmlopen("Lyrics by Album Title|","robots=none","style=$style");
	print qq{<div style="width: 660px; margin-left: auto; margin-right: auto;">};
	foreach $artist(@artist){
		chop($artist{$artist});
		$print="";
		foreach $i(split(/:/,$artist{$artist})){
			next unless $LYRICS{"Song.$i.1"};
			&gettitleinfo($title[$i]);
			$title=$titleinfo[1];
			($artist,$release)=split(/: /,$title,2);
			if($lastartist ne $artist){
					print qq{<h4 style="text-align: center;">$artist</h4>\n<div style="text-align: center;">};
					$lastartist=$artist;
			}
			($value)=split(/\n/,$titleinfo[$titleinfotype{CoverArt}]);
			($value)=split(/;/,$value);
			if($value){
				print qq{<a href="$weblib::URL/Title.$i"><img src="$value" style="width: 50px; height: 50px; padding: 2px; border: 0px;"></a>};
			}
			$print.= qq{<div style="padding-bottom: 0px; border-bottom: 0px solid #888;">};
			$print.= qq{$release };
			$print.= qq{(<a href="$weblib::URL/Title.$i">Song Titles Only</a>) };
			$print.= qq{(<a href="$weblib::URL/Title.$i/full">Full Lyrics</a>)<br></div>\n};
		}
		print qq{<br style="clear: left;"></div>\n};
		print $print;
	}
	print "</div>\n";
	$back=qq{<a href="$weblib::URL">Main Page</a>\n};
}
elsif($ENV{PATH_INFO} =~ m#^/Title\.(\d+)#){
	$i=$1;
	$BODYENHANCE=" background=/$HTMLPATH/pictures/$LYRICS{\"BGTitle.$i\"}"
		if $LYRICS{"BGTitle.$i"};
	$full=$ENV{PATH_INFO} =~ m#/full$#;
	&gettitleinfo($LYRICS{"Title.$i"});
	$title=$titleinfo[1];
	($artist,$release)=split(/: /,$title,2);
	&htmlopen("$title+","robots=none","style=$style");
	$value=$titleinfo[$titleinfotype{CoverArt}];
	foreach $value(split(/\n/,$value)){
		($value,$width,$height)=split(/;/,$value);
		$width=170 unless $width;
		$height=170 unless $height;
		if($pict){
			print qq{<img src="$value" width="$width" height="$height">\n};
		}
		else{
			print qq{<img src="$value" align="right" width="$width" height="$height">\n};
		}
		$pict++;
	}
	foreach $label(split(/;/,"Artist;Players;Producer")){
		next unless $value=$titleinfo[$titleinfotype{$label}];
		$value=~s/\n/<br>\n/g;
		print  "<h2>$label</h2>$value\n";
	}
	print qq{<p id="top">\n};
	foreach $label(split(/;/,"Release Date;Duration;Label;Catalog Number")){
		($labelkey=$label)=~s/ //g;
		$value=$titleinfo[$titleinfotype{$labelkey}];
		print "<b>$label</b>: $value<br>" if $value;
	}
	$comment=$titleinfo[$titleinfotype{"Comments"}];
	print "<p>$comment<p>\n" if $comment;
	print "<ul>\n";
	$start=1;
	@song=();
	while(1){
		$key="Song.$i.$start";
		last unless $song=$LYRICS{$key};
		if($song=~s/^@//){$key="Song.$song";$song=$LYRICS{$key};}
		&getsonginfo($song);
		$song=$songinfo[1];
		$song.=" (Performed by $songinfo[8])" if $songinfo[8];
		$song.=" (As performed on $songinfo[9])" if $songinfo[9];
		if($full){print qq{<li><a href="#song$start">$song</a>\n};push(@song,$key);}
		else{print qq{<li><a href="$weblib::URL/$key">$song</a>\n};}
		$start++;
	}
	print "</ul>\n";
	if($full){
		foreach $key(@song){
			($head,$title,$song)=split(/\./,$key);
			&getsonginfo($LYRICS{$key});
			print qq{<h2><a href="#top" name="song$song">$songinfo[1]</a></h2>\n};
			print "<pre>\n";
			if($LYRICS{"Chunk.$title.$song.1"}=~/^@(\d+)\.(\d+)/){
				$title=$1;$song=$2;
			}
			$start=0;
			while(1){
				$start++;
				last unless $chunk=$LYRICS{"Chunk.$title.$song.$start"};
				$chunk.="\n" unless substr($chunk,-1,1) eq "\n";
				print "$chunk\n";
			}
			print "</pre>\n";
		}
	}
	$reviews=$titleinfo[$titleinfotype{"OtherReviews"}];
	if($reviews){
		print "<h2>Reviews</h2>\n<ul>\n";
		@reviews=split(/\n/,$reviews);
		foreach $review(@reviews){
			($name,$url)=split(/:/,$review,2);
			print qq{<li><a href="$url">$name</a>\n};
		}
		print "</ul>\n";
	}
	$otherref=$titleinfo[$titleinfotype{"OtherReferences"}];
	$muscent=$titleinfo[$titleinfotype{"MusicCentral"}];
	$amg=$titleinfo[$titleinfotype{"AllMusicGuide"}];
	$amvg=$titleinfo[$titleinfotype{"AllMovieGuide"}];
	print "<h2>Other References</h2><ul>\n" if $otherref||$amg||$amvg;
	@otherref=split(/\n/,$otherref);
	foreach $otherref(@otherref){
		print qq{<li><a href="$otherref">$otherref</a>\n};
	}
	$vbar="|"x(9-length($amg));
	#print "<li><a href=http://musiccentral.msn.com/Album/Display/$muscent>Music Central information for $release</a>\n" if $muscent;
#http://www.allmusic.com/cg/x.dll?p=amg&sql=A17099
	print qq{<li><a href="http://www.allmusic.com/album/r$amg">All Music Guide entry for $release</a>\n} if $amg;
	$vbar="|"x(9-length($amvg));
	print qq{<li><a href="http://allmovie.com/cg/x.dll?p=avg&sql=AV$vbar$amvg">All Movie Guide entry for $release</a>\n} if $amvg;
	print "</ul>\n" if $otherref||$amg||$amvg;
	$back="";
	$back=qq{<a href="$weblib::URL$ENV{PATH_INFO}/full">Display Full Lyrics for $release</a><br>\n} if !$full;
	$back.=qq{<a href="$weblib::URL">Main Page</a>\n};
}
elsif($ENV{PATH_INFO} eq "/alphabetic"){
foreach $key(grep(/Song/,keys %LYRICS)){
	next if $LYRICS{$key}=~/^@/;
	($title)=($key=~/Song\.(\d+)\.\d+/);
	&getsonginfo($LYRICS{$key});
	if($song{$songinfo[1]}){
		($oldtitle)=($song{$songinfo[1]}=~/Song\.(\d+)\.\d+/);
		next if $oldtitle<$title;
	}
	$song{$songinfo[1]}=$key;
}
&htmlopen("TR/Utopia/Nazz Songs|","robots=none","style=$style");
print "<ol>\n";
foreach $song(sort keys %song){
	$i=0;
	$key=$song{$song};
	&getsonginfo($LYRICS{$key});
	$song=$songinfo[1];
	$song.=" (Performed by $songinfo[8])" if $songinfo[8];
	$song.=" (As performed on $songinfo[9])" if $songinfo[9];
	print qq{<li><a href="$weblib::URL/$key">$song</a>\n} if $song;
	$i++;
}
print "</ol>\n";
$back=qq{<a href="$weblib::URL">Main Page</a>\n};
}
elsif($ENV{PATH_INFO} =~ m#^/Song#){
$start=1;
($head,$title,$song)=split(/\./,$ENV{PATH_INFO});
&gettitleinfo($LYRICS{"Title.$title"});
&getsonginfo($LYRICS{"Song.$title.$song"});
&htmlopen($titleinfo[1],$songinfo[1],"robots=none","style=$style");
foreach $label("Composer","Duration"){
	if($infoprint=$songinfo[$songinfotype{$label}]){
		if($april1){
			$infoprint=~s/Todd/Ted/g;
			$infoprint=~s/Rundgren/Runger/g;
		}
		print "<b>$songinfotype[$songinfotype{$label}]</b>: $infoprint<br>\n";
	}
}
foreach $label("GuitarTab","MIDI"){
	if($infoprint=$songinfo[$songinfotype{$label}]){
		print qq{<b>$songinfotype[$songinfotype{$label}]</b>: <a href="$infoprint">$infoprint</a><br>\n};
	}
}
if($infoprint=$songinfo[$songinfotype{"YouTube"}]){
	print qq{<a href="https://www.youtube.com/watch?v=$infoprint">Video Link</a><br>\n};
	print <<EOF;
<iframe width="420" height="315" src="https://www.youtube.com/embed/$infoprint">
</iframe>
EOF
}
print "<pre>\n";
$titlesave=$title;$songsave=$song;
if($LYRICS{"Chunk.$title.$song.$start"}=~/^@(\d+)\.(\d+)/){
	$title=$1;$song=$2;
}
while(1){
last unless $chunk=$LYRICS{"Chunk.$title.$song.".$start++};
while($chunk=~/%[\w.]+:[^%]+%/){
	$before=$`;
	$after=$';
	($phrase=$&)=~/%([\w.]+):([^%]+)%/;
	$phraselink=$1;
	$phrase=$2;
	$chunk="$before$phrase$after";
}
$chunk.="\n" unless substr($chunk,-1,1) eq "\n";
print "$chunk\n";
}
print "</pre>\n";
$title=$titlesave;$song=$songsave;
$back="";
if($commentary=$songinfo[$songinfotype{"Commentary"}]){
while($commentary=~/%[\w.]+:[^%]+%/){
	$before=$`;
	$after=$';
	($phrase=$&)=~/%([\w.]+):([^%]+)%/;
	$phraselink=$1;
	$phrase=$2;
	$commentary="$before$phrase$after";
}
	$back.="<b>Commentary</b><br>$commentary<p>\n";
}
if($soundbyte=$songinfo[$songinfotype{"SoundByte"}]){
	$back.=qq{<a href="$soundbyte">A sample from the song</a><br>\n};
}
if($digsong=$songinfo[$songinfotype{"DigitizedSong"}]){
	$back.=qq{<a href="$digsong">The entire digitized song</a><br>\n};
}
$back.=qq{<a href="http://www.ascap.com/ace/title/detail.cgi?tcode=$songinfo[7]">ASCAP Entry for $songinfo[1]</a><br>\n} if $songinfo[7];
if($songinfo[6] =~ /\./){@si6=split(/\./,$songinfo[6]);$songinfo[6]=$si6[2];$songinfo[6]=~s/^0*//;}
$back.=qq{<a href="http://repertoire.bmi.com/title.asp?blnWriter=True&blnPublisher=True&blnArtist=True&keyid=$songinfo[6]&querytype=WorkID">BMI Entry for $songinfo[1]</a><br>\n} if $songinfo[6];
$nextsong=$song+1;
$prevsong=$song-1;
$break="";
if(defined($LYRICS{"Song.$title.$prevsong"})){
	$break="<br>\n";
	$back.=qq{<a href="$weblib::URL/Song.$title.$prevsong">&lt;&lt;&lt; Previous Song</a> };
}
if(defined($LYRICS{"Song.$title.$nextsong"})){
	$break="<br>\n";
	$back.=qq{ <a href="$weblib::URL/Song.$title.$nextsong">>>> Next Song</a>};
}
$back.=$break;
$back.=qq{<a href="$weblib::URL/Title.$title">$titleinfo[1]</a><br>\n};
$back.=qq{<a href="$weblib::URL">Main Page</a>\n};
}
elsif($ENV{PATH_INFO} =~ m#^/search#){
	&htmlopen("Lyrics Search|","robots=none","style=$style");
	print <<EOF;
<form action=$weblib::URL/search method=POST>
Word or Phrase<br>
<input name=Search size=60><br>
<input type=checkbox name=Substring>Allow Substrings<br>
<input type=submit>
</form>
EOF
	&ReadParse;
	if($in{Search}){
		foreach $key(grep(/^Chunk/,keys %LYRICS)){
			$search=$in{Search};
			$search="\\b$search\\b" unless $in{Substring};
			next unless $LYRICS{$key} =~ /$search/i;
			($label,$title,$song,$chunk)=split(/\./,$key);
			$SONG{$title,$song}=$SONG{$title,$song}?"$SONG{$title,$song};$chunk":$chunk;
		}
		foreach $key(keys %SONG){
			($title,$song)=split(/$;/,$key);
			@chunks=sort bynum split(/;/,$SONG{$key});
			&getsonginfo($LYRICS{"Song.$title.$song"});
			print qq{<h3><a href="$weblib::URL/Song.$title.$song">$songinfo[1]</a></h3>\n};
			foreach $chunk(@chunks){
				$key="Chunk.$title.$song.$chunk";
				($chunktext=$LYRICS{$key})=~s/\b$in{Search}/<b>$&<\/b>/gi;
				$chunktext.="\n" unless substr($chunktext,-1,1) eq "\n";
				print "<pre>$chunktext</pre>\n";
			}
		}
	}
	$back=qq{<a href="$weblib::URL">Main Page</a>\n};
}
else{
	if($ENV{PATH_INFO} =~ s"^/edit""){
		if($ENV{PATH_INFO} eq ""){
			&htmlopen("Title Search|","robots=none","style=$style");
			print "<form action=$weblib::URL/edit method=POST>\n";
			print "<input name=Search size=60><input type=submit></form>\n";
			&ReadParse;
			if($in{Search}){
				foreach $title(grep(/Title/,keys %LYRICS)){
					&gettitleinfo($LYRICS{$title});
					push(@titles,$title) if $titleinfo[1] =~ /$in{Search}/i;
				}
				foreach $song(grep(/Song/,keys %LYRICS)){
					&getsonginfo($LYRICS{$song});
					push(@songs,$song) if $songinfo[1] =~ /$in{Search}/i;
				}
				if(@titles){
					print "<h2>Titles</h2><ul>\n";
					foreach $title(@titles){
						&gettitleinfo($LYRICS{$title});
						print qq{<li><a href="$weblib::URL/edit/$title">$titleinfo[1]</a>\n};
					}
					print "</ul>\n";
				}
				if(@songs){
					print "<h2>Songs</h2><ul>\n";
					foreach $song(@songs){
						&getsonginfo($LYRICS{$song});
						print qq{<li><a href="$weblib::URL/edit/$song">$songinfo[1]</a>\n};
					}
					print "</ul>\n";
				}
			}
		}
		elsif($ENV{PATH_INFO} =~ m#/Title#){
			$start=1;
			($head,$title)=split(/\./,$ENV{PATH_INFO});
			&ReadParse;
			&gettitleinfo($LYRICS{"Title.$title"});
			foreach $key(keys %in){
				$titleinfo[$titleinfotype{$key}]=$in{$key};
			}
			$LYRICS{"Title.$title"}=&maketitleinfo;
			delete $LYRICS{"BGTitle.$title"},$in{BG}="" if $in{BG} eq "DELETE";
			$LYRICS{"BGTitle.$title"}=$in{BG} if $in{BG};
			&htmlopen("$titleinfo[1]+","robots=none","style=$style");
			&gettitleinfo($LYRICS{"Title.$title"});
			print "<form action=$weblib::URL/edit$ENV{PATH_INFO} method=POST>\n";
			print "<input type=submit value=\"Submit Corrections\"><br>\n";
			print &htmltable;
			for($titleinfo=1;$titleinfo<=$#titleinfotype;$titleinfo++){
				$s=$titleinfo[$titleinfo];
				next unless $t=$titleinfotype[$titleinfo];
				($t2=$t)=~s/ //g;
				($cols,$rows)=split(/,/,$titledatatype[$titleinfo]);
				if($rows){
					print &htmlrow($t,"<textarea name=\"$t2\" rows=5 cols=60 wrap=soft>$s</textarea>");
				}
				else{
					print &htmlrow($t,"<input name=\"$t2\" size=60 value=\"$s\">");
				}
			}
			$BG=$LYRICS{"BGTitle.$title"};
			print &htmlrow("Background Art","<input name=BG size=60 value=\"$BG\">");
			print &htmltableend;
			print "</form>\n";
			print "<ul>\n";
			while(1){
				$key="Song.$title.$start";
				last unless $song=$LYRICS{$key};
				&getsonginfo($song);
				print qq{<li><a href="$weblib::URL/edit/$key">$songinfo[1]</a>\n};
				$start++;
			}
			print "</ul>\n";
			$back=qq{<a href="$weblib::URL$ENV{PATH_INFO}">Back to Title</a><br>\n};
		}
		elsif($ENV{PATH_INFO} =~ m#/Song#){
			$start=1;
			($head,$title,$song)=split(/\./,$ENV{PATH_INFO});
			&ReadParse;
			&getsonginfo($LYRICS{"Song.$title.$song"});
			&gettitleinfo($LYRICS{"Title.$title"});
			foreach $key(keys %in){
				next unless $songinfotype{$key};
				$songinfo[$songinfotype{$key}]=$in{$key};
			}
			$LYRICS{"Song.$title.$song"}=&makesonginfo;
			if(!$LYRICS{"Chunk.$title.$song.1"}){
				$LYRICS{"Chunk.$title.$song.1"}="No lyrics";
			}
			&htmlopen("$titleinfo[1]|","robots=none","style=$style");
			&getsonginfo($LYRICS{"Song.$title.$song"});
			$next=$ENV{PATH_INFO};
			$next=~s#^/##;
			print "<form action=$weblib::URL/edit/$next method=POST>\n";
			print "<input type=submit value=\"Submit Corrections\">\n";
			print "<br>\n";
			print &htmltable;
			for($songinfo=1;$songinfo<=$#songinfotype;$songinfo++){
				$s=$songinfo[$songinfo];
				next unless $t=$songinfotype[$songinfo];
				($t2=$t)=~s/ //g;
				($cols,$rows)=split(/,/,$songdatatype[$songinfo]);
				if($rows){
					print &htmlrow($t,"<textarea wrap=soft name=\"$t2\" rows=5 cols=60>$s</textarea>");
				}
				else{
					print &htmlrow($t,"<input name=\"$t2\" size=60 value=\"$s\">");
				}
			}
			print &htmltableend;
			while(1){
				$key="Chunk.$title.$song.$start";
				last unless $chunk=$LYRICS{$key};
				$chunk=$LYRICS{$key}=$in{$key} if $in{$key};
				$chunk=~s/^  //;
				$chunk=~s/\n  /\n/g;
				print "<textarea wrap=soft name=\"$key\" cols=70 rows=10>$chunk</textarea><br>\n";
				$start++;
			}
			print "<input type=submit value=\"Submit Corrections\">\n";
			print "<br>\n";
			print "</form>\n";
			$back=qq{<a href="$weblib::URL$ENV{PATH_INFO}">Back to Song</a><br>\n};
			$nextsong=$song+1;
			$prevsong=$song-1;
			$break="";
			if(defined($LYRICS{"Song.$title.$prevsong"})){
				$break="<br>\n";
				$back.=qq{<a href="$weblib::URL/edit/Song.$title.$prevsong">&lt;&lt;&lt; Previous Song</a> };
			}
			if(defined($LYRICS{"Song.$title.$nextsong"})){
				$break="<br>\n";
				$back.=qq{ <a href="$weblib::URL/edit/Song.$title.$nextsong">>>> Next Song</a>};
			}
			$back.=$break;
		}
	}
	else{
		local($title)="Todd Rundgren Lyrics Archive+";
		$title="Ted Runger Lyrics Archive+" if $april1;
		&htmlopen($title,"robots=none","style=$style");
		print <<EOF;
Select your option for lyrics display from the list below:
<img src="/trc/pictures/note.gif" align="left">
<dl>
<dt><a href="$weblib::URL/album">Short Album list</a>
<dd>List of album titles only (also see below).
Select an album title to get list of songs on that album.
<dt><a href="$weblib::URL/full">Long Album List</a>
<dd>All album titles with songs listed in order.
<dt><a href="$weblib::URL/alphabetic">Alphabetical Song List</a>
<dd>All songs listed alphabetically.
<dt>Search for a specific word or phrase
<form action="$weblib::URL/search" method="POST">
<input name="Search" size="60"><br>
<input type="submit">
</form>
</dl>
EOF
		&loadtitles;
		print "<center>\n";
		foreach $artist(@artist){
			$artistprint=$artist;
			print "<b>$artistprint</b><br>\n";
			@titles=split(/:/,$artist{$artist});
			$title=shift(@titles);
			&gettitleinfo($LYRICS{"Title.$title"});
			($a,$t)=split(/:/,$titleinfo[1],2);
			$tprint=$t;
			print qq{<a href="lyrics/Title.$title">$tprint</a>\n};
			foreach $title(@titles){
				&gettitleinfo($LYRICS{"Title.$title"});
				($a,$t)=split(/:/,$titleinfo[1],2);
				$tprint=$t;
				print qq{ | <a href="lyrics/Title.$title">$tprint</a>\n};
			}
			print "<br>\n";
		}
		print "</center>\n";
	}
}
$back="<hr>$back" if $back;
print <<EOF;
$back
<br>
$PAGETAIL
EOF
&htmlclose;
sub bynum { $a <=> $b;}
sub loadtitles{
	$start=1;
	while(1){
		last unless $Title=$LYRICS{"Title.$start"};
		if($april1){
			$Title=~s/Todd/Ted/g;
			$Title=~s/Rundgren/Runger/g;
		}
		$title[$start]=$Title;
		&gettitleinfo($Title);
		$artist=$titleinfo[2];
		if($artist eq ""){($artist)=($titleinfo[1] =~ /^([^:]+)/);}
		push(@artist,$artist) unless $artist{$artist};
		$artist{$artist}.="$start:";
		$start++;
	}
}
