<?php
/*
    Copyright (C) 2016 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/>.
*/

$configFile = __DIR__ ."../config/config.php";
if(file_exists($configFile))
	include($configFile);
else{
	error_log("No config file ". $configFile ." not found!");
	$config = array();
}

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

	public function databaseFile(){
		return $this->dataDirectory() ."log-webservice.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 addTrailingSlash($this->_config["datadirectory"]);
		return "/var/lib/gnuboat/";
	}
	
	private static function addTrailingSlash($path){
		$length = strlen($path);
		if(substr($path, $length - 1, 1) != "/"){
			return $path ."/";
		}
		return $path;
	}
	
	private $_config;
}

?>
