#!/usr/bin/php
<?php
	chdir(dirname($argv[0]));
	require("../libraries/settings.php");

	/* Returns database table structure
	 */
	function get_structure($db, $table) {
		if (($columns = $db->execute("show columns from %S", $table)) == false) {
			return false;
		}

		$skip = array("id", "name");
		$structure = array();
		foreach ($columns as $column) {
			if (in_array($column["Field"], $skip) == false) {
				array_push($structure, $column["Field"]);
			}
		}

		return $structure;
	}

	/* Return pages
	 */
	function get_columns($page_file) {
		$columns = array();

		$file = config_file($page_file);
		foreach ($file as $page) {
			if ($page != "logout") {
				$page = str_replace("*/", "", $page);
				array_push($columns, $page);
			}
		}

		return array_unique($columns);
	}

	/* Connect to the database
	 */
	$db = new MySQLi_connection(DB_HOSTNAME, DB_DATABASE, DB_USERNAME, DB_PASSWORD);
	if ($db->connected == false) {
		exit("Internal error: database not available.\n");
	}

	/* Get column information
	 */
	if (($columns_db = get_structure($db, "roles")) == false) {
		exit("Error while reading table structure.\n");
	}
	$columns_new = get_columns("private_pages");

	/* Add columns
	 */
	foreach ($columns_new as $col) {
		if (in_array($col, $columns_db) == false) {
			print "Adding column '".$col."': ";
			if ($db->query("alter table roles add %S tinyint default 0", $col) == false) {
				print mysql_error();
			} else if ($db->query("update roles set %S=1 where id=1", $col) == false) {
				print mysql_error();
			} else {
				print "ok";
			}
			print "\n";
		}
	}

	/* Remove columns
	 */
	foreach ($columns_db as $col) {
		if (in_array($col, $columns_new) == false) {
			print "Deleting column '".$col."': ";
			if ($db->query("alter table roles drop %S", $col) == false) {
				print mysql_error();
			} else {
				print "ok";
			}
			print "\n";
		}
	}
?>
