<?php

	ini_set('include_path', '.');
	
	require_once("class.installer.php");
	
	$params = array();
	$params["db_type"] = getenv("DB_main_TYPE");
	$params["db_host"] = "localhost";
	$params["db_name"] = getenv("DB_main_NAME");
	$params["db_user"] = getenv("DB_main_LOGIN");
	$params["db_password"] = getenv("DB_main_PASSWORD");
	
	$params["admin_name"] = getenv("SETTINGS_admin_name");
	$params["admin_username"] = getenv("SETTINGS_admin_login");
	$params["admin_password"] = getenv("SETTINGS_admin_password");
	$params["admin_email"] = getenv("SETTINGS_admin_email");
	
	$url_scheme = getenv("BASE_URL_SCHEME");
    $url_host = getenv("BASE_URL_HOST");
    $url_port = getenv("BASE_URL_PORT");
    $url_path = getenv("BASE_URL_PATH");
    $url_full = $url_scheme . "://" . $url_host . ($url_port != "" ? ":$url_port" : "") . ($url_path[0] == "/" ? "" : "/") . $url_path;
	
    if($url_full[strlen($url_full)-1] == "/"){
		$url_full = substr($url_full, 0, strlen($url_full)-1);
	}
	$params["site_url_http"] = $url_full;
	$params["site_url_https"] = $url_full;
	
	$params["site_server_path"] = getenv("WEB___DIR");
	if($params["site_server_path"][strlen($params["site_server_path"])-1] == "/"){
		$params["site_server_path"] = substr($params["site_server_path"], 0, strlen($params["site_server_path"])-1);
	}
	
	
	if(count($argv) < 2){
    	print "Usage: configure (install | upgrade <version> | configure | remove)\n";
    	exit(1);
	}

	$command = $argv[1];

	//$ff = fopen($params["site_server_path"]."/install.log", "w+");
	//fputs($ff, "command: ".$command."\n");
	
	//fputs($ff, "params:\n".print_r($params, true)."\n");
	
	switch($command){
		case "install" : {
			$installer = new Installer();
			
			$installer->database_file_path = "install.sql";
			$installer->config_file_path = $params["site_server_path"]."/content/engine/engine_config.php";
			
			$installer->db_hostname = $params["db_host"];
			$installer->db_username = $params["db_user"];
			$installer->db_password = $params["db_password"];
			$installer->db_database = $params["db_name"];
			
			$installer->writeConfig();

			$installer->installDatabase(
				$installer->db_hostname, 
				$installer->db_username, 
				$installer->db_password, 
				$installer->db_database
			);
			
			$installer->setInitialParams($params);

			chmod($params["site_server_path"]."/content/compiled", 0777);
			chmod($params["site_server_path"]."/content/admin/upload", 0777);
			chmod($params["site_server_path"]."/content/admin/backup", 0777);
			
			$messages = $installer->messages;
			$errors = $installer->errors;
			if(count($errors) > 0){
				foreach($errors as $err){
					print $err."\n";
					//fputs($ff, $err."\n");
				}
			}
			else{
				foreach($messages as $msg) print $msg."\n";
				//fputs($ff, $msg."\n");
			}
			break;
		}
		
		case "upgrade" : {
			print "FIXME: $command is not implemented.\n";
    		exit(0);
			break;
		}
		
		case "remove" : {
			print "FIXME: $command is not implemented.\n";
    		exit(0);
			break;
		}
		
		case "configure" : {
			$installer = new Installer();
			
			$installer->database_file_path = "install.sql";
			$installer->config_file_path = $params["site_server_path"]."/content/engine/engine_config.php";
						
			$installer->updateParams($params);

			$messages = $installer->messages;
			$errors = $installer->errors;
			if(count($errors) > 0){
				foreach($errors as $err) print $err."\n";
			}
			else{
				foreach($messages as $msg) print $msg."\n";
			}
			break;
		}
		
		default : {
			print "Error: unknown command $command.\n";
			exit(1);
		}
	}
	
	//fclose($ff);

?>