#!/usr/bin/perl

BEGIN {
    if (!$ENV{'PERL5LIB'} || !$ENV{'OPALS_CONF'}) {
        print_help();
        exit 1;
    }

    sub print_help {
        print <<_STR_;
NAME:
    Set union URL or location ID in preferences.
SYNOPSIS:
    PERL5LIB=/www/opals/module OPALS_CONF=/etc/opals/conf/\$SITECODE \\
        $0 -u \$UNION_URL [-c on|off]
DESCRIPTION:
    -u  Union URL, eg. http://host.domain.com
    -c  Turn on circulation data update to union site.
_STR_
    }
}

use Getopt::Std;

my %options = ();
getopts("u:c:",\%options);
my $url = $options{u};
if (defined $url && $url !~ m/^http:\/\//) {
    print_help();
    exit 2;
}

use Opals::Context;
use Opals::WebServiceClient qw(
    wsc_getLibInfo
);

use strict;
my $dbh = Opals::Context->dbh();;
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;
# Codes start...
my $host = Opals::Context->preference('unionSvcUrl');

if (!$host) {
    if (!$url) {
        print "Please specify URL.\n\n";
        print_help();
        exit 2;
    }

    $host = $url;
    set_preference($dbh, $url, 'unionSvcUrl', 'URL', 'Union URL', 4);
}

$host =~ s/^http:\/\///;
$host =~ s/\/+$//;

print "Update location ID using data from $host";

my ($loc_code) = $dbh->selectrow_array(<<_SQL_);
select  lCode
from    opl_library
where   lid = 3
_SQL_
print " for [$loc_code]: ";

my $libInfo = wsc_getLibInfo(
        host        => $host, 
        loc_code    => $loc_code
    );

my @lids = keys %{$libInfo};
my $lid = $lids[0];

print $libInfo->{$lid}->{'unionId'}, "\n";
set_preference($dbh, $libInfo->{$lid}->{'unionId'}, 'unionId', 'text',
        'union authentication id used for updating union site(deletion/import)',
        2);

# if unionId not found, disable unionCircUpdate
if (!$libInfo->{$lid}->{'unionId'}) {
    set_preference($dbh, 0, 'unionCircUpdate', '0|1', 
        '1:enable auto update circulation data to union; 0:disabled', 5);
    exit 0;
}

if ($options{c}) {
    print 'Turn '.$options{c}.' update circulation data on union site.'."\n";

    if ($options{c} eq 'on') {
        set_preference($dbh, 1, 'unionCircUpdate', '0|1', 
            '1:enable auto update circulation data to union; 0:disabled', 5);
    }
    else {
        set_preference($dbh, 0, 'unionCircUpdate', '0|1', 
            '1:enable auto update circulation data to union; 0:disabled', 5);
    }
    
}

# Codes end.

exit 0;
################################################################################


sub set_preference {
    my ($dbh, $val, $var, $opt, $desc, $gOrder) = @_;

    my $valShow = $val;
    if ($opt eq '0|1') {
        $valShow = ($val) ? 'enabled' : 'disabled';
    }

    my $sth = $dbh->prepare(<<_SQL_);
update  opl_preference
set     val = ?,
        valShow = ?
where   var = ?
_SQL_

    my $rv = $sth->execute($val, $val, $var);
    if (!$rv) {
        print "Add union URL $val\n";
        $dbh->do(<<_SQL_);
insert
into    opl_preference
set     val = '$val',
        valShow = '$val',
        var = '$var',
        opt = '$opt',
        description = '$desc',
        hidden = 0,
        gid = 8,
        gOrder = $gOrder
_SQL_
    }

    $sth->finish;
}
