#!/usr/bin/perl
# Script to view the decrypted contents of RSCOMMON.client_ftp.
# It mimics the lookup mechanicam used by the DTMV ftp_royalty_report.pl
# script (/app/tools/bookpub/bin/sale_report/ftp_royalty_report.pl), so
# as long as you can see the plaintext password with this script then
# the ftp_royalty_report.pl script should also see the plaintext password
# and work as expected.
#
# ---------------------------------------------------------------- 
#                         *** WARNING ***
# Do NOT run ftp_royalty_report.pl directly or you may end up
# sending data to external DTMV sites.
#                         *** WARNING ***
# ---------------------------------------------------------------- 
#

use strict;

use lib '/app/tools/common/lib';
use Common::RSApp;
use Common::DB::Item::Client;
use Common::DB::Item::ClientFTP;

use Getopt::Std;

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

my $clientID = $opt{c};

my $appSingleton = Common::RSApp->new( clientID => 0 );
my $dbo          = Common::RSApp::GetClientDB();


# Gather the FTP data using a collection and verify that data
# is decrypted as expected.  No external FTP connection is made
# by the following code.
#
my $coll;
if ( $clientID ) {
    $coll = Common::DB::Item::ClientFTP->GetFtpData($clientID);
    my $client = Common::DB::Item::Client->Lookup( client_id => $clientID );
    my $clientName = (defined $client) ? $client->client_name : 'RSCOMMON';
    print "Client: $clientName\n";

} else {
    $coll = Common::DB::Item::ClientFTP->GetAll();
}

while ( $coll->hasNext() ) {

    my $dbItem      = $coll->next();
    my $clientFtpID = $dbItem->client_ftp_id;

    # Show the client FTP settings.

    # If everything is working, the PII data in the database will encrypted (ending in NNNN)
    # but the corresponding value shown here will be not be encrypted.

    print join("  ",
        "clientFtpID(". $clientFtpID. ")",
        "FtpSite(". $dbItem->ftp_site. ")",
        "Username(". $dbItem->username. ")",
        "Password(". $dbItem->password. ")",
        "KeyID(". $dbItem->key_id . ")",
    ) . "\n";
}
