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

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

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace GNUBoat;

/**
 * @brief Loads the configuration from webservice/config/config.php (if file exists) and provides the settings for usage
 */
class Config
{
	public function __construct()
	{
		$configFile = __DIR__ ."/../../config/config.php";
		if(file_exists($configFile)){
			include($configFile);
			$this->_config = $config;
		}
		else{
			$this->_config = array();
		}
	}

	public function databaseFile()
	{
		return $this->dataDirectory() ."gnuboat.rrd";
	}

	public function shutdownRequestFile()
	{
		if(array_key_exists("shutdownfile", $this->_config))
			return $this->_config["shutdownfile"];
		return "/var/spool/gnuboat/shutdown.request";
	}

	public function dataDirectory()
	{
		if(array_key_exists("datadirectory", $this->_config))
			return $this->addTrailingSlash($this->_config["datadirectory"]);
		return "/var/lib/gnuboat/";
	}

	public function logDirectory()
	{
		if(array_key_exists("logdirectory", $this->_config))
			return $this->addTrailingSlash($this->_config["logdirectory"]);
		return "/var/log/gnuboat/";
	}

	private static function addTrailingSlash($path)
	{
		$length = strlen($path);
		if(substr($path, $length - 1, 1) != "/"){
			return $path ."/";
		}
		return $path;
	}

	private $_config;
}

?>
