#!/usr/bin/perl

use strict;

use Data::Dumper;
use File::Path;
use File::Basename;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use lib '/app/tools/data_classes/lib';
use lib '/app/tools/mediaserve/lib';
use Common::Util;
use Common::RSApp;
use Common::Locale;
use Common::Log;
use BookPub::Catalog::Import::ONIX;

use BookPub::DB::Item::Book;
use BookPub::DB::Item::BookContributor;
use BookPub::DB::Item::BookFormat;
use BookPub::DB::Item::BookSubject;
use BookPub::DB::Item::Product;
use BookPub::DB::Item::ProductType;
use BookPub::DB::Item::BookProduct;
use BookPub::DB::Item::Chapter;
use BookPub::DB::Item::Imprint;
use BookPub::DB::Item::ContributorRole;
use BookPub::DB::Item::Contributor;
use Client::Client;
use MediaServe::DB::Item::MediaFile;
use MediaServe::SFTP;
use MediaServe::Server;

use BookPub::DB::Item::BookProductImageFile;

use constant kMediaServerUser     => 'web';
use constant kMediaServerPassword => 'getImages';
use constant kMediaServerIdent    => undef;

die "\nusage:$0 SRC_FILE\n" unless ( scalar @ARGV == 1 );

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

my $imageDirectoryPath = $ARGV[0];
die "ERROR - no image path specified" unless $imageDirectoryPath;
die "ERROR - $imageDirectoryPath is not a directory" unless ( -d $imageDirectoryPath );

# So, what we're gonna to here is loop through all the 'pending' media files,
# find the corresponding book product images, then _if_ the image is available, upload it.
#
my $currentHost;

my $sftp = MediaServe::SFTP->new(
    user     => kMediaServerUser,
    password => kMediaServerPassword,
    identity => kMediaServerIdent,
);

my $imageFileList = BookPub::DB::Item::BookProductImageFile->GetAll();
while ( my $imageFile = $imageFileList->next() ) {
    my $mediaFileID = $imageFile->media_file_id;
    my $mediaFile = MediaServe::DB::Item::MediaFile->Lookup( media_file_id => $mediaFileID );
    if ( $mediaFile && 'pending' eq $mediaFile->status ) {

        # Find the file.
        #
        my $fullPath = $imageDirectoryPath . "/" . $mediaFile->filename;
        if ( -f $fullPath ) {
            Common::Log::Print("file: $fullPath  mediaFileID $mediaFileID");
            my ( $host, $path ) = MediaServe::Server::GetImagePath( clientID => Common::RSApp::GetClientID(), mediaFileID => $mediaFileID );

            # HARD CODE THIS PUPPY
            $host = 'cm01.royaltyshare.com';

            Common::Log::Print("path: $path");
            if ( !$currentHost || $host ne $currentHost ) {
                $sftp->close() if $currentHost;

                $currentHost = $host;
                Common::Log::Print("connecting to $currentHost");
                $sftp->connect($currentHost);
            }

            Common::Log::Print("mkdir");
            $sftp->mkdir( dirname($path) );
            Common::Log::Print("put");
            if ( !$sftp->put( $fullPath, $path ) ) {
                Common::Log::Print("- TRANSFER FAILED: $@");
            } else {
                Common::Log::Print("+ transfered");

                # Flag the media file record as 'active'

                $mediaFile->status('active');
                $mediaFile->save();
            }
        } else {
            Common::Log::Print("... cannot find $fullPath");
        }
    }
}

$sftp->close();

