#!/usr/local/bin/perl5.6.1
#######################################################################
# This software has been created by Genome Research Limited (GRL).    #
# GRL hereby grants permission to use, copy, modify and distribute    #
# this software and its documentation for non-commercial purposes     #
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  #
# otherwise in connection with the software, its use or the use of    #
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible #
# or liable for any loss or damage whatsoever arising in any way      #
# directly or indirectly out of the use of this software or its       #
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out #
# above, and must contain this copyright notice.                      #
#######################################################################
# $Id: load_waitingpool.pl,v 1.6 2004/08/12 13:50:45 kva Exp $


BEGIN {
#     warn "Using local version of load_waitingpool.pl...\n";
#     $ENV{DEBUG_WRAPDBI} = 1;
}


use WrapDBI;
use DBI;
require 'badgerGetOpt.pl';

use load_waitingpool;
#require '/nfs/team71/psg/kva/badger-bin/finch/load_waitingpool.pm';

my ($lp,$debug);
my $opts = {   
    "-lp" => {
        "type" => "text",
        "var" => \$lp,
        "help" => "List of loadingplates.",
    },

     "-debug" => {
        "type" => "boolean",
        "var"  => \$debug,
        "help" => "debugging tags",
    },

};

&badgerGetOpt($prog,'',$opts) && die "Quit\n";

# Database connection
#my $db = 'trackingclone_apr2002'; #development testing.
my $db = 'reports'; #live testing.
my $dbh1;
my $dbh2;

my $db2 = 'wp_api_user'; #live db

#FOR TESTING PURPOSES
#my $tdb2 = 'accesspool';
#my $tpwd = 'atestpool';
#my $tcnt = 'dbi:Oracle:tgtr';

eval {
    $dbh1 = WrapDBI->connect("$db", {RaiseError => 1});    
    $dbh1->{autocommit} = 0;

    $dbh2 = WrapDBI->connect("$db2", {RaiseError => 1});    
    $dbh2->{autocommit} = 0;

##FOR TESTING PURPOSES.
#    $dbh2 = DBI->connect("$tcnt", "$tdb2", "$tpwd", {RaiseError => 1}) || die $DBI::errstr;
#    $dbh2->{AutoCommit} = 0;

};
if ($@){
    print "CONNECTION ERROR OCCURED : $@\n";
    die;
}

##log into generitrack
##pull the data via db link from cas.
##cross check the output with what is in WAITING_POOL
##Then insert the mapping data into the WAITING_POOL table.

my @loadingplates=split',',$lp;


if ((@loadingplates)>0){
    foreach (@loadingplates){
        print "plate $_\n";
    }
    
    my $list = \@loadingplates;

    &load_waitingpool::load_wp($dbh1,$dbh2,$list,$debug);
    
} else {
    print "No loadingplate id was defined.\n";
}

END {
    if ($dbh1){
	eval {
	    $dbh1->rollback();
	    $dbh1->disconnect();
	}
    }
    if ($dbh2){
	eval {
	    $dbh2->rollback();
	    $dbh2->disconnect();
	}
    }
};
