#!/usr/bin/perl

use MP3::Info; 
use File::Find;

print <<ENDPREFIX;
<html>
<head>
<title>Catalogue</title>
</head>
<body>
<table width=100% border=1>
<tr>
  <th>Artist/Collection</th>
  <th>Album/Folder</th>
  <th>Track Name</th>
  <th>Duration</th>
</tr>
ENDPREFIX

find(\&record, "./");

print "</table>\n</body>\n</html>\n";

sub record()
{
    $name = $_;
    $dir = $File::Find::dir;
    @segs = split("/", $dir);
    $artist = $segs[1];
    shift(@segs);
    shift(@segs);
    $album = join("/", @segs);
    if (-d $name
     || $name =~ /jpg|png|gif|bmp|doc|rtf|txt/i
     || $name =~ /^[.]/
     || $dir =~ /Recycled/i) {
        return;
    }

    if (! $album) {
        $album = "&nbsp;";  # for generating a table cell in HTML
    }

    $info = get_mp3info($name);
    print "<tr>";
    if ($name =~ /[.]mp3$/ && ! $@ && $info->{SECS} > 0) {
        $s = $info->{SS};
        $m = $info->{MM} % 60;
        $h = int($info->{MM} / 60);
        if ($h > 0) {
            printf("<td>%s</td><td>%s</td><td>%s</td><td>%d:%02d:%02d</td>", $artist, $album, $name, $h, $m, $s);
        }
        else {
            printf("<td>%s</td><td>%s</td><td>%s</td><td>%02d:%02d</td>", $artist, $album, $name, $m, $s);
        }
    }
    else {
        printf("<td>%s</td><td>%s</td><td>%s</td><td>&nbsp;</td>", $artist, $album, $name);
        $@ = "";  # reset error message
    }
    print "</tr>\n";
}
