#!/usr/bin/perl

use strict;
use File::Path;
use File::Copy qw(copy);

$| = 1;

use Getopt::Long;
use Data::Dumper;
use File::Path;

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

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Payor;

my %args = ();
parseCommandLine( \%args );

my @clientIDs;
if ( $args{clientID} ) {
    push @clientIDs, $args{clientID};
} else {
    # For production
    @clientIDs = Common::RSDB::GetAllRPSClientIDs();

    # For dev/staging
    #@clientIDs = Common::RSDB::GetLocalRPSClientIDs();
}

my $logoPath = "/app/tools/common/production/images/logos/";

# Loop over all the RPS clients
foreach my $clientID (@clientIDs) {
    print STDERR "processing client ID $clientID\n";
    my $appSingleton = Common::RSApp->new( clientID => $clientID );
    my $dbo          = Common::RSApp::GetClientDB();
    my $dbh          = $dbo->DBH;

    my $cdbo = Common::RSApp::GetCommonDB();
    my $cdbh = $cdbo->DBH;
    
    # We're looking for the existing site logo.
    # It could be in a few places, so we'll check in this order:
    # 1. client.client_logo in RSCOMMON
    # 2. png in common/production/images/logos
    # 3. gif in common/production/images/logos
    my $logo;
    my $ext = "png";
    my $payorLogoPath = "/app/tools/common/production/images/payor_logos/" . $clientID . "/";
    
    my $client = Common::DB::Item::Client->Lookup(client_id => $clientID);
    my $altLogo = $client->client_logo;
    if ($altLogo) {
        $logo = $logoPath . $altLogo;
    }
    
    if (!$logo) {
        my $clientNameClean = $client->client_name_clean;
        my $pngLogo = $logoPath . $clientNameClean . ".png";
        my $gifLogo = $logoPath . $clientNameClean . ".gif";
        
        if (-e $pngLogo) {
            $logo = $pngLogo;
        } elsif (-e $gifLogo) {
            $logo = $gifLogo;
            $ext = "gif";
        }
    }
    
    if (!$logo) {
        print STDERR "!!! No logo found for client ID $clientID\n";
    } else {
        # Okay, we have a logo. 
        # Let's set up the directory for this client.
        if ( !-d $payorLogoPath ) {
            mkpath($payorLogoPath) or die "ERROR: Unable to create path $payorLogoPath: $!\n";
        }        
            
        # And finally we need to make a copy of it for each payor.
        my $payors = RPS::DB::Item::Payor->GetAll();
        while ( my $payor = $payors->next() ) {
            my $payorID = $payor->payor_id;        
            copy($logo,$payorLogoPath . $payorID . "." . $ext) or die "Failed to copy $logo: $!\n";       
        }        
    }        

}

#==============
#
# Subroutines
#
#==============

sub parseCommandLine {
    my ($a) = @_;
    my $clientID;
    my $labelContractID;
    GetOptions( 'c|client_id=i' => \$clientID, );
    ${ _ [0] }{clientID} = $clientID;

}    #parseCommandLine

sub usage {
    my $errstr = shift;
    my $text = ($errstr) ? "ERROR: $errstr\n" : '';
    $text .= "Usage $0 -c <clientID>>\n";
    return $text;
}

1;
