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

define("MP3_DIR", "/space/mp3/");
define("SYM_DIR", "/space/mp3/# ID3-Genres/");

define("DIR_LEVEL", 2);
define("GENRE_FILE_THRESHOLD", 0.25);
define("SCR_UPD_LOOPS", 4);

include "/home/six/id3.class.php";

function ewait() {

	static $w;

	echo chr(8);
	$w = ($w+1)%4;

	switch ($w) {

		case 0: echo "|"; break;
		case 1: echo "/"; break;
		case 2: echo "-"; break;
		case 3: echo "\\"; break;

	}

}

echo "mp3 symlinks-by-id3-genre creator (c)2003 by sIX / aEGIS\n--------------------------------------------------------\n\n";

// Get dir listing

echo "getting directory listing ... ";

$cmd = "find ".MP3_DIR." -iname \"*.mp3\" -follow | grep -v \"".SYM_DIR."\" 2>/dev/null";
$files = explode("\n", trim(`$cmd`));
$nfiles = count($files);

echo "OK (".$nfiles." files)\n";

// Sort in "sets"

echo "building file sets        ... ";

$sets = array();

foreach ($files as $fname) {

	$sdir = DIRECTORY_SEPARATOR.strtok($fname, DIRECTORY_SEPARATOR);
	for ($a=0; $a<=DIR_LEVEL; $a++) $sdir .= DIRECTORY_SEPARATOR.strtok(DIRECTORY_SEPARATOR);
	$sname = strtok("");

	if (is_array($sets[$sdir])) {

		$sets[$sdir]["files"][] = $sname;

	} else {
	
		$set = array();
		$set["dir"] = $sdir;
		$set["files"] = array($sname);
	
		$sets[$sdir] = $set;

	}

}

echo "OK (".count($sets)." sets)\n";

// Read ID3 tags

echo "reading ID3 tags          ... ";

echo " ";
$ls = "";

$used_genres = array();

foreach ($sets as $k=>$set) {

	$s_genres = array();
	$tot_genres = 0;

	foreach ($set["files"] as $fname) {
	
		$id3 = new id3($set["dir"].DIRECTORY_SEPARATOR.$fname);

		if ($id3->genre) {
			
			++$s_genres[$id3->genre];
			++$tot_genres;

		}

	}

	$s_g = array();
	
	foreach ($s_genres as $genre=>$nb) if (($nb/$tot_genres)>=GENRE_FILE_THRESHOLD) {
		
		if (count($s_g)==0) {

			++$tot_sets_with_genre;
			$tot_files_with_genre += count($set["files"]);
		
		}
		
		$s_g[]=$genre;

		++$used_genres[$genre];
		
	}

	$sets[$k]["genres"] = $s_g;

	$g_fnum += count($set["files"]);
	
	if ($l++%SCR_UPD_LOOPS==0) {

		for ($a=0; $a<strlen($ls); $a++) echo chr(8);
		ewait();

		$ls = " (".(int)$g_fnum."/".$nfiles." ".sprintf("%.1f", ($g_fnum/$nfiles)*100)."%)";
		echo $ls;

	}

//	if ($g_fnum>1000) break;

}

for ($a=0; $a<=strlen($ls); $a++) echo chr(8);

echo "OK (".count($used_genres)." genres, ".$tot_sets_with_genre."/".count($sets)." sets, ".$tot_files_with_genre."/".count($files)." files)\n";

arsort($used_genres);

// (re)create direcotries

echo "recreating directories    ... ";

$cmd = "ls -1 \"".SYM_DIR."\"";
$ex_dirs = explode("\n", trim(`$cmd`));

$rem_dirs = $cre_dirs = 0;

foreach ($ex_dirs as $ex) {

	$cmd = "rm -f \"".SYM_DIR.$ex."/\"*";
	system($cmd);
	$cmd = "rm -rf \"".SYM_DIR.$ex."\"";
	system($cmd);

	++$rem_dirs;

}

foreach ($used_genres as $ug=>$fn) {

	$cmd = "mkdir -p \"".SYM_DIR.$ug."\"";
	system($cmd);

	++$cre_dirs;

}

echo "OK (".(int)$rem_dirs." removed, ".(int)$cre_dirs." created)\n";

// create symlinks

echo "creating symbolic links   ... ";

echo " ";

foreach ($sets as $set) {

	if (is_array($set["genres"])) foreach ($set["genres"] as $sg) {

		$cmd = "ln -s \"".$set["dir"]."\" \"".SYM_DIR.$sg."/\"";
		system($cmd);

		++$cre_sl;
	
	}

	if ($l++%SCR_UPD_LOOPS==0) ewait();	

}

echo chr(8);
echo "OK (".(int)$cre_sl." created)\n";

echo "\n";

echo "done\n";

?>
