#!/usr/bin/perl -w

use strict;
use DBI;
use Getopt::Std;

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...
my $sth = $dbh->prepare("SHOW TABLES LIKE 'sip_session';");
 $sth->execute();
 if (my $t =$sth->fetchrow_hashref){
    print "sip selfcheck already configured \n";
}

else{
    print "adding selfcheck.... \n";
    add_selfCheck();

}

#---------------------------------------------------------------------------------------
sub add_selfCheck{
    $dbh->do('drop table if exists sip_session');
    my $sql = <<_SQL_;
    CREATE TABLE `sip_session` (
      `sessionid` varchar(36) NOT NULL DEFAULT '',
      `sc` varchar(50) NOT NULL DEFAULT '',
      `ipaddress` varchar(15) NOT NULL DEFAULT '',
      `debug` enum('true','false') default 'false',
      `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`sessionid`),
      KEY `i_sc` (`sc`),
      KEY `i_ip` (`ipaddress`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8
_SQL_
    $dbh->do($sql);

    my $sth = $dbh->prepare(<<_SQL_);
    replace into opl_preference values (?,?,?,?,?,?,?,?,?)
_SQL_

    $sth->execute('selfcheck','0','Off','0|1','radio','SIP self-service checkout',0,1,13);
    $sth->execute('acsd_uuid','','','string','radio','ACSD UUID',1,1,13);

    $sth = $dbh->prepare(<<_SQL_);
    replace into opl_prefFormParam values (?,?,?,?)
_SQL_
    $sth->execute('selfcheck',0,'On','1');
    $sth->execute('selfcheck',1,'Off','0');

# Update 'selfcheck' if set
    my ($acsd_uuid) = $dbh->selectrow_array(<<_SQL_);
    select val from opl_preference where var='acsd_uuid'
_SQL_
    if ($acsd_uuid) {
      $dbh->do(<<_SQL_);
    update opl_preference set val='1',valShow='On' where var='selfcheck'
_SQL_
    }
}
# 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;
}
############################################################
