<?php
//---------------------------------------------------------------------------------------//
@set_time_limit(300);

//---------------------------------------------------------------------------------------//
// Grab all indexes ..we are gonna do em from scratch

// Only operate on these tables:
$tables_list = array(
			"newblocks"
);

$tables = mysql_list_tables($xoopsConfig['dbname']);
while (list($table_name) = $db->fetch_array($tables)) {
	$indexes = $db->query("SHOW INDEX FROM $table_name");
	while ($index_name = $db->fetch_array($indexes)) {
		$key_comp = strtolower($index_name['Key_name']);
		$key_name = $index_name['Key_name'];
		if ( !empty($key_comp) && ($key_comp != "primary") && ($key_comp != "unique") ) {
			$org_name = str_replace($db->prefix.'_', '', $table_name);
			if (in_array($org_name, $tables_list)) {
				$sql[] = "ALTER TABLE $table_name DROP INDEX $key_name";
			}
		}
	}
}
//---------------------------------------------------------------------------------------//

// newblocks


$sql[] = "ALTER TABLE ".$db->prefix("newblocks")." CHANGE show_mid show_mid VARCHAR(30) NOT NULL DEFAULT '0'";
$sql[] = "ALTER TABLE ".$db->prefix("newblocks")." ADD show_template varchar(40) NOT NULL default 'standard' AFTER show_mid";

//---------------------------------------------------------------------------------------//
// Execute the above SQL
$count = count($sql);
for ($i=0; $i<$count; $i++) {
	$db->query($sql[$i]) or print("<li>".$db->error()."</li><br />");
}

//---------------------------------------------------------------------------------------//
include_once(XOOPS_ROOT_PATH."/class/xoopsmodule.php");
$modules =& XoopsModule::getInstalledModules();

foreach ($modules as $module) {
	switch($module->dirname) {
		case 'meta-generator':
			@$module->uninstall();
			break;
			
		case 'newbb_plus':
			include_once(XOOPS_ROOT_PATH."/modules/newbb_plus/xoops_version.php");
			include_once(XOOPS_ROOT_PATH."/modules/newbb_plus/config.php");
		
			function get_current_version()
			{
			        global $db;
			        $xoopsModule = XoopsModule::getByDirname('newbb_plus');
			 
			        $sql = 'select version from '.$db->prefix('modules').' WHERE mid='.$xoopsModule->mid();
			        if($result = $db->query($sql))
			        {
			                if($db->num_rows($result) == 1)
			                {
			                        $row = $db->fetch_object($result);
			                        return $row->version;
			                }
			        }
			        return '';
			}
 
			$cur_ver = get_current_version();
			$new_ver = '0.81';
			echo "<h3>Newbb Plus Updater</h3>";
			echo "<hr>";
			echo "Current Version: ".$cur_ver."<br>";
			echo "New Version: ".$new_ver."<br>";
			$updater = XOOPS_ROOT_PATH.'/modules/newbb_plus/_update/update_'.$cur_ver.'_'.$new_ver.'.php';
			if (file_exists($updater))
			{
			        include ($updater);
			        $xoopsModule = XoopsModule::getByDirname('newbb_plus');
			        $xoopsModule->update();
			}else{
			        echo "<h3>No updater available for these versions!</h3>";
			        die();
			}
			echo "<hr>";
			echo "<h3>Update Complete!</h3>";
			echo "<h3><font color='#ff0000'>The '_update' directory should now be removed.</font></h3>";
 
			break;
	


		default:
			@$module->update();
	}
}
?>