#!%PERL% -w 
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. 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.
#3. Neither the name of vhffs 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.

package Vhffs::Conf;

use strict;
use utf8;
use Encode;
use Config::General;

#Vhffs::Conf written by sod` <dieu AT gunnm DOT org>

#This is obviously a good way to manage the configuration of Vhffs
#Config::General helps a bit, for sure


my %Config;
my $filename;

sub new
{
    my ( $this , $param ) = @_;
    my $class = ref($this) || $this;

    my $self={
	filename => $param,
    };

    $filename = $param;

    bless($self , $class );
    return $self;
}


#Parse the main Vhffs Configuration file
sub parse
{
    my $conf;
    
    $conf = new Config::General( 
				 -ConfigFile => $filename ,
				 -UTF8 => 1
				 );
    %Config = $conf->getall();

    $Config{users}{available_shells} = '/bin/false' unless( defined $Config{'users'}{'available_shells'} );
    $Config{users}{default_shell} = '/bin/false' unless( defined $Config{'users'}{'default_shell'} );

    $Config{'global'}{'host_name'} = Encode::decode_utf8( $Config{'global'}{'host_name'} );

    return \%Config;
}


#return a reference to a hash with the specified configuration
sub get_global
{
	return $Config{'global'};
}

sub get_panel
{
	return $Config{'panel'};
}

sub get_gpg
{
	return $Config{'gpg'};
}

sub get_database
{
	return $Config{'database'};
}

sub get_users
{
	return $Config{'users'};
}

sub get_groups
{
	return $Config{'groups'};
}

# get a service configuration
# if a parameter is given, the configuration of the service will be returned
# if not, it returns the whole configuration for all services
sub get_service
{
	my ( $self , $service ) = @_;
	return $Config{"services"}{$service} if( defined $service );
	return $Config{"services"};    
}


# get availability of a service (return 1 if open, 0 if closed or unknown)
sub get_service_availability
{
	my ( $self , $service ) = @_;
	use Vhffs::Functions;
	return Vhffs::Functions::strtobool( $Config{'services'}{$service}{'activate'} );
}

sub get_listengine
{
	return $Config{'listengine'};
}

sub get_robots
{
	return $Config{'robots'};
}

sub get_irc
{
	return $Config{'irc'};
}


# -- global parameters ---

sub get_host_name
{
	return $Config{'global'}{'host_name'};
}

sub get_host_website
{
	return $Config{'global'}{'host_website'};
}

sub get_datadir
{
	return $Config{'global'}{'datadir'};
}

sub get_templatedir
{
	return $Config{'global'}{'templatedir'};
}

sub get_intldir
{
	return $Config{'global'}{'intldir'};
}

sub get_default_language
{
	return $Config{'global'}{'default_language'};
}

sub get_master_mail
{
	return $Config{'global'}{'vhffs_master'};
}

sub get_moderator_mail
{
	return $Config{'global'}{'vhffs_moderator'};
}

sub get_mailtag
{
    	return $Config{'global'}{'mailtag'};
}

sub get_allow_subscribe
{
	use Vhffs::Functions;
	return Vhffs::Functions::strtobool( $Config{'global'}{'allow_subscribe'} );
}

sub get_alert_mail
{
	return $Config{'global'}{'alert_mail'};
}

sub get_moderation
{
	use Vhffs::Functions;
	return Vhffs::Functions::strtobool( $Config{'global'}{'moderation'} );
}

sub use_vhffsfs
{
	use Vhffs::Functions;
	return Vhffs::Functions::strtobool( $Config{'global'}{'use_vhffsfs'} );
}

sub get_available_languages
{
    return split /\s/, $Config{'global'}{'available_languages'} if(defined $Config{'global'}{'available_languages'});
    return ('en_US');
}

1;
