#!/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_insert_AmazonId /tmp/urt; perl -pi -e "s/_MY_SITE_/$i/" /tmp/urt; sudo /tmp/urt; done

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

use strict;
use DBI;
use Getopt::Std;
use POSIX qw(
    ceil
);
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);
$| = 1;

# Amazon
my $sth = $dbh->prepare(<<_SQL_);
select var 
from   opl_preference 
where  var='amazonId' 
_SQL_
$sth->execute;

my ($aid) = $sth->fetchrow_array();
if (!$aid) {
    $sth = $dbh->prepare(<<_SQL_);
insert into opl_preference 
set var='amazonId', val='',opt='string', description='Amazon Web Service ID'
_SQL_
    $sth->execute;
}
$sth->finish;

# Syndetics
$sth = $dbh->prepare(<<_SQL_);
select var 
from   opl_preference 
where  var='syndeticsId' 
_SQL_
$sth->execute;

($aid) = $sth->fetchrow_array();
if (!$aid) {
    $sth = $dbh->prepare(<<_SQL_);
insert into opl_preference 
set var='syndeticsId', val='',opt='string', description='Syndetics client code'
_SQL_
    $sth->execute;
}
$sth->finish;

# Circulation sound
$sth = $dbh->prepare(<<_SQL_);
select var 
from   opl_preference 
where  var='circulationSound' 
_SQL_
$sth->execute;

($aid) = $sth->fetchrow_array();
if (!$aid) {
    $sth = $dbh->prepare(<<_SQL_);
insert into opl_preference 
set var='circulationSound', val='1',opt='string', description='Set circulation sound system: 0=off; 1=on'
_SQL_
    $sth->execute;
}
$sth->finish;

# UTF-8 import
$dbh->do("alter table opl_marcImport add encoding enum('UTF-8','MARC-8') not null default 'UTF-8' after uid");
$dbh->do("update opl_marcImport set encoding='MARC-8'");

# UTF-8 export
$dbh->do("alter table opl_marcExport add eEncoding enum('UTF-8','MARC-8') not null default 'UTF-8' after eTo");
$dbh->do("update opl_marcExport set eEncoding='MARC-8'");



# 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;
}
