<?php

if ( !file_exists("/etc/homeaudio.ini") ) {
   echo "Error: /etc/homeaudio.ini not found!\n\n";
   exit();
}

if ( false === ($config = parse_ini_file("/etc/homeaudio.ini")) ) {
   echo "Error: Could not parse /etc/homeaudio.ini!\n\n";
   exit();
}

// These two constants define the tables used from the old database
define("OLDSONGSTABLE", "music.songs");
define("OLDQUEUESTABLE", "music.queues");

// These three constants define the tables used from the new database
define("SONGSTABLE", "musicnew.songs");
define("NEWQUEUESTABLE", "musicnew.queues");
define("QUEUECONTENTSTABLE", "musicnew.queuecontents");

define("MP3ROOTDIR", $config['MP3DIR']);
define("ARTDIR", $config['ARTDIR']);

$globaldbh = new PDO("mysql:host=localhost;dbname=" . $config['DBNAME'], $config['DBUSER'], $config['DBPASS']);
$ignoredirs = array();
$ignoredirs[] = "Album Playlists";
$ignoredirs[] = "Alphabetical";
$ignoredirs[] = "Incoming2";
$ignoredirs[] = "Recent";
$ignoredirs[] = "Recent (1-2 Months)";
$ignoredirs[] = "Recent (2-3 Months)";
$ignoredirs[] = "playlists";

$extbymimetypes = array(
   'image/bmp' => 'bmp',
   'image/jpeg' => 'jpg',
   'image/png' => 'png',
   'image/gif' => 'gif'
);

function convertQueues() {
   global $globaldbh;
   $query = "DELETE FROM " . NEWQUEUESTABLE . " WHERE type='user'";
   $sth = $globaldbh->prepare($query);
   $sth->execute();
   $query = "INSERT INTO " . NEWQUEUESTABLE . " (name) SELECT qname FROM " . OLDQUEUESTABLE . " WHERE qtype='user'";
   $sth = $globaldbh->prepare($query);
   $sth->execute();
}

function convertQueueContents() {
   global $globaldbh;
   $totalcount = 0;
   $convertedcount = 0;
   $queues = array();
   $query = "DELETE " . QUEUECONTENTSTABLE . " FROM " . QUEUECONTENTSTABLE . " LEFT JOIN " . NEWQUEUESTABLE . " ON qid=id WHERE type='user'";
   $sth = $globaldbh->prepare($query);
   $sth->execute();
   $query = "SELECT id, name FROM " . NEWQUEUESTABLE;
   $sth = $globaldbh->prepare($query);
   $sth->execute();
   while ( $row = $sth->fetch() ) {
      $queues[$row['name']] = $row['id'];
   }
   $query = "SELECT fullpath, qname FROM " . OLDSONGSTABLE . " LEFT JOIN " . OLDQUEUESTABLE . " ON qid=id WHERE qtype='user'";
   $sth = $globaldbh->prepare($query);
   $sth->execute();
   $query_new = "INSERT INTO " . QUEUECONTENTSTABLE . " (qid, songid) VALUES(:qid, :songid)";
   $sth_new = $globaldbh->prepare($query_new);
   $query_search = "SELECT id FROM " . SONGSTABLE . " WHERE song=:song AND genre=:genre";
   $sth_search = $globaldbh->prepare($query_search);
   while ( $row = $sth->fetch() ) {
      $fields_search = array();
      $fields_search[':song'] = basename($row['fullpath']);
      list($fields_search[':genre'], $junk) = explode("/", substr($row['fullpath'], strlen(MP3ROOTDIR)), 2);
      $sth_search->execute($fields_search);
      $totalcount++;
      if ( $row_search = $sth_search->fetch() ) {
         $fields_new = array();
         $fields_new[':qid'] = $queues[$row['qname']];
         $fields_new[':songid'] = $row_search['id'];
         $sth_new->execute($fields_new);
         $convertedcount++;
         if ( ($convertedcount % 100) == 0 ) echo ".";
      } else {
         echo "Couldn't find: ", $fields_search[':song'], " in ", $fields_search[':genre'], "\n";
      }
   }
   return array($totalcount, $convertedcount);
}

echo "Converting DB:\n\n";
echo "Converting user queues...\n";
convertQueues();
echo "Converting user queue contents...";
$convcount = convertQueueContents();
echo " converted ", $convcount[1], " out of ", $convcount[0], " songs in old queues.\n";

?>