#!/usr/bin/perl
# Philip Shuman philip at shuman dot org http://www.shuman.org
# cdList.pl v0.2.1

$address_file = "cd_list";
&writeHeader;
&readEnv;
&readLines; 
&writeInfo;
&writeFooter;

sub writeInfo {
   print "<p><table border=1 cellpadding=3>\n";
   if ($FORM{'sortby'} eq "album") {
      print " <tr>";
      print "<td></td>";
      print "<td bgcolor=DDDDDD><font face=verdana>";
      print "      Sort by: <a href=cdList.cgi?sortby=artist>Artist</a></td>";
      print "   <td><a href=cdList.cgi?sortby=album>Album</a></font></td>\n";
      print "   <td bgcolor=DDDDDD><a href=cdList.cgi?sortby=genre>CDDB Genre</a></td>\n";
      print "   <td bgcolor=DDDDDD><font face=verdana><a href=cdList.cgi?sortby=id>CDDB ID</a></font></td>\n";
      print " </tr>\n";
      %dbk = reverse %ALBUM_UNIQUE;

      my $i = 0;
      foreach $lkey (sort { lc($a) cmp lc($b) } keys %dbk) {
         print " <tr>\n";
         $i++;
         print "<td>$i</td>";
         print "    <td>$ARTIST{$dbk{$lkey}}</td>\n";
         print "    <td>$ALBUM{$dbk{$lkey}}</td>\n";
         print "    <td>$GENRE{$dbk{$lkey}}</td>\n";
         print "    <td><a href=http://www.freedb.org/freedb_search_fmt.php\?";
         print "cat=$GENRE{$dbk{$lkey}}&id=$ID{$dbk{$lkey}}>";
         print "$ID{$dbk{$lkey}}</a></td>\n";
         print " </tr>\n";
      }
   }
   elsif ($FORM{'sortby'} eq "id") {
      print " <tr>";
      print "<td></td>";
      print "<td bgcolor=DDDDDD><font face=verdana>";
      print "      Sort by: <a href=cdList.cgi?sortby=artist>Artist</a></td>";
      print "   <td><a href=cdList.cgi?sortby=album>Album</a></font></td>\n";
      print "   <td bgcolor=DDDDDD><a href=cdList.cgi?sortby=genre>CDDB Genre</a></td>\n";
      print "   <td bgcolor=DDDDDD><font face=verdana><a href=cdList.cgi?sortby=id>CDDB ID</a></font></td>\n";
      print " </tr>\n";
      %dbk = reverse %ID_UNIQUE;

      my $i = 0;
      foreach $lkey (sort { lc($a) cmp lc($b) } keys %dbk) {
         print " <tr>\n";
         $i++;
         print "<td>$i</td>";
         print " <td>$ARTIST{$dbk{$lkey}}</td>\n";
         print "   <td>$ALBUM{$dbk{$lkey}}</td>\n";
         print "    <td>$GENRE{$dbk{$lkey}}</a></td>\n";
         print "    <td><a href=http://www.freedb.org/freedb_search_fmt.php\?";
         print "cat=$GENRE{$dbk{$lkey}}&id=$ID{$dbk{$lkey}}>";
         print "$ID{$dbk{$lkey}}</a></td>\n";
         print " </tr>\n";
      }
   }
   elsif ($FORM{'sortby'} eq "genre") {
      print " <tr>";
      print "<td></td>";
      print "<td bgcolor=DDDDDD><font face=verdana>";
      print "      Sort by: <a href=cdList.cgi?sortby=artist>Artist</a></td>";
      print "   <td><a href=cdList.cgi?sortby=album>Album</a></font></td>\n";
      print "   <td bgcolor=DDDDDD><a href=cdList.cgi?sortby=genre>CDDB Genre</a></td>\n";
      print "   <td bgcolor=DDDDDD><font face=verdana><a href=cdList.cgi?sortby=id>CDDB ID</a></font></td>\n";
      print " </tr>\n";
      %dbk = reverse %GENRE_UNIQUE;

      my $i = 0;
      foreach $lkey (sort { lc($a) cmp lc($b) } keys %dbk) {
         print " <tr>\n";
         $i++;
         print "<td>$i</td>";
         print " <td>$ARTIST{$dbk{$lkey}}</td>\n";
         print "   <td>$ALBUM{$dbk{$lkey}}</td>\n";
         print "    <td>$GENRE{$dbk{$lkey}}</a></td>\n";
         print "    <td><a href=http://www.freedb.org/freedb_search_fmt.php\?";
         print "cat=$GENRE{$dbk{$lkey}}&id=$ID{$dbk{$lkey}}>";
         print "$ID{$dbk{$lkey}}</a></td>\n";
         print " </tr>\n";
      }
   }
   else {
      print " <tr><td></td><td bgcolor=DDDDDD><font face=verdana>";
      print "      Sort by: <a href=cdList.cgi?sortby=artist>Artist</a></td>";
      print "   <td><a href=cdList.cgi?sortby=album>Album</a></font></td>\n";
      print "   <td bgcolor=DDDDDD><a href=cdList.cgi?sortby=genre>CDDB Genre</a></td>\n";
      print "   <td bgcolor=DDDDDD><font face=verdana><a href=cdList.cgi?sortby=id>CDDB ID</a></font></td>\n";
      print " </tr>\n";

      %dbk = reverse %ARTIST_UNIQUE;

      my $i = 0;
      foreach $lkey (sort { lc($a) cmp lc($b) } keys %dbk) {
         print " <tr>\n";
         $i++;
         print "<td>$i</td>";
         print " <td>$ARTIST{$dbk{$lkey}}</td>\n";
         print "   <td>$ALBUM{$dbk{$lkey}}</td>\n";
         print "    <td>$GENRE{$dbk{$lkey}}</a></td>\n";
         print "    <td><a href=http://www.freedb.org/freedb_search_fmt.php\?";
         print "cat=$GENRE{$dbk{$lkey}}&id=$ID{$dbk{$lkey}}>";
         print "$ID{$dbk{$lkey}}</a></td>\n";
         print "</tr>\n";
      }
   }
   print "</table>";
}


sub writeHeader {
   print ("Content-type: text/html\n\n");
   print "<html><head><title>Our Compact Discs</title></head><body bgcolor=DDDDDD text=000000 vlink=336699 alink= 336699 link=336699> \n";
}

sub writeFooter {
   print "</body></html>\n";
}

sub readEnv {
   @pairs = split(/&/, $ENV{'QUERY_STRING'});
   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      $value =~ s/<!--(.|\n)*-->//g;
      $FORM{$name} = $value;
   }
}

sub readLines {
   open (FILE,"$address_file") || die "oops, can't open $id_file";
   $lineNum = 0; 
   while (<FILE>) {
      $lineNum++;
      ($id, $artistName, $albumName, $genre) = split(/\|/);
      chomp $albumName;
      chomp $genre;
      $ID{$lineNum} = $id;
      $ID_UNIQUE{$lineNum} = $id . $lineNum;
      $ARTIST{$lineNum} = $artistName;
      $ARTIST_UNIQUE{$lineNum} = $artistName . $lineNum;
      $ALBUM{$lineNum} = $albumName;
      $ALBUM_UNIQUE{$lineNum} = $albumName . $lineNum;
      $GENRE{$lineNum} = $genre;
      $GENRE_UNIQUE{$lineNum} = $genre . $lineNum;
   }
   close(FILE);
}
