#!/usr/bin/perl -w

use strict;
use DBI;
use Getopt::Std;
use POSIX qw(
    ceil
);

use File::stat;
use Time::localtime;
my %options = ();
getopts("c:",\%options);
my $configFile = $options{c};
if (!$configFile || ! -f $configFile) {
    print "Usage: $0 -c CONFIG_FILE\n";
    exit 1;
}

my $config = loadConfig($configFile);
my $dbh = makeConnection($config);
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;
# Codes start...

Update_insertTables($dbh);

# Codes end.

exit 0;

############################################################
sub makeConnection {
    my ($config) = @_;
    if (!$config) {
        return;
    }
    my ($db_driver, $db_name, $db_host, $db_port, $db_user, $db_password);

    $db_driver   = $config->{'db_driver'} || 'mysql';
    $db_name     = $config->{'db_name'};
    $db_host     = $config->{'db_host'};
    $db_port     = $config->{'db_port'}   || '3306';
    $db_user     = $config->{'db_user'};
    $db_password = $config->{'db_password'};

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

    return DBI->connect($dsn, $db_user, $db_password);
}
############################################################


sub loadConfig {
    my ($configFile) = @_;
#    print "Enter the config filename of Opals: ";
#    $configFile = <STDIN>;
    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 Update_insertTables{
    my($dbh)=@_;

 my ($rec) =$dbh->selectrow_array("select var from opl_preference where var='defaultSearchPage'");
 if (!$rec){
    $dbh->do("INSERT into opl_preference (var,val,opt,description) values('defaultSearchPage','0','0|1|2','Default search interface; 0:standard,1:beginner,2:advance') ");
 }

 ($rec) =$dbh->selectrow_array("select var from opl_preference where var='charge_damage'");

 if (!$rec)
 {
    $dbh->do("INSERT into opl_preference (var,val,opt,description) values('charge_damage', '1','0|1','set system fine for damage: 0=off; 1=on') ");   
    $dbh->do("INSERT into opl_preference (var,val,opt,description) values('charge_lost',   '1','0|1','set system fine for lost: 0=off; 1=on') ");   
    $dbh->do("INSERT into opl_preference (var,val,opt,description) values('charge_overdue','1','0|1','set system fine for overdue: 0=off; 1=on') ");   
    my ($overdueFine) = $dbh->selectrow_array("select val from opl_preference where var='fine'");
    if($overdueFine !=1){
        $overdueFine=0;
    }
    $dbh->do("update opl_preference set val=$overdueFine where var='charge_overdue'");
 }
    
    
 $dbh->do("UPDATE opl_zDatabase set host='scools.org' WHERE name='union' AND  host='union.dione.scoolaid.net'");
        
}

#////////////////////////////////////////////////////////////////////////////
