#!/usr/local/bin/perl
require "weblib.lib";
require "/usr/local/config/$ENV{SERVER_NAME}/config.txt";
require "$LYRICSLIB";
use LWP::Simple;
use DB_File;
$VERSION="2.4.2";
$TESTDB="/home/rdlinder/private/DB/lyricscopy";
dbmopen(%LYRICS,$LYRICSDB,0644);
$MAINTITLE=$LYRICS{MAINTITLE};
$SHORTTITLE=$LYRICS{SHORTTITLE};
$INTRO=$LYRICS{INTRO};
$PAGETAIL=$LYRICS{PAGETAIL};
$viewport="viewport=width=device-width, initial-scale=1.0";
$style=<<EOF;
body {font: 10pt verdana,helvetica; background-color: whitesmoke;}
.picture {background-color: white; width: auto; padding: 5px; padding-right: 7px; border: 1px solid black; margin: 0px; margin-left: 5px; text-align: center;}
.right {float: right;}
EOF
$administrator=getcookie("LyricsAdmin");
if(($full=$ENV{PATH_INFO} eq "/full") or ($fullest=$ENV{PATH_INFO} eq "/fullest")){
	&loadtitles;
	&htmlopen("Lyrics by Album Title|","robots=none","style=$style",$viewport);
	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[$titleinfotype{Title}];
			if($full){print "<dt>$title\n";}
			elsif($fullest){print "<dt><h2>$title</h2>\n";}
			$start=1;
			while(1){
				$key="Song.$i.$start";
				$start++;
				last unless $song=$LYRICS{$key};
				if($song=~/^(1:)?@(\d+\.\d+)/){$keyr="Song.$2";$song=$LYRICS{$keyr};}
				&getsonginfo($song);
				$song=$songinfo[$songinfotype{SongTitle}];
				$song.=" (Performed by $songinfo[$songinfotype{PerformedBy}])" if $songinfo[$songinfotype{PerformedBy}];
				$song.=" (As performed on $songinfo[$songinfotype{PerformedOn}])" if $songinfo[$songinfotype{PerformedOn}];
				if($ENV{PATH_INFO} eq "/full"){print qq{<dd><a href="$weblib::URL/$key">$song</a>\n};}
				elsif($ENV{PATH_INFO} eq "/fullest"){
					$songsave=$song;
					($head,$title,$song)=split(/\./,$key);
					&getsonginfo($LYRICS{$key});
					print qq{<h3><a href="$weblib::URL/$key">$songsave</a></h3>\n};
					print qq{<a href="#top">(Back to the Top)</a>\n};
					if($LYRICS{"Chunk.$title.$song.1"}=~/^@(\d+)\.(\d+)/){
						$title=$1;$song=$2;
					}
					$startchunk=0;
					$lyrics="";
					while(1){
						$startchunk++;
						last unless $chunk=$LYRICS{"Chunk.$title.$song.$startchunk"};
						$chunk.="\n" unless substr($chunk,-1,1) eq "\n";
						$lyrics.=metadata($chunk)."\n";
					}
					print "<pre>\n";
					print $lyrics;
					print "</pre>\n";
				}
			}
		}
	}
	print "</dl>\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",$viewport);
	print qq{<div style="max-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[$titleinfotype{Title}];
			($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; padding: 2px; border: 1px solid black;"></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";
}
elsif($ENV{PATH_INFO} =~ m#^/Title\.(\d+)#){
	$i=$1;
	$BODYENHANCE=" background=/Media/Image/$LYRICS{\"BGTitle.$i\"}"
		if $LYRICS{"BGTitle.$i"};
	$full=$ENV{PATH_INFO} =~ m#/full$#;
	&gettitleinfo($LYRICS{"Title.$i"});
	$title=$titleinfo[$titleinfotype{Title}];
	($artist,$release)=split(/: /,$title,2);
	&htmlopen("$title+","robots=none","style=$style",$viewport);
	if($administrator){print <<EOF;
<form action="$weblib::URL/edit$ENV{PATH_INFO}" method="POST">
<input type="submit" value="Edit">
</form>
EOF
	}
	$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" style="width: ${width}px; height: ${height}px; border: 1px solid black;">\n};
		}
		else{
			print qq{<img src="$value" style="width: ${width}px; height: ${height}px; border: 1px solid black; float: right;">\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 "<ol>\n";
	$start=1;
	@song=();
	while(1){
		$key="Song.$i.$start";
		last unless $song=$LYRICS{$key};
		if($song=~/^(1:)?@(\d+\.\d+)/){$keyr="Song.$2";$song=$LYRICS{$keyr};}
		&getsonginfo($song);
		$song=$songinfo[$songinfotype{SongTitle}];
		$song.=" (Performed by $songinfo[$songinfotype{PerformedBy}])" if $songinfo[$songinfotype{PerformedBy}];
		$song.=" (As performed on $songinfo[$songinfotype{PerformedOn}])" if $songinfo[$songinfotype{PerformedOn}];
		if($full){
			print qq{<li><a href="#song$start">$song</a>\n};
			if($keyr){push(@song,"$keyr.$start");$keyr="";}
			else{push(@song,"$key.$start");}
		}
		else{
			if($songinfo[$songinfotype{DigitizedSong}]){$music=" <img src=\"http://rocemabra.com/Media/Icons/audio.png\" style=\"height: 16px;\">";}
			else{$music="";}
			if($songinfo[$songinfotype{SheetMusic}]){$sheet=" <img src=\"http://rocemabra.com/Media/Icons/sheetmusic.png\" style=\"height: 16px;\">";}
			else{$sheet="";}
			print qq{<li><a href="$weblib::URL/$key">$song</a>$music$sheet\n};
		}
		
		$start++;
	}
	print "</ol>\n";
	if($full){
		foreach $key(@song){
			($head,$title,$song,$start)=split(/\./,$key);
			$key="$head.$title.$song";
			&getsonginfo($LYRICS{$key});
			print qq{<h2><a href="#top" name="song$start">$songinfo[$songinfotype{SongTitle}]</a></h2>\n};
			if($LYRICS{"Chunk.$title.$song.1"}=~/^@(\d+)\.(\d+)/){
				$title=$1;$song=$2;
			}
			$start=0;
			$lyrics="";
			while(1){
				$start++;
				last unless $chunk=$LYRICS{"Chunk.$title.$song.$start"};
				$chunk.="\n" unless substr($chunk,-1,1) eq "\n";
				$lyrics.=metadata($chunk)."\n";
			}
			print "<pre>\n";
			print $lyrics;
			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 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;
}
elsif($ENV{PATH_INFO} eq "/alphabetic"){
	foreach $key(grep(/Song/,keys %LYRICS)){
		next if $LYRICS{$key}=~/^(1:)?@/;
		($title)=($key=~/Song\.(\d+)\.\d+/);
		&getsonginfo($LYRICS{$key});
		if($song{$songinfo[$songinfotype{SongTitle}]}){
			($oldtitle)=($song{$songinfo[$songinfotype{SongTitle}]}=~/Song\.(\d+)\.\d+/);
			next if $oldtitle<$title;
		}
		$song{$songinfo[$songinfotype{SongTitle}]}=$key;
	}
	&htmlopen("$SHORTTITLE+","robots=none","style=$style",$viewport);
	print "<ol>\n";
	foreach $song(sort keys %song){
		$i=0;
		$key=$song{$song};
		&getsonginfo($LYRICS{$key});
		$song=$songinfo[$songinfotype{SongTitle}];
		$song.=" (Performed by $songinfo[$songinfotype{PerformedBy}])" if $songinfo[$songinfotype{PerformedBy}];
		$song.=" (As performed on $songinfo[$songinfotype{PerformedOn}])" if $songinfo[$songinfotype{PerformedOn}];
		if($songinfo[$songinfotype{DigitizedSong}]){$music=" <img src=\"http://rocemabra.com/Media/Icons/audio.png\" style=\"height: 16px;\">";}
		else{$music="";}
		if($songinfo[$songinfotype{SheetMusic}]){$sheet=" <img src=\"http://rocemabra.com/Media/Icons/sheetmusic.png\" style=\"height: 16px;\">";}
		else{$sheet="";}
		print qq{<li><a href="$weblib::URL/$key">$song</a>$music$sheet\n} if $song;
		$i++;
	}
	print "</ol>\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"});
	if($songinfo[$songinfotype{SongTitle}]=~/^(1:)?@(\d+)\.(\d+)/){&getsonginfo($LYRICS{"Song.$2.$3"});}
	&ReadParse;
	&htmlopen($titleinfo[$titleinfotype{Title}],$songinfo[$songinfotype{SongTitle}],"robots=none","style=$style",$viewport);
	if($administrator){print <<EOF;
<form action="$weblib::URL/edit$ENV{PATH_INFO}" method="POST">
<input type="submit" value="Edit">
</form>
EOF
	}
	foreach $label("Composer","Duration"){
		if($infoprint=$songinfo[$songinfotype{$label}]){
			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
	}
	$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++};
		$lyrics.=metadata($chunk)."\n";
	}
	if($chords && !$in{nochords}){$nochords=qq{ name="nochords"};$out="out";}
	print <<EOF if $chords;
<form action="$weblib::URL$ENV{PATH_INFO}" method="POST">
<input type="submit"$nochords value="Display With$out Chords">
</form>
EOF
	$title=$titlesave;$song=$songsave;
	$backtotitle=qq{<a href="$weblib::URL/Title.$title">$titleinfo[$titleinfotype{Title}]</a><br>\n};
	$nextsong=$song+1;
	$prevsong=$song-1;
	($navprev,$break)=defined($LYRICS{"Song.$title.$prevsong"})?(qq{<a href="$weblib::URL/Song.$title.$prevsong">&lt;&lt;&lt; Previous Song</a> },"<br>\n"):("","");
	($navnext,$break)=defined($LYRICS{"Song.$title.$nextsong"})?(qq{<a href="$weblib::URL/Song.$title.$nextsong">&gt;&gt;&gt; Next Song</a> },"<br>\n"):("","");
	print "$backtotitle$navprev$navnext";
	print "<pre>\n$lyrics</pre>\n";
	$back="";
	if(defined($songinfotype{HiddenEnclosures})){
		metadata($songinfo[$songinfotype{HiddenEnclosures}]);
	}
	foreach $label("Commentary","Other Comments"){
		($tag=$label)=~s/\s+//g;
		if(defined($songinfotype{$tag}) && ($commentary=$songinfo[$songinfotype{$tag}])){
			$commentary=metadata($commentary);
			$back.="<b>$label</b><br>$commentary<p>\n" if $commentary;
		}
	}
	if($soundbyte=$songinfo[$songinfotype{"SoundByte"}]){
		$back.=qq{<a href="$soundbyte">A sample from the song</a><br>\n};
	}
	if($songinfotype{"SheetMusic"}){
		if($sheetmusic=$songinfo[$songinfotype{"SheetMusic"}]){
			$back.=qq{<a href="/Media/PDF/$sheetmusic" target="_blank">Sheet Music Download</a><br>};
		}
	}
	if($digsong=$songinfo[$songinfotype{"DigitizedSong"}]){
		@digsong=split(/;/,$digsong);
		foreach $digsong(@digsong){
			if($digsong !~ /^https?:/){$digsong="/Media/Audio/$digsong";}
			($digsong,$songlabel)=split(/%%/,$digsong);
			if($songlabel){$back.="<div>$songlabel</div>\n";}
			$back.=<<EOF;
<audio height="45" controls="controls" preload="auto">
<source src="$digsong" type="audio/mpeg" />
HTML5 Audio not available</audio><br>
EOF
		}
	}
	if($vidsong=$songinfo[$songinfotype{"LocalVideo"}]){
		@vidsong=split(/;/,$vidsong);
		$songlabel="";
		foreach $vidsong(@vidsong){
			if($vidsong !~ /^https?:/){$vidsong="/Media/Video/$vidsong";}
			($vidsong,$songlabel)=split(/%%/,$vidsong);
			if($songlabel){$back.="<div>$songlabel</div>\n";}
		$back.=<<EOF;
<video width="640" height="480" controls="controls" preload="auto">
<source src="$vidsong">
HTML5 Video not available</video><br>
EOF
		}
	}
	$back.=qq{<a href="http://www.ascap.com/ace/title/detail.cgi?tcode=$songinfo[$songinfotype{ASCAP}]">}.
		"ASCAP Entry for $songinfo[$songinfotype{SongTitle}]</a><br>\n" if $songinfo[$songinfotype{ASCAP}];
	if($songinfo[$songinfotype{BMI}] =~ /\./){
		@si6=split(/\./,$songinfo[$songinfotype{BMI}]);$songinfo[$songinfotype{BMI}]=$si6[2];$songinfo[$songinfotype{BMI}]=~s/^0*//;
	}
	$back.=qq{<a href="http://repertoire.bmi.com/title.asp?blnWriter=True&blnPublisher=True&blnArtist=True&keyid=$songinfo[$songinfotype{BMI}]&querytype=WorkID">}.
		"BMI Entry for $songinfo[$songinfotype{SongTitle}]</a><br>\n" if $songinfo[$songinfotype{BMI}];
	$back.="$backtotitle$navprev$navnext$break\n";
}
elsif($ENV{PATH_INFO} =~ m#^/search#){
	&htmlopen("Lyrics Search|","robots=none","style=$style",$viewport);
	print <<EOF;
<form action=$weblib::URL/search method=POST>
Word or Phrase<br>
<input name=Search size=40><br>
<input type=checkbox name=Substring>Allow Substrings<br>
<input type=submit value="Search">
</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[$songinfotype{SongTitle}]</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>",metadata($chunktext),"</pre>\n";
			}
		}
	}
}
else{
	if($ENV{PATH_INFO} =~ s"^/(edit|find)""){
		$option=$1;
		$option="find" unless $administrator;
		$action=$option eq "edit"?"/edit":"";
		if($ENV{PATH_INFO} eq ""){
			if($option eq "edit"){&IntroEditor;}
			else{
				&htmlopen("Title Search+","robots=none","style=$style",$viewport);
				print "<form action=$weblib::URL/$option method=POST>\n";
				print "<input name=Search size=40><input type=submit value=\"Search Titles\"></form>\n";
			}
			&ReadParse;
			if($in{Search}){
				foreach $title(grep(/Title/,keys %LYRICS)){
					&gettitleinfo($LYRICS{$title});
					push(@titles,$title) if $titleinfo[$titleinfotype{Title}] =~ /$in{Search}/i;
				}
				foreach $song(grep(/Song/,keys %LYRICS)){
					&getsonginfo($LYRICS{$song});
					push(@songs,$song) if $songinfo[$songinfotype{SongTitle}] =~ /$in{Search}/i;
				}
				if(@titles){
					print "<h2>Titles</h2><ul>\n";
					foreach $title(@titles){
						&gettitleinfo($LYRICS{$title});
						print qq{<li><a href="$weblib::URL$action/$title">$titleinfo[$titleinfotype{Title}]</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$action/$song">$songinfo[$songinfotype{SongTitle}]</a>\n};
					}
					print "</ul>\n";
				}
			}
		}
		elsif($ENV{PATH_INFO} =~ m#/Title#){
			if($option eq "edit"){
				$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[$titleinfotype{Title}]+","robots=none","style=$style",$viewport);
				&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[$songinfotype{SongTitle}]</a>\n};
					$start++;
				}
				print "</ul>\n";
			}
			else{
				&htmlopen("$weblib::URL$ENV{PATH_INFO}");
			}
			$back=qq{<a href="$weblib::URL$ENV{PATH_INFO}">Back to Title</a><br>\n};
		}
		elsif($ENV{PATH_INFO} =~ m#/Song#){
			if($option eq "edit"){
				$start=1;
				($head,$title,$song)=split(/\./,$ENV{PATH_INFO});
				&ReadParse;
				if($in{DeleteSong}){
					dechunk($title,$song);
					delete $LYRICS{"Chunk.$title.$song.1"};
					delete $LYRICS{"Song.$title.$song"};
					$song-- unless $song==1;
					%in=(); # Wipe out any updates
				}
				$titlepath="Title.$title";
				$songpath="Song.$title.$song";
				&getsonginfo($LYRICS{$songpath});
				&gettitleinfo($LYRICS{$titlepath});
				foreach $key(keys %in){
					next unless $songinfotype{$key};
					$songinfo[$songinfotype{$key}]=$in{$key};
				}
				$LYRICS{$songpath}=&makesonginfo;
				if(!$LYRICS{"Chunk.$title.$song.1"}){
					$LYRICS{"Chunk.$title.$song.1"}="No lyrics";
				}
				&htmlopen("$titleinfo[$titleinfotype{Title}]|","robots=none","style=$style",$viewport);
				print $deferredaction;
				if($in{DeChunkIt}){&dechunk($title,$song);}
				elsif($in{ChunkIt}){&chunk($title,$song);}
				&getsonginfo($LYRICS{$songpath});
				print "<form action=$weblib::URL/edit/$songpath method=POST>\n";
				print "<input type=submit value=\"Submit Corrections\">\n";
				print "<br>\n";
				$backtosong=qq{<a href="$weblib::URL/$songpath">Back to Song</a><br>};
				$backtotitle=qq{<a href="$weblib::URL/$titlepath">$titleinfo[$titleinfotype{Title}]</a><br>\n};
				$nextsong=$song+1;
				$prevsong=$song-1;
				($navprev,$break)=defined($LYRICS{"Song.$title.$prevsong"})?(qq{<a href="$weblib::URL/edit/Song.$title.$prevsong">&lt;&lt;&lt; Previous Song</a> },"<br>\n"):("","");
				($navnext,$break)=defined($LYRICS{"Song.$title.$nextsong"})?(qq{<a href="$weblib::URL/edit/Song.$title.$nextsong">&gt;&gt;&gt; Next Song</a> },"<br>\n"):("",$break);
				$navnext=qq{<a href="$weblib::URL/edit/Song.$title.$nextsong">&gt;&gt;&gt; Add Song</a> } unless $navnext;
				print "$backtosong$navprev$navnext$break$backtotitle";
				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 qq{<input type="checkbox" name="DeleteSong"> Delete Song<br>\n};
				print "<input type=submit value=\"Submit Corrections\">\n";
				print "<br>\n";
				print "</form>\n";
				print "<form action=$weblib::URL/edit/$songpath method=POST>\n";
				print qq{<input type="submit" value="Chunk It" name="ChunkIt">\n};
				print qq{<input type="submit" value="DeChunk It" name="DeChunkIt">\n};
				print "</form><br>";
				$back="$backtosong$navprev$navnext$break$backtotitle";
			}
			else{
				&htmlopen("$weblib::URL$ENV{PATH_INFO}");
			}
		}
	}
	else{
		if($administrator){
			$editform=<<EOF;
<form action="$weblib::URL/edit$ENV{PATH_INFO}" method="POST">
<input type="submit" value="Edit">
</form>
EOF
			&ReadParse;
			if($in{MAINTITLE}){
				$MAINTITLE=$LYRICS{MAINTITLE}=$in{MAINTITLE};
				$SHORTTITLE=$LYRICS{SHORTTITLE}=$in{SHORTTITLE};
				$INTRO=$LYRICS{INTRO}=$in{INTRO};
				$PAGETAIL=$LYRICS{PAGETAIL}=$in{PAGETAIL};
			}
		}
		else{$editform="";}
		&htmlopen("$MAINTITLE+","robots=none","style=$style",$viewport);
		print $editform,$INTRO;
		print <<EOF;
Select your option for lyrics display from the list below:
<img src="/Media/Image/musicnotes.png" style="max-width: 200px; padding: 10px; float: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><br clear="left">Search for a specific word or phrase
<form action="$weblib::URL/search" method="POST">
<input name="Search" size="40">
<input type="submit" value="Search Lyrics">
</form>
<form action="$weblib::URL/find" method="POST">
<input name="Search" size="40">
<input type="submit" value="Search Titles">
</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[$titleinfotype{Title}],2);
			$tprint=$t;
			print qq{<a href="$weblib::URL/Title.$title">$tprint</a>\n};
			foreach $title(@titles){
				&gettitleinfo($LYRICS{"Title.$title"});
				($a,$t)=split(/:/,$titleinfo[$titleinfotype{Title}],2);
				$tprint=$t;
				print qq{ | <a href="$weblib::URL/Title.$title">$tprint</a>\n};
			}
			print "<br>\n";
		}
		print "</center>\n";
	}
}
$back="<hr>$back" if $back;
print <<EOF;
$back
<br>
<i><a href="$weblib::URL">Lyrics v. $VERSION</a></i><br>
$PAGETAIL
EOF
&htmlclose;
sub bynum { $a <=> $b;}
sub loadtitles{
	local($start)=1;
	local($Title,$artist);
	while(1){
		last unless $Title=$LYRICS{"Title.$start"};
		$title[$start]=$Title;
		&gettitleinfo($Title);
		$artist=$titleinfo[$titleinfotype{Artist}];
		if($artist eq ""){($artist)=($titleinfo[$titleinfotype{Title}] =~ /^([^:]+)/);}
		push(@artist,$artist) unless $artist{$artist};
		$artist{$artist}.="$start:";
		$start++;
	}
}
sub metadata{
	local($chunk)=shift;
	local $before,$after,$phrase,$phraselink,@lines,$line,@enclosure,$enclosure,$display;
# A developing definition of SLML - Simple Lyric Markup Language
# %enclosure is a global hash that contains any enclosures from previous chunks
# Process "Chunk" for metadata
# Style 1 - Simple for Chord and label placement - {chord}lyrics{chord}lyrics
#	Example: {Dm}lyrics{C}more lyrics
#	Example for label: {Verse}lyrics; {Bridge}lyrics; {[boldlable]}
	if($chunk=~m#{.+?}#){
		@lines=split(/\r?\n/,$chunk);
		foreach $line(@lines){
			while($line=~m#{.+?}[^{]*#){
				$before=$`;
				$after=$';
				$&=~m#{(.+?)}([^{]*)#;
				$phraselink=$1;
				$phrase=$2;
				if($phraselink=~/^(Verse|Pre-Chorus|Chorus|Bridge|Intro|Outro|\[(.+)\])/){ # Label Only - text after phraselink is currently ignored
				$display=$2?$2:$1;
					$phrase=qq{<div style="display: block; font-weight: bold;">$display</div>$phrase};
				}
				elsif($phraselink){ # By default assume it's a chord name
					$chords=1;
					$phrase=qq{<div style="display: inline-block;">$phraselink<br>$phrase</div>} unless $in{nochords};
				}
				$line="$before$phrase$after";
			}
		}
		$chunk=join("\n",@lines);
	}
# Style 2 - %keyword(_value):normal lyric text%
# Keyword & value may be any combination of letters, numbers and periods
# Metadata elements currently defined are
# 	Verse/(Pre-)Chorus/Bridge/Intro/Outro enclosure definitions, allowing for Style 3 implementations
#		Example: %Verse:lyrics%; %Bridge_1:lyrics%
#	URL link
#		Example: %URL_FAWM:https://fawm.org%
#	Hidden enclosure
#		Example: %_abc:Text for enclosure%
	while($chunk=~/%[\w.]+:[^%]+%/){
		$before=$`;
		$after=$';
		$&=~/%([\w.]+):([^%]+)%/;
		$phraselink=$1;
		$phrase=$2;
		if($phraselink=~/^_(.+)/){
# Hidden Enclosure can be embedded in the same "chunk" for immediate use (must be defined before first usage)
# Does not otherwise display
			$enclosure{$1}=$phrase;
			$phrase="";
		}
		elsif($phraselink=~/^(Verse|Pre-Chorus|Chorus|Bridge|Intro|Outro)/){ # Enclosure
			$phrase=qq{<div style="display: inline-block;"><span style="font-weight: bold">$1</span><br>$phrase</div>};
			$enclosure{$phraselink}=$phrase;
		}
		elsif($phraselink =~ /^URL(_(.+))?$/){ # Any text for URL anchor must be defined first as a Hidden enclosure
			$text=$2?$enclosure{$2}:$phrase;
			$phrase=qq{<a href="$phrase" target="_blank">$text</a>};
		}
		elsif($phraselink =~ /^Content/){
			$phrase=get("http://rocemabra.com/content/$phrase");
		}
		elsif($phraselink =~ /^DisplayURL(_(.+))?$/){
			$phrase=get($phrase);
		}
		else{$enclosure{$1}=$phrase;}
		$chunk="$before$phrase$after";
	}
	$chunk.="\n" unless substr($chunk,-1,1) eq "\n";
# Style 3 - For substitution of previously defined Style 2 enclosure - must be only element in chunk
	if($chunk =~ /%(.+)%/){$chunk="$enclosure{$1}<br>";}
	return $chunk;
}
sub IntroEditor{
	&htmlopen("Intro Editor+","robots=none","style=$style",$viewport);
	print <<EOF;
<form action="$weblib::URL" method="POST">
<label>Main Title <input name="MAINTITLE" size="60" value="$MAINTITLE"></label><br>
<label>Short Title <input name="SHORTTITLE" size="60" value="$SHORTTITLE"></label><br>
<textarea name="INTRO" style="width: 100%; height: 600px" wrap="soft">$INTRO</textarea>
<label>Page Tail Text <textarea name="PAGETAIL" style="width: 100%;">$PAGETAIL</textarea></label><br>
<input type="submit" value="Update Titles & Intro">
</form>
EOF
}
sub dechunk{
	local($chunknext);
	local($chunknum)=1;
	local($title)=shift;
	local($song)=shift;
	local($chunk)=$LYRICS{"Chunk.$title.$song.$chunknum"};
	while(1){
		$chunknum++;
		last unless $chunknext=$LYRICS{"Chunk.$title.$song.$chunknum"};
		delete $LYRICS{"Chunk.$title.$song.$chunknum"};
		chomp($chunk);
		$chunk.="\n\n$chunknext";
	}
	$LYRICS{"Chunk.$title.$song.1"}=$chunk;
}
sub chunk{
	local(@chunks);
	local($chunknum)=1;
	local($title)=shift;
	local($song)=shift;
	if($LYRICS{"Chunk.$title.$song.2"}){
		print qq{<div style="font-size: 200%; font-weight: bold; border: 5px blue ridge; padding: 10px;">},
			qq{Lyrics are already chunked, must dechunk first!</div>\n};
	}
	local($chunk)=$LYRICS{"Chunk.$title.$song.1"};
	(@chunks)=split(/\r?\n\r?\n/s,$chunk);
	foreach $chunk(@chunks){
		$LYRICS{"Chunk.$title.$song.$chunknum"}=$chunk;
		$chunknum++;
	}
}
