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

use Data::Dumper;
use Getopt::Std;

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

use lib '/app/tools/rps/lib';
use RPS::Mechanical::UK::Process::RunController;


# Allow tuning of the verbosity of our output.
#
my $gVerbosityLevel = 1;


# Parse the command-line options.
#
my %gOptions;
_parseCommandLine(\%gOptions);


# Install a signal handler to catch ctrl-c
#
$SIG{INT} = \&CTRL_C;
$SIG{TERM} = \&KILL_TERM;

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


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


sub CTRL_C
{
    Common::Log::Print("caught a ctrl-c, aborting");
    $gController->interupt();
    exit(1);
}

sub KILL_TERM
{
    Common::Log::Print("caught a term signal, aborting");
    $gController->interupt();
    exit(1);
}


# Pass dates in 'yyyy-mm-dd' format.
#
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 (defined $opt{V})
    {
        $gVerbosityLevel = $opt{V};
    }
}


sub _usage
{
    print "\nusage: $0 -c client_id -r run_id [-V n]\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 artist royalty run to process\n";
    print "\t-V <N>\t\t\tVerbosity level - 0 means no output\n";
}

