# 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::Main;


use strict;
use utf8;
use DBI;
use Vhffs::Functions;
use Vhffs::Conf;

use constant
{
    VHFFSCONFPATH => "/etc/vhffs/vhffs.conf"
};

# Current user UID. On the panel it would be
# the currently logged user, on IRC moderation
# the user who talk to the bot.
my $current_user_uid;

sub init
{
	my $self;
	my $this;
	my $dbh;
	my $class;
	my $config;
	my $db_config;
	
	$this = shift;
 
	#First, create the object
	$self={};
	$class = ref($this) || $this;
	bless($self , $class );


	#Then, config stuff
	$config = new Vhffs::Conf( VHFFSCONFPATH );
	$self->{'config'} = $config if (defined $config);
	$self->{'config'}->parse;


	#Finally, backend stuff
	$db_config = $config->get_database();
    
    $dbh = Vhffs::Functions::db_connect $db_config;
    if(defined $dbh) {
    	$self->{'db'} = $dbh;
	} else {
        warn "Unable to open database connection\n";
		undef $self;
	}

	return $self;
}

sub get_db_object
{
	my $self = shift;

	if( defined $self->{'db'} )
	{
		return $self->{'db'};
	}
	else
	{
		return undef;
	}
}

sub get_config
{
	my $self = shift;

	if( defined $self->{'config'} )
	{
		return $self->{'config'};
	}
	else
	{
		return undef;
	}
}

sub is_valid
{
    my $self = shift;
    return (defined $self->{db} && $self->{db}->ping() > 0);
}

sub reconnect {
    my $self = shift;

    my $db_config = $self->{config}->get_database();

    my $dbh = Vhffs::Functions::db_connect $db_config;
    if(defined $dbh) {
        $self->{'db'} = $dbh;
    } else {
        warn "Unable to open database connection\n";
    }
}

sub current_user_uid
{
	$current_user_uid = $_[0] if(defined $_[0]);
	return $current_user_uid;
}

1;

__END__

=head1 NAME

Vhffs::Main - The main class of Vhffs API : connect to the database, ...

=head1 SYNOPSIS

	use Vhffs::Main;
	my $vhffs = init Vhffs::Main;
	my $conf = $vhffs->get_config;

=head1 DESCRIPTION

The Vhffs::Main object is VERY important on VHFFS platform. When you
invoke init Vhffs::Main, it creates a Vhffs::Main object, so : 

	- It creates connections to backend

	- Read configuration and create a Vhffs::Conf object

The utility of this object if to have a Db connection and the config
in one object.

=head1 METHODS

init() : initialize the object

get_config() : Return a Vhffs::Conf object

get_db_object : Return a DBI object

is_valid() : returns 1 if the object is valid (good database connection, ...), 0 otherwise

current_user_uid([$uid]): get or set the current user's UID.

=head1 SEE ALSO
Vhffs::User, Vhffs::Group

=head1 AUTHOR

	Julien Delange <julien at gunnm dot org>

=head1 COPYRIGHT

	Copyright 2005 by Julien Delange

