package Opals::Context;

#use Exporter;
#@ISA       = qw(Exporter);
# Symbols to be exported by default
#@EXPORT    = qw(
#    config
#    preference
#    dbh
#);
# Symbols to be exported on request
#@EXPORT_OK = qw(
#    
#);
# Version number
$VERSION   = 0.01;


#use utf8;
use strict;
use DBI;


#use constant OPALS_CONF => "$ENV{'OPALS_ROOT'}/config/conf.opals";
my $g_context;
########################################################################

sub import {
    my $module = shift;
    my $configFile = shift;

    $g_context = Opals::Context->new($configFile) unless $g_context;
#    Opals::Context->export_to_level(1, @_);
}

########################################################################

sub new {
    return $g_context if (defined $g_context);
    my $module = shift;
    my $configFile = shift;
    my $self = {};

    #unless (defined $configFile && -e $configFile) {
    unless ($configFile && -e $configFile) {
        #$configFile = $ENV{'OPALS_CONF'} || OPALS_CONF;
        $configFile = $ENV{'OPALS_CONF'};
    }
    
    $self->{'config_file'} = $configFile;
    
    # Load configurations
    $self->{'config'} = load_config($configFile);

    #$self->{'dbh'} = undef;

    $ENV{'Z_INDEX_BASE'} = 0;
#    $ENV{'Z_INDEX_BASE'} = 1;

    bless $self, $module;
    return $self;
}

########################################################################

sub getContext {
    return $g_context;
}

########################################################################

sub load_config {
    my ($configFile) = @_;
    my $config;# = {};

    open CONF, $configFile || die "Cannot open file $configFile";
    while (<CONF>) {
        chomp;
        s/#.*//;                # remove comments
        next if /^\s*$/;        # ignore blank lines

        if (/^\s*(\w+)\s*=\s*(.*?)\s*$/) {
            $config->{$1} = $2;
        }
    }
    close CONF;
    
    return $config;
}

########################################################################

sub config {
    my $self      = shift;
    my $configVar = shift;
    
    if (defined $g_context->{'config'}) {
        return $g_context->{'config'}->{$configVar};
    }

    return;
}

########################################################################

sub preference {
    my $self = shift;
    my $var = shift;
    my $dbh = dbh();
    my ($val) = $dbh->selectrow_array(<<_STH_);
select val
from   opl_preference
where  var='$var'
_STH_
    $dbh->disconnect();

    return $val;
}

########################################################################

sub dbh {
#    return $g_context->{'dbh'} if (defined $g_context->{'dbh'});
    
    my ($db_driver, $db_name, $db_host, $db_port, $db_user, $db_password);
    $db_driver   = $g_context->{'config'}->{'db_driver'} || 'mysql';
    $db_name     = $g_context->{'config'}->{'db_name'};
    $db_host     = $g_context->{'config'}->{'db_host'};
    $db_port     = $g_context->{'config'}->{'db_port'}   || '3306';
    $db_user     = $g_context->{'config'}->{'db_user'};
    $db_password = $g_context->{'config'}->{'db_password'};

    my $dsn = "dbi:$db_driver:$db_name:$db_host:$db_port";

#    return DBI->connect($dsn, $db_user, $db_password);
    my $dbh = DBI->connect($dsn, $db_user, $db_password);

    my ($TZ) = $dbh->selectrow_array(<<_STH_);
select val
from   opl_preference
where  var='timezone'
_STH_

    if ($TZ) {
        $ENV{'TZ'} = $TZ;
        $dbh->do("set time_zone='$TZ'");
    }

    return $dbh;
#    $g_context->{'dbh'} = DBI->connect($dsn, $db_user, $db_password);
#
#    return $g_context->{'dbh'};
}

1;
