use strict;
use Net::FTP;
use Data::Dumper;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';

use BookPub::DB::Item::CatalogImport;
use BookPub::Catalog::Import::Job::Fetch;
use Common::Log;
use Common::RSApp;

my $app = Common::RSApp->new( clientID => 308 );

my $ftp = Net::FTP->new("upload.royaltyshare.com");
die "No worky" unless $ftp;

$ftp->login( 'rsbookpub', 'klepton0n0' ) or die "ERROR - login failed";
$ftp->binary() or die "ERROR - 'binary' failed";
$ftp->cwd('hbgusa/_archive') or die "ERROR - cd failed";

Common::Log::Print("fetching _archive directory listing");
my $directoryListing = $ftp->ls();    # short form, just filenames

my @directories;
foreach my $file (@$directoryListing) {
    next unless $file =~ /^2010/;

    push @directories, $file;
}

@directories = sort @directories;

# Let's _disconnect_ here, and reconnect each time.
# Otherwise I suspect we'll timeout...
#
$ftp->quit();
$ftp = undef;

foreach my $d (@directories) {
    Common::Log::Print("processing $d");
    $ftp->login( 'rsbookpub', 'klepton0n0' ) or die "ERROR - login failed";
    $ftp->binary() or die "ERROR - 'binary' failed";
    $ftp->cwd('hbgusa') or die "ERROR - cd failed";

    # Rename the directory - put a '_' in front of it.  So we don't re-process these
    # files if we need to re-start this.
    $ftp->rename( "_archive/$d", "_archive/_" . $d );

    # 'Move' (ie rename) all the files in this directory to the main level.
    #
    $ftp->cwd( "_archive/_" . $d );
    my $files = $ftp->ls();
    $ftp->cdup();
    $ftp->cdup();
    foreach my $file (@$files) {
        Common::Log::Print("moving $file");
        $ftp->rename( "_archive/_" . $d . "/$file", $file );
    }

    # Disconnect immediately
    #
    $ftp->quit();
    $ftp = undef;

    # Queue up a catalog import.
    #
    my $catalogImport = BookPub::DB::Item::CatalogImport->Create( status => BookPub::DB::Item::CatalogImport::kImportStatusFetchReady, );
    $catalogImport->save();

    # Now queue the job.
    #
    my $job = BookPub::Catalog::Import::Job::Fetch->new( importID => $catalogImport->catalog_import_id(), );

    $job->enqueue();

    # Record the new job id in the catalog import record
    #
    $catalogImport->fetch_job_id( $job->id() );
    $catalogImport->save();

    my $catalogImportID = $catalogImport->catalog_import_id();

    Common::Log::Print( "Job id " . $job->id . " queued for catalog import id " . $catalogImport->catalog_import_id() );

    # Wait for that to finish.
    #
    while (1) {
        sleep(120);
        my $currentState = BookPub::DB::Item::CatalogImport->Lookup( catalog_import_id => $catalogImportID );
        my $status = $currentState->status();
        if ( 'DONE' eq $status ) {
            Common::Log::Print("  import has completed successfully, moving on");
            last;
        }

        if ( 'E' eq substr( 0, 1, $status ) ) {
            Common::Log::Print("  ERROR!  status = $status, aborting");
            die "ERROR encountered, aborting";
        }
    }

    # !!! Just do one for now...
    #
    last;
}

