#!/usr/bin/perl -w
=item
command to update all the sites:

for i in `ls /etc/opals/conf/`; do \cp -p /www/opals/script/update_20090615 /tmp/urt; sudo perl -pi -e "s/_MY_SITE_/$i/" /tmp/urt; sudo perl /tmp/urt; done

=cut
use lib '/www/opals/module';
use Opals::Context("/etc/opals/conf/_MY_SITE_");
#use Opals::Context("/etc/opals/conf/ztest");

use strict;
use DBI;
#use Getopt::Std;
use POSIX qw(
    ceil
);
my $dbh = Opals::Context->dbh();
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;
# Codes start...


my $zdbDir = Opals::Context->config('zRoot') .'/'
           . Opals::Context->config('zPort') .'/'
           . 'record' .'/'
           . Opals::Context->config('zDatabase');

my $indexDir;
my $attTbl;
my $query_insert ;

#change field modDate from timestamp -> datetime 
# to preserve time stamp


updateTables($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 updateTables{
    my ($dbh)=@_;
    my $rec;




#backup 

$dbh->do('drop table if exists opl_transactions_bk');
$dbh->do('create table opl_transactions_bk select * from opl_transactions');
#reset settleDate
$dbh->do('update opl_transactions set settleDate=null');
$dbh->do('update opl_odl set settleDate=null');
my $sth= $dbh->prepare('select * from opl_preference where var=?');
my $sth_addPref=$dbh->prepare("insert into opl_preference set var='amazonSecreteKey',description='Amazon Secret Access Key'");
my $sth_updateTrans=$dbh->prepare("update opl_transactions set settleDate=? where settleDate is null && tid <= ? && uid=?");
my $sth_updateTransCode=$dbh->prepare("update opl_transactions set code=? where description regexp ?");

my $sth_updateODL=$dbh->prepare("update opl_odl o inner join opl_transactiondetail d on d.odl_id =o.odl_id inner join opl_transactions t on t.tid=d.tid set o.settleDate=t.settleDate");

 $sth->execute('amazonSecreteKey');
 if(!($rec =$sth->fetchrow_hashref)){
     $sth_addPref->execute();
 }  
 $sth= $dbh->prepare('select tid,date,uid from opl_transactions where balance <=0 order by tid');
 $sth->execute();
 while(my ($tid,$date,$uid)=$sth->fetchrow_array){
    $sth_updateTrans->execute($date,$tid,$uid);
 }
 $sth_updateODL->execute();
 $sth_updateTransCode->execute(1,'overdue');
 $sth_updateTransCode->execute(2,'lost');
 $sth_updateTransCode->execute(3,'damage');
 $sth_updateTransCode->execute(4,'payment');
 $sth_updateTransCode->execute(5,'forgive');
 $sth->finish;
 $sth_addPref->finish;
 sth_updateODL->finish;
 sth_updateTransCode->finish;
 sth_updateTrans->finish;
}

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