#!/usr/bin/perl
#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
use strict;

use Data::Dumper;
use Getopt::Std;
use POSIX qw(ceil);
use Carp;
use File::Path;

use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tools/raptor/lib';
use Raptor::DB::Item::Sale;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::UKMechanicalRun;
use RPS::DB::Item::McpsStatement;
use RPS::DB::Item::McpsStatementItem;
use RPS::DB::Item::SaleRunMap;
use RPS::Mechanical::UK::Process::RunController;
use RPS::Mechanical::UK::Job::Base;

my $gVerbosityLevel = 1;

# Get the id from the command-line
#
my %options;
_parseCommandLine( \%options );
_commitRun( $options{clientID}, $options{runID} );

sub _parseCommandLine {
    my ($settings) = @_;

    my %opt;
    getopts( 'r:c:V:', \%opt );

    if ( !$opt{r} || !$opt{c} ) {
        _usage();
        exit(1);
    }
    $settings->{runID}    = $opt{r};
    $settings->{clientID} = $opt{c};
    if ( $opt{V} ) {
        $gVerbosityLevel = $opt{V};
    }
}

sub _usage {
    print "\nusage: $0 -c client_id -r run_id\n";
    print "\n";
    print "Arguments:\n";
    print "\t-c <client_id>\t\tThe client_id of the client to process\n";
    print "\t-r <run_id>\t\tThe id of the UK mechanical royalty run to commit\n";
}

sub _commitRun {
    my ( $clientID, $runID ) = @_;
    assert($clientID);
    assert($runID);

    # Instantiate the application singleton.
    #
    my $appSingleton = Common::RSApp->new( clientID => $clientID );

    # Instantiate the run controller object, then let 'er rip.
    #
    my $controller = RPS::Mechanical::UK::Process::RunController->new( runID => $runID, logLevel => $gVerbosityLevel );

    my $exitCode = $controller->commit();
    exit($exitCode);
}

1;
