use DBI;

my $home="/usr/local/controller";
my $driver="mysql";
my $mysqlhost="localhost";
my $opt_user="";
my $opt_password="";
my $opt_base="";

sub configuration() {
	my $param = shift;
	my $value = "";
	my $conf="$home/scripts/scripts.conf";
	open(CONF,$conf) or die "Can't open config file!";
	my $confparam="";
	my $confvalue="";
	
	while(<CONF>){
	    chomp();
	#    print $_ . "\n";
	    ($confparam,$confvalue)=split(/=/,$_,2);
	    if($confvalue ne ""){
	        if($confparam eq "mysqlhost") { $mysqlhost=$confvalue; }
	        elsif($confparam eq "dbuser") { $opt_user=$confvalue; }
	        elsif($confparam eq "dbpass") { $opt_password=$confvalue; }
	        elsif($confparam eq "db") { $opt_base=$confvalue; }
			else {}
			if(defined($param)) {
				if($confparam eq $param) { $value=$confvalue; }
			}
	    }
	    else {
#	print "split didn't find = in line, failed.\n";
	    }
	}
	close(CONF);
	
	return $value;
}

sub satisMysqlConnect() {
	configuration();
	my $url = "DBI:$driver:$opt_base:$mysqlhost";
	my $dbh = DBI->connect($url, $opt_user, $opt_password, {RaiseError => 0});
	if(!$dbh) { 
    	print "<xml mysql=\"fail\"/>\n";
	    exit(1);
    	print "Unreachable code.\n";
	}
	return $dbh;
}

1;