<?

if(file_exists("eval.code.version.txt"))
{
	$dbcodeversion_file=file("eval.code.version.txt");
	$dbcodeversion=trim($dbcodeversion_file[0]);
}
else
{
	echo "Could not load current eval.code.version.txt\n";
	exit;
}
include_once "../sfiab_common.inc.php";
//same fix here for mysql 5.1 not truncating the 16 char usernames
$DBUSER=substr($DBUSER,0,16);
mysql_connect($DBHOST,$DBUSER,$DBPASS);
mysql_select_db($DBNAME);
$q=mysql_query("SELECT db_version FROM eval_config");
$r=mysql_fetch_object($q);
$dbdbversion=$r->db_version;
if(!$dbdbversion)
{
	echo "Could not get current 'Evaluations' db version.  Is the plug-in properly installed?\n";
	exit;
}

if($dbcodeversion && $dbdbversion)
{
	//lets see if they match
	if($dbcodeversion == $dbdbversion)
	{
		echo "DB and CODE for 'Evaluations' are all up-to-date.  Version: $dbdbversion\n";
		exit;
	}
	else if($dbcodeversion<$dbdbversion)
	{
		echo "ERROR: dbcodeversion<dbdbversion ($dbcodeversion<$dbdbversion).  This should not happen!";
		exit;

	}
	else if($dbcodeversion>$dbdbversion)
	{
		echo "DB update requirements detected<br />";
		echo "Current 'Evaluations' DB Version: $dbdbversion<br />";
		echo "Current 'Evaluations' CODE Version: $dbcodeversion<br />";

		echo "Updating database from $dbdbversion to $dbcodeversion<br />";

		for($ver=$dbdbversion+1;$ver<=$dbcodeversion;$ver++)
		{
			if(file_exists("db.eval.update.$ver.sql"))
			{
				echo "db.eval.update.$ver.sql detected - running...";
				$exit_code = 0;   // assume no errors for now
				$filename = 'db.eval.update.'.$ver.'.sql';
				// Temporary variable, used to store current query
				$templine = '';
				// Read in entire file
				$lines = file($filename);
				// Loop through each line
				foreach ($lines as $line)
				{
					// Skip it if it's a comment
					if (substr($line, 0, 2) == '--' || $line == '')
						continue;
						// Add this line to the current segment
					$templine .= $line;
					// If it has a semicolon at the end, it's the end of the query
					if (substr(trim($line), -1, 1) == ';')
					{
						// Perform the query
						if(!mysql_query($templine)){
							echo('<br/>Error performing query!<br/>'.$templine.'<br/>  mysqlerror: '.mysql_error().'<br /><br />');
							$error_count += 1;
						    $exit_code = -1;   // do we bail out here or keep going?  keep going for now,  get all errors
						}
						// Reset temp variable to empty
						$templine = '';
					}
				}
				if($exit_code != 0) {
					/* mysql failed!, what now? */
					$error_count += 1;
		        	echo "<br /><b>ERROR in db_update: Failed to execute query(s) without error!</b><br />";
				}
				else{
					echo " Done!<br />";	
				}
			}
			else
			{
				echo "Version $ver SQL update file not found - skipping over\n";
			}
		}
		echo "<br />All done - updating  Evaluations' new DB version to $dbcodeversion\n";
		if ($error_count == 0 ){
	   		mysql_query("UPDATE eval_config SET db_version='$dbcodeversion'");
		}

	}

}
else
{
	echo "ERROR: dbcodeversion and dbdbversion are not defined\n";
}


?>