#------------------------------------------------------------
# Copyright (C) 2010 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package BookPub::Catalog::Import::Job::Base;

use strict;
use warnings;

use Sys::Hostname;

use lib '/app/tools/common/lib';
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 _defaultClass { return 'DTMVCatalogImport'; }

sub _generateCommandLine {
    my ($self) = @_;

    my $importID = $self->getCommandLineArg('importID');
    assert($importID);

    my $clientID = $self->clientID();
    assert($clientID);

    my $logLevel = $self->getCommandLineArg('logLevel');
    $logLevel = 1 unless $logLevel;

    # !!! Use the 'config' system to get the path to the royalty scripts.
    #
    my $config     = BookPub::Config->new();
    my $scriptPath = $config->get('catalog_import_script_dir');

    # !!! Note that this is a linux-only command line.
    # !!! If we end up wanting to run these on windows <shudder>, we'd have
    # !!! to create a 'Win32' subclass and override this method.
    # !!! Actually, we'd need to create a 'linux' subclass as well.
    #
    my $executable  = $self->_scriptName();
    my $commandLine = "nice $scriptPath/$executable -c $clientID -i $importID -V $logLevel";

    return $commandLine;
}

sub _scriptName {
    my ($self) = @_;
    assert( 0, 'override this to return the name of the script to run' );
}

sub generateLogBasePath {
    my ($self) = @_;

    my $clientID = $self->clientID();
    assert($clientID);

    my $importID = $self->getCommandLineArg('importID');

    return ( ref($self) )->LogFilePath( $clientID, $importID );
}

# I'm making this a static public method, so I can somewhat abstractly generate paths
# from things like the delete script (which won't have a Job object to kick around).
#
sub LogFilePath {
    my ( $class, $clientID, $importID ) = @_;
    assert($clientID);
    assert($importID);

    my $config   = BookPub::Config->new();
    my $basePath = $config->get('catalog_import_staging_dir');

    my $clientData = Common::DB::Item::Client->Lookup( client_id => $clientID );

    $basePath .= '/' . $clientData->client_name_clean . "/$importID";

    return $basePath;
}

###
1;    #
###

