<?php
/*
    Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
    MA 02110-1301  USA.
*/

require(__DIR__ ."/../inc/envsetup.php");

function printError($message)
{
	echo "<div class=\"error\">\n";
	echo "	<h3>". _("The following error occured:") ."</h3>\n";
	echo $message;
	echo "</div>\n";
}

function printResult($result, $message)
{
	$class = "success";
	if(!$result){
		$class = "error";
	}
	echo "<li class=\"". $class ." li". $class ."\">". $message ."</li>\n";
}

$title = _("Setup");
include(__DIR__ ."/../inc/header.php");
?>

		<h2><?php echo _("Welcome to the setup"); ?></h2>
		<ul>
<?php

$setup = new SiWeather\Webservice\Setup();
$webserviceConfig = new SiWeather\Webservice\WebserviceConfig();

$errorMessage = "";
$result = $setup->dataDirectoryCheck($errorMessage);
if($result == SiWeather\Webservice\DataDirectoryChecks::NotExist && $result != SiWeather\Webservice\DataDirectoryChecks::Success){
	$result = $setup->dataDirectoryCreate($errorMessage);
}

if($result != SiWeather\Webservice\DataDirectoryChecks::Success){
	printResult(false, Translator::swGettext("Data directory not accessible (%s)", $errorMessage));
}
else{
	printResult(true, Translator::swGettext("Data directory checked (%s)", realpath($webserviceConfig->getConfig("dataDirectory"))));
	$result = $setup->databaseDirectoryCheck($errorMessage);
	if($result != SiWeather\Webservice\DatabaseChecks::Success){
		printResult(false, $errorMessage);
	}
	else{
		printResult(true, _("Database directory checked"));
		$result = $setup->databaseFileCheck($errorMessage);
		if($result != SiWeather\Webservice\DatabaseChecks::Success){
			printResult(false, $errorMessage);
		}
		else{
			printResult(true, _("Database file checked"));
			if(!$setup->databaseConnection($errorMessage)){
				printResult(false, $errorMessage);
			}
			else{
				$prefix = "Database up and running";
				$versionSqlite = $setup->databaseVersionSqlite($errorMessage);
				if($versionSqlite <= 0){
					printResult(false,  Translator::swGettext("%s (Could not determine SQLite version: %s)", $prefix, $errorMessage));
				}
				else{
					$versionSpatalite = $setup->databaseVersionSpatalite($errorMessage);
					if($versionSpatalite <= 0){
						printResult(false, Translator::swGettext("%s (Could not determine Spatalite version: %s)", $prefix, $errorMessage));
					}
					else{
						printResult(true, Translator::swGettext("%s (SQLite: %s, Spatalite: %s)", $prefix, $versionSqlite, $versionSpatalite));
					}
				}
			}
		}
	}
}

if(!$setup->apiCheckWithoutRewrite($errorMessage)){
	printResult(false, Translator::swGettext("Could not connect to webservice without rewrite (%s)", $errorMessage));
}
else{
	$url = $webserviceConfig->webserviceBaseUrl() ."index.php/";
	printResult(true, Translator::swGettext("Webservice withpout rewrite accessible at %s", "<a href=\"". $url ."\">". $url ."</a>"));
}
if(!$setup->apiCheckWithRewrite($errorMessage)){
	printResult(false, Translator::swGettext("Could not connect to webservice with rewrite (%s)", $errorMessage));
}
else{
	$url = $webserviceConfig->webserviceBaseUrl();
	printResult(true, Translator::swGettext("Webservice withp rewrite accessible at %s", "<a href=\"". $url ."\">". $url ."</a>"));
}

?>
		</ul>
<?php
	include(__DIR__ ."/../inc/footer.php");
?>
