#!/usr/bin/perl -w
use strict;

#warning: recursive ogginfo: 
#find mp3/ -name '*.ogg' |xargs ogginfo >ogginfo-index
#todo: get ordering from .m3u's.

my $dir = "/home/bfields/public_html/music";

my @albums = ();
my $genres = {};
my $artists = {};
my $tracks = {};

readalbums("$dir/getinfo2-output");

xref_tag($genres, "genre");
xref_tag($artists, "artist");
# doesn't work, it's a list: xref_tag($artists, "track");

output_albums();
output_album_index();
output_genre_indices();
output_genre_index();

sub escape_name
{
	my $name = shift();

	$name =~ s|/|_|g;
	return $name;
}

sub write_head
{
	my $file = shift();
	my $title = shift();

	print($file "<html><head><title>$title</title></head>\n");
	print($file "<body>\n");
}

sub write_short_album
{
	my $file = shift();
	my $album = shift();

	my $id = $album->{"id"};
	print($file "<li><a href=\"../albums/$id.html\">"
		. $album->{"title"} . "</a></li>\n");
}

sub output_genre_indices
{
	my $genre;

	foreach $genre (keys(%$genres)) {
		my $file = escape_name($genre);
		$file = "$dir/genres/$file.html";
		open(INDEX, ">$file") or die("failed to open $file");
		write_head(\*INDEX, "$genre index");
		print(INDEX "<ul>\n");
		my $album;
		foreach $album (@{$genres->{$genre}}) {
			write_short_album(\*INDEX, $album);
		}
		print(INDEX "</ul>\n</body>\n</html>\n");
		close(INDEX);
	}
}

sub output_genre_index
{
	my $genre;

	open(INDEX, ">$dir/genres/index.html")
		or die("failed to open $dir/genres/index.html");
	write_head(\*INDEX, "genre index");
	print(INDEX "<ul>\n");
	foreach $genre (keys(%$genres)) {
		my $file = escape_name($genre);
		print(INDEX "<li><a href=\"$file.html\">$genre</a></li>\n");
	}
	print(INDEX "</ul>\n");
	print(INDEX "</html>\n");
	close(INDEX);
		
}

sub output_albums
{
	my $album;
	my $track;

	foreach $album (@albums) {
		open(ALBUM, ">$dir/albums/".$album->{"id"}.".html");
		write_head(\*ALBUM, $album->{"title"});
		my $genrefile = "../genres/".escape_name($album->{"genre"})
						.".html";
		print ALBUM <<EOF;
title: $album->{"title"}<br>
artist: $album->{"artist"}<br>
genre: <a href="$genrefile">$album->{"genre"}</a><br>
date: $album->{"date"}<br>
EOF
		print(ALBUM "<ul>\n");
		foreach $track (@{$album->{"tracks"}}) {
			print(ALBUM "<li>$track</li>\n");
		}
		print(ALBUM "</ul>");
		print(ALBUM "</body></html>\n");
		close(ALBUM);
	}
}

sub output_album_index
{
	my $album;

	open(INDEX, ">$dir/albums/index.html");
	print(INDEX "<html>\n<head><title>album title index"
			. "</title></head></body>\n");
	print(INDEX "<body>\n<ul>\n");
	foreach $album (@albums) {
		write_short_album(\*INDEX, $album);
	}
	close(INDEX);
}

sub xref_tag
{
	my $hash = shift();
	my $tag = shift();

	my $album;
	foreach $album (@albums) {
		my $value = $album->{$tag};
		if (!($hash->{$value})) {
			$hash->{$value} = [];
		}
		push(@{$hash->{$value}}, $album);
	}
}

sub readalbums
{
	my $in = shift();

	open(IN, "<$in");
	my $album = 0;
	my $l;
	my $id = 0;
	while ($l = <IN>) {
		if ($l =~ m/^album = (.*)$/) {
			if ($album) {
				push(@albums, $album);
			}
			$album = {"title" => $1, "id" => $id++};
		} else {
			$album or die("huh??\n");
			add_to_album($album, $l);
		}
	}
}

sub add_to_album
{
	my $album = shift();
	my $line = shift();
	for ($line) {
		/^artist = (.*)$/ && do { $album->{"artist"} = $1; last; };
		/^date = (.*)$/ && do { $album->{"date"} = $1; last; };
		/^genre = (.*)$/ && do { $album->{"genre"} = $1; last; };
		/^\t(.*)$/ && do
			{
				if (!$album->{"tracks"}) {
					$album->{"tracks"} = [];
				}
				push(@{$album->{"tracks"}}, $1);
				last;
			};
		print(STDERR "wierd line: $line\n");
	}
}
