<?php

require_once "lib/installer/installer_sqlstatements.php";
require_once "lib/installer/installer_functions.php";
require_once "lib/installer/installer_default_settings.php";
output("`@`c`bBuilding the Tables`b`c");
output("`2I'm now going to build the tables.");
output("If this is an upgrade, your current tables will be brought in line with the current version.");
output("If it's an install, the necessary tables will be placed in your database.`n");
output("`n`@Table Synchronization Logs:`n");
rawoutput("<div style='width: 100%; height: 150px; max-height: 150px; overflow: auto;'>");
$descriptors = descriptors($DB_PREFIX);
require_once "lib/tabledescriptor.php";
reset($descriptors);
while (list($tablename, $descriptor) = each($descriptors)) {
    output("`3Synchronizing table `#{$tablename}`3..`n");
    synctable($tablename, $descriptor, true);
    if ($session['dbinfo']['upgrade'] == false) {
        //on a clean install, destroy all old data.
        db_query("TRUNCATE TABLE {$tablename}");
    }
}
rawoutput("</div>");
output("`n`2The tables now have new fields and columns added, I'm going to begin importing data now.`n");
rawoutput("<div style='width: 100%; height: 150px; max-height: 150px; overflow: auto;'>");
$dosql = false;
reset($sql_upgrade_statements);
while (list($key, $val) = each($sql_upgrade_statements)) {
    if ($dosql) {
        output("`3Version `#%s`3: %s SQL statements...`n", $key, count($val));
        if (count($val) > 0) {
<?php

require_once "lib/installer/installer_functions.php";
if (httppostisset("DB_PREFIX") > "") {
    $session['dbinfo']['DB_PREFIX'] = httppost("DB_PREFIX");
}
if ($session['dbinfo']['DB_PREFIX'] > "" && substr($session['dbinfo']['DB_PREFIX'], -1) != "_") {
    $session['dbinfo']['DB_PREFIX'] .= "_";
}
$descriptors = descriptors($session['dbinfo']['DB_PREFIX']);
$unique = 0;
$game = 0;
$missing = 0;
$conflict = array();
$link = mysqli_connect($session['dbinfo']['DB_HOST'], $session['dbinfo']['DB_USER'], $session['dbinfo']['DB_PASS']);
mysqli_select_db($session['dbinfo']['DB_NAME']);
$sql = "SHOW TABLES";
$result = mysqli_query($sql);
while ($row = mysqli_fetch_assoc($result)) {
    list($key, $val) = each($row);
    if (isset($descriptors[$val])) {
        $game++;
        array_push($conflict, $val);
    } else {
        $unique++;
    }
}
$missing = count($descriptors) - $game;
if ($missing * 10 < $game) {
    //looks like an upgrade
    $upgrade = true;