package Login;

##########################################################
#                                                        #
# dictyBase Extension of Login                           #
#                                                        #
##########################################################

use Login_base;

use DictyBaseConfig;

BEGIN { %Login:: = %Login_base:: }

use dicty::UI::Form;
use FormatdictyBase qw (FooterReturnEmail Copyright);
use ModConfig;

#use dictyBaseCentralMod qw(:formatPage);

use lib "/usr/local/dicty/www_dictybase/db/lib/dictyBase/Objects";
use ConfigPathdictyBase;

my $conf = ModConfig->load;

our $dbh;
###################################################################################
sub GetPassword{
###################################################################################
# This subroutine uses the dbauth program to retrieve the password for a particular
# user in a particular database

    my ($database, $user) = @_;

    my $configPath = ConfigPathdictyBase->new;

#   my $password = `/usr/local/bin/db-auth $user\@$database`;

    my $command = 'perl '.$configPath->dictyBin."getPassword.pl $user";

    my $password = `$command`;

    chomp $password;

    if ($?){ # error in script

        &error_page("An error occurred during authentication.");

    }elsif(!$password){ # didn't get a password

        &error_page("An error occurred.  No authentication was provided.");

    }

    return ($password);

}
#
# changed connect to connect_cached which is a singleton method (if the a database
# connection exists with the given parameters, it returns that connection.
#
# changed back because there were too many disconnects which were disconnecting a valid 
#  database handle
#
#
####################################################################################
sub ConnectToDatabase{
####################################################################################
# This will return a database handle to the specified database, which must be passed
# in as the first argument.  If a username and password are passed
# into the function, the it will use those to log on, otherwise it will default to 
# webb

# Usage : my $dbh = &ConnectToDatabase($database, $user, $pass);
# 
# $user and $pass are optional.  If no user is supplied, the function will
# default to the user CGM_DDB.  If no password is supplied, the function will
# attempt to get the correct password for the user.

    my $database = lc(shift)
	|| die "Error you must pass a database name as the first argument to ConnectToDataBase\n";
    my $user = uc(shift) || $conf->value('DBUSER');
   # my $pass = shift || &GetPassword($database, $user);
    my $pass = shift || $conf->value('PASSWORD');
    my $sid = $conf->value('ORACLE_SID');
    my $host = $conf->value('DBHOST');

    $database = $conf->value('DATABASE');

    &SetEnvironment($database);
    $ENV{'ORACLE_HOME'} = $ENV{'ORACLE_ROOT'};

    eval {
    
	$dbh = DBI->connect_cached("dbi:Oracle:dictybase", $user, $pass, { RaiseError=>1, AutoCommit=>0 });
    
    };
    
    if ($@) {
       if ( $@ =~ /invalid username\/password/ ) {
          error_page("Invalid username/password");
       }
       else {
          warn $@;
          error_page ( "The database is not currently available.  It may be down for maintenance.  Please check back in an hour. We apologize for the inconvenience." );
       }

    }

    return $dbh;

}


sub error_page {
   my ($message) = @_;
   dictyBaseCentralMod->printStartPage("Database Error");
   dicty::UI::Form->print_error_box($message);
   dictyBaseCentralMod->FooterReturnEmail;
   exit(1);
}

1;
