#!/usr/bin/perl
# This script finalizes a data import by doing the following:
#   - Scan the logfile (typically 'debug.txt') file for import exceptions.
#   - Create a DataImportFile object for the specified exceptions filename.
#
# Usage:
#   ./post_import.pl -c clientID -f filename -i importID -l logfile
#
# where
#  -c  is the client identifier
#  -i  specifies the import identifier for the DataImport node representing the import.
#  -f  is the name of the exceptions file
#  -l  is the name of the import logfile
#
use warnings;
use strict;
use Getopt::Long;
use Data::Dumper;
use Cwd;

use lib '/app/tools/support/lib';
use Support::DB::Item::DataImport;
use Support::DB::Item::DataImportFile;

use lib '/app/tools/common/lib';
use Common::RSApp;

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

print "D:post_import.pl: args = ". Dumper(\%args) . "\n";

my $filename  = $args{file_name};
my $clientID  = $args{client_id};
my $importID  = $args{import_id};
my $logfile   = $args{logfile};


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

my $importObj;
my $inputFile;
if ( $importID ) {
    $importObj = Support::DB::Item::DataImport->Lookup( import_id => $importID );
    die("Invalid importID !!!") if ( !$importObj );

    $inputFile = Support::DB::Item::DataImportFile->Lookup(
        import_id => $importID,
        file_type => Support::DB::Item::DataImportFile::kFileTypeTemplate
    );
    die("No import template DataFile found for importID $importID !!!") if ( !$inputFile );
}

my $exString = `tail -100 $logfile | grep "Total Exceptions"`;
chomp $exString;
my($totalExceptions) = $exString =~ /Total Exceptions\s+(\d+)/;

my $rowsString = `tail -100 $logfile | grep "Total Rows"`;
chomp $rowsString;
my($totalRows) = $rowsString =~ /Total Rows\s+(\d+)/;

if ( $importObj ) {

    my %args = (
        import_id        => $importID,
        file_name        => "$filename",
        file_type        => Support::DB::Item::DataImportFile::kFileTypeExceptions,
        status           => Support::DB::Item::DataImportFile::kFileStatusImported,
        total_exceptions => $totalExceptions,
        records          => $totalRows,
    );

    my $exceptionsFile = Support::DB::Item::DataImportFile->Create( %args );
    $exceptionsFile->save();

    # Flag the original input template as imported
    $inputFile->status( Support::DB::Item::DataImportFile::kFileStatusImported );
    $inputFile->save();
}




#====================
# --- Subroutines ---
#====================
sub parseCommandLine {
    my( $a ) = @_;
    my $clientID;
    my $filename;
    my $importID;
    my $logfile;

    if ( ! GetOptions(
        'c=i' => \$clientID,
        'f=s' => \$filename,
        'l=s' => \$logfile,
        'i=i' => \$importID,
    )) {
        die("An error has occurred while parsing arguments, aborting\n");
    }

    $a->{client_id} = $clientID;
    $a->{file_name} = $filename;
    $a->{import_id} = $importID;
    $a->{logfile}   = $logfile;
}

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