#------------------------------------------------------------
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package BookPub::RDAS::Job;

use strict;
use warnings;

use Data::Dumper;
use Sys::Hostname;

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

use lib '/app/tools/bookpub/lib';
use BookPub::Config;

use lib '/app/tools/job/lib';
use Job::Job;

use base 'Job::Job';

sub _generateCommandLine {
    my $self = shift;
    my %args = @_;

    my $config     = BookPub::Config->new();
    my $scriptPath = $config->get('rdas_script_dir');
    my $script     = $self->_scriptName();

    my $commandLine = "nice $scriptPath/$script " . $self->_parseOptions(@_);
    return $commandLine;
}

sub _parseOptions {
    my $self    = shift;
    my $options = $self->{CommandLineArguments}->{options};
    my $args    = $self->{CommandLineArguments}->{args};
    my $result  = '';

    return unless ($args);
    $result .= " -vv" unless ($options);

    foreach my $key ( keys(%$args) ) {
        if ( $options->{$key} && $options->{$key}->{parameter} ) {
            $result .= " --$key=$args->{$key}";
        }

        elsif ( $options->{$key} ) {
            die "$args->{$key} is not numeric" unless ( $args->{$key} =~ /^\d+$/ );

            for ( my $i = 0 ; $i < $args->{$key} ; $i++ ) {
                $result .= " --$key ";
            }
        }

        else {
            # If we have passed in option definitions then ignore any unknown
            # options
            $result .= " --$key=$args->{$key}" unless ( $options && keys(%$options) );
        }
    }

    return $result;
}

sub _defaultPriority { return Job::Job::kPriorityNormal; }

sub _scriptName { assert( 0, "_scriptName must be overloaded" ) }

###
1;    #
###
