#!/usr/bin/perl
use strict;

use Data::Dumper;
use Getopt::Std;
use POSIX qw(:sys_wait_h :signal_h :errno_h);

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::Process;
use Common::RSApp;
use Common::Log;
use Common::Assert;
use Common::DB::Item;
use BookPub::DB::Item::CatalogImport;
use BookPub::Catalog::Import::Process::Importer::Factory;
use BookPub::Catalog::Import::Process::Fetcher::Factory;
use BookPub::Catalog::Import::Job::ImageRetrieve;

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

my $gCatalogImportID = $gOptions{importID};
assert($gCatalogImportID);
my $gClientID = $gOptions{clientID};
assert($gClientID);

my $app = Common::RSApp->new( clientID => $gClientID );
BookPub::DB::Item::CatalogImport->CleanUp($gCatalogImportID);

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

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

    if ( !$opt{i} || !$opt{c} ) {
        _usage();
        exit(1);
    }
    $settings->{importID} = $opt{i};
    $settings->{clientID} = $opt{c};

    if ( defined $opt{V} ) {
        $gVerbosityLevel = $opt{V};
    }
}

sub _usage {
    print "\nusage: $0 -c client_id -f path_to_XML_file [-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-i <catalog_import_id>\tThe ID of the catalog import record we're processing.\n";
    print "\t-V <N>\t\t\tVerbosity level - 0 means no output\n";
}

