#!/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_20090216 /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;




$dbh->do(<<_SQL_);
alter table opl_itemType add `itemCategory` enum('1','2','3') NOT NULL default '1'
_SQL_

$dbh->do(<<_SQL_);
alter table opl_menuItem 
add `language` varchar(8) default 'en' after color
_SQL_

$dbh->do(<<_SQL_);
alter table opl_marcImport 
add addToNewitemList tinyint(1) unsigned default '0',
add newItemExpDate datetime default null
_SQL_


$dbh->do(<<_SQL_);
CREATE TABLE IF NOT EXISTS `opl_unionRequest` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `uid` tinyint(10) unsigned default NULL,
  `reqType` set('deletion','import') default 'deletion',
  `date` datetime default NULL,
  `status` enum('creating','pending','processing','sent') default 'creating',
  `unionSvrRespErr` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  KEY `uid` (`uid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1

_SQL_

$dbh->do(<<_SQL_);
CREATE TABLE IF NOT EXISTS `opl_unionRequestDetail` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `reqId` int(10) unsigned NOT NULL default '0',
  `rid` int(10) unsigned default '0',
  `barcode` varchar(50) default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1
_SQL_


$dbh->do(<<_SQL_);
CREATE TABLE IF NOT EXISTS `opl_userImg` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `uid` int(10) unsigned NOT NULL,
  `userbarcode` varchar(18) default NULL,
  `orgFile` mediumblob,
  `thumbnail` mediumblob,
  `mimeType` varchar(255) default NULL,
  `fileName` varchar(255) default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `u_user` (`uid`,`userbarcode`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1
_SQL_




my $sth= $dbh->prepare('select * from opl_preference where var=?');
my $sth_addPref=$dbh->prepare('insert into opl_preference set var=?,val=?,opt=?,description=?');
 $sth->execute('bibTimeclear');
 if(!($rec =$sth->fetchrow_hashref)){
     $sth_addPref->execute('bibTimeclear','60m','String','bibiography time clear');
 }
 $sth->execute('defaultSearchPage');
 if(!($rec =$sth->fetchrow_hashref)){
     $sth_addPref->execute('defaultSearchPage','0','0|1|2','Default search interface; 0:standard,1:beginner,2:advance');
 }
 $sth->execute('multilingual');
 if(!($rec=$sth->fetchrow_hashref)){
     $sth_addPref->execute('multilingual','0','0|1','0: default language; 1:multilingual');
 }
 $sth->execute('showBeginnerSrch');
 if(!($rec=$sth->fetchrow_hashref)){
     $sth_addPref->execute('showBeginnerSrch','1','1|0','enable or disable the beginner search screeen; 1:enable, 0:disable ');
 }
 $sth->execute('autoAdd2NewItemList');
 if(!($rec=$sth->fetchrow_hashref)){
     $sth_addPref->execute('autoAdd2NewItemList','0','1|0','Auto add to new item list in z-import,import or clone record; 1=on; 0=off');
 }
 $sth->execute('autoUpdateUnion');
 if(!($rec=$sth->fetchrow_hashref)){
     $sth_addPref->execute('autoUpdateUnion','0','1|0','1:enable auto update union; 0:disabled');
 }
  $sth->execute('unionId');
 if(!($rec=$sth->fetchrow_hashref)){
     $sth_addPref->execute('unionId','','text','union authentication id used for updating union site(deletion/import)');
 }
 $sth->execute('unionSvcUrl');
 if(!($rec=$sth->fetchrow_hashref)){
     $sth_addPref->execute('unionSvcUrl','1','1|0','union URL');
 }
 
$sth->finish;
$sth_addPref->finish;
}

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