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

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

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

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);
my $dbh = Opals::Context->dbh();
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;

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

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

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

    my($amazonId)=$sth->fetchrow_array() ;
    $amazonId =~ s/^\s+|\s+$//g;
    if($amazonId eq '1PTM7QSQNCVEE0NKP4G2'){
            print "=============\n";
            $sth = $dbh->prepare("update opl_preference set   val='AKIAI6EVEXB7RTKKVFCA' where var='amazonId'");
            $sth->execute();
            $sth = $dbh->prepare("replace into opl_preference 
                    set   val='/BYY8megi1yOsyywvBp/lgt+b1cVhIPH+hxyuAR6' , var='amazonSecreteKey',
                    description=' Amazon Secret Access Key'");
            $sth->execute();
    }
   
=item    
    if(!$amazonId){
        $sth = $dbh->prepare("replace into   opl_preference 
                              set   var='amazonId', val='AKIAI6EVEXB7RTKKVFCA',opt='string', description='Amazon Web Service ID' ");
        $sth->execute();
        $sth = $dbh->prepare("replace into   opl_preference 
                              set   var='amazonSecreteKey', val='/BYY8megi1yOsyywvBp/lgt+b1cVhIPH+hxyuAR6',opt='string', description=' Amazon Secret Access Key'");
        $sth->execute();
    }
    else{
        $amazonId =~ s/^\s+|\s+$//g;
       # if($amazonId eq '1PTM7QSQNCVEE0NKP4G2'){
            $sth = $dbh->prepare(" update  opl_preference set   val='AKIAI6EVEXB7RTKKVFCA' where var='amazonId'");
            $sth->execute();
            $sth = $dbh->prepare("replace into opl_preference 
                    set   val='/BYY8megi1yOsyywvBp/lgt+b1cVhIPH+hxyuAR6' , var='amazonSecreteKey',
                    description=' Amazon Secret Access Key'");
            $sth->execute();
        #}
    }
   
=cut     
 $sth->finish;



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

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