#!/usr/local/bin/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 59 2006-08-10 17:47:53Z peter $
#
#  $LastChangedRevision: 59 $

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 %sources;
our %plugins;
our $HTMLDIR;
our $low_water;
our $syslog_facility;
our $pidfile;

# Optional Notification vars
our $MAIL_FROM;
our $RCPT_TO;
our $SMTP_SERVER;

#
# 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 : "/usr/local/etc/nfsen.conf";
	if ( !-f "$CONFFILE" ) {
		$Log::ERROR = "No NFSEN config file found.";
		return undef;
	}
	
	# preset default values:
	$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;
	%sources		 = ();
	$HTMLDIR		 = "/var/www/htdocs/nfsen";
	$low_water		 = 0.9;
	$syslog_facility = 'local3';
	$pidfile		 = '/var/run/nfsen.pid';

	$MAIL_FROM		 = '';
	$RCPT_TO		 = '';
	$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;
	}

	$Log::ERROR = undef ;
	return 1;

} # End of LoadConfig

1;
