#!%%PERL%%
#
#  Copyright (c) 2004, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions and the following disclaimer in the documentation
#     and/or other materials provided with the distribution.
#   * Neither the name of SWITCH nor the names of its contributors may be
#     used to endorse or promote products derived from this software without
#     specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#
#  $Author: peter $
#
#  $Id: NfConf.pm 75 2007-03-13 12:19:28Z peter $
#
#  $LastChangedRevision: 75 $

package NfConf;

use strict;
use Log;

our $BASEDIR;
our $BINDIR;
our $LIBEXECDIR;
our $CONFDIR;
our $DOCDIR;
our $VARDIR;
our $PROFILEDATADIR;
our $PROFILESTATDIR;
our $PLUGINDIR;
our $PREFIX;
our $USER;
our $GROUP;
our $WWWUSER;
our $WWWGROUP;
our $BUFFLEN;
our $SUBDIRLAYOUT;
our $DISKLIMIT;
our $COMMSOCKET;
our %sources;
our %sim;
our %plugins;
our $HTMLDIR;
our $low_water;
our $syslog_facility;
our $pidfile;
our $DEBUG;
our $AllowsSystemCMD;
our $SIMmode;
our $Refresh;

# Alerting email vars
our $MAIL_FROM;
our $MAIL_BODY;
our $SMTP_SERVER;

our $RRDoffset;

#
# Loads the config from nfsen.conf file
# returns 1 on success. 
# returns undef if failed. Set Log:ERROR
sub LoadConfig {
	my $InitConfigFile = shift;

	my $CONFFILE = defined $InitConfigFile ? $InitConfigFile : "%%CONFDIR%%/nfsen.conf";
	if ( !-f "$CONFFILE" ) {
		$Log::ERROR = "No NFSEN config file found.";
		return undef;
	}
	
	# preset default values:
	$DEBUG			 = 0;
	$BASEDIR		 = undef;
	$BINDIR			 = undef;
	$LIBEXECDIR		 = undef;
	$CONFDIR		 = undef;
	$DOCDIR			 = undef;
	$VARDIR			 = undef;
	$PROFILEDATADIR	 = undef;
	$PROFILESTATDIR	 = undef;
	$PLUGINDIR		 = undef;
	$PREFIX			 = '/usr/local/bin';
	$USER			 = 'nobody';
	$GROUP			 = undef;
	$WWWUSER		 = $USER;
	$WWWGROUP		 = 'www';
	$BUFFLEN		 = undef;
	$SUBDIRLAYOUT	 = 0;
	$DISKLIMIT		 = 98;
	$COMMSOCKET		 = undef;
	%sources		 = ();
	%sim		 	 = ();
	$HTMLDIR		 = "/var/www/htdocs/nfsen";
	$low_water		 = 0.9;
	$syslog_facility = 'local3';
	$pidfile		 = '/var/run/nfsen.pid';
	$RRDoffset	 	 = 0;
	$SIMmode		 = 0;
	$Refresh		 = 300;
	$AllowsSystemCMD = 0;

	$MAIL_FROM		 = undef;
	$MAIL_BODY		 = q{Alert '@alert@' triggered at timeslot @timeslot@};
	$SMTP_SERVER	 = '';

	# Read Configuration
	if ( ! open( TMP, $CONFFILE) ) {
		die "Can't read config file '$CONFFILE': $!\n";
	}
	close TMP;

	if ( !do $CONFFILE ) {
		$Log::ERROR = "Errors in config file: $@";
		return undef;
	}

	if ( defined $BASEDIR && ! -d $BASEDIR && defined $InitConfigFile ) {
		mkdir $BASEDIR or
			$Log::ERROR = "Can not create BASEDIR '$BASEDIR': $!",
			return undef;
	}

	if ( defined $BASEDIR && ! -d $BASEDIR ) {
		$Log::ERROR = "Config seems to be buggy. BASEDIR '$BASEDIR' not found!";
		return undef;
	}

	if ( !defined $COMMSOCKET ) {
		$COMMSOCKET = "$VARDIR/run/nfsen.comm";
	}

	$Log::ERROR = undef ;
	return 1;

} # End of LoadConfig

1;
