#!/opt/local/bin/php
<?php

chdir('..');
require_once 'common.php';
Config::loadFile('app', 'config/app.ini');

$artist_name = $argv[1];

$db = Db::getInstance();
$a_model = Model::factory('musicbrainz', 'Artist');
$rg_model = Model::factory('musicbrainz', 'ReleaseGroup');
$r_model = Model::factory('musicbrainz', 'Release');
$t_model = Model::factory('musicbrainz', 'Track');

$artist_id = $a_model->getId($artist_name);

if (!$artist_id) {
	echo "Bad artist name!\n\n\n"; die;
}

$albums = $rg_model->getArtistAlbums($artist_id);
$release_groups = $r_model->getReleases(array_keys($albums), true);

$g_tracks = array();
foreach ($release_groups as $release_group => $releases) {
	$g_tracks[$release_group] = $t_model->getUniqueReleaseTracks(array_keys($releases));
}

foreach($g_tracks as $release_group => $tracks) {
	$tracks = $tracks['tracks'] + $tracks['bonus'];
	foreach ($tracks as $track) {
		$track_id = $track['id'];
		$track_title = addslashes($artist_name .' - '. $track['name']);
		$track_length = $track['length'];
		$q = "INSERT INTO beathaven.queue VALUES({$track_id}, 0, E'{$track_title}', {$track_length});";
		echo $q."\n";
		$db->q($q);
	}
}