#!/usr/bin/perl
use strict;


use Data::Dumper;
use Getopt::Std;
use POSIX qw(ceil);
use POSIX ":sys_wait_h";
use Sys::Hostname;
use Carp;


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


use lib '/app/tools/rps/lib';
use RPS::DB::Item::CAMechanicalRun;
use RPS::DB::Item::MechanicalRun;
use RPS::DB::Item::UKMechanicalRun;

my $gVerbosityLevel = 1;



# Parse the command-line options.
#
my %options;
_parseCommandLine(\%options);

my @clientIDs;
if ($options{clientID})
{
    push @clientIDs, $options{clientID};
}
else
{
    @clientIDs = _getLocalClientIDs();
}

foreach my $clientID (@clientIDs)
{

    # Instantiate the application singleton object.
    #
    my $appSingleton = Common::RSApp->new(clientID => $clientID);
    
    _report("\n---- Updating client $clientID");
    
    # First fix the US mech runs
    my $usRuns = RPS::DB::Item::MechanicalRun->GetAll();
    while (my $usRun = $usRuns->next())
    {
        _updateLabel($usRun);
    }
    
    # Now fix the CA mech runs
    my $caRuns = RPS::DB::Item::CAMechanicalRun->GetAll();
    while (my $caRun = $caRuns->next())
    {
        _updateLabel($caRun);
    }    
    
    # And lastly the UK mech runs, which have a slightly different label format.
    my $ukRuns = RPS::DB::Item::UKMechanicalRun->GetAll();
    while (my $ukRun = $ukRuns->next())
    {
        _updateUKLabel($ukRun);
    }      

}


sub _updateLabel
{
    my ($run) = @_;
    
    # Grab and localize the dates.
    my $startDate = Common::Client::Current()->Locale()->formatDate($run->start_date);
    my $endDate = Common::Client::Current()->Locale()->formatDate($run->end_date);

    my $label = $startDate . ' - ' . $endDate;

    $run->label($label);
    $run->save();
}


sub _updateUKLabel
{
    my ($run) = @_;
    
    my $quarter = $run->quarter;
    my $year = $run->year;

    my $label = 'Q' . $quarter . ' ' . $year;

    $run->label($label);
    $run->save();
}



sub _usage
{
    print "\nusage: $0 -c client_id [-V]\n";
    print "\n";
    print "Arguments:\n";
    print "\t-c <client_id>\t\tThe client_id of the client to process\n";
    print "\t-V <N>\t\t\tVerbosity level - 0 means no output\n";
}


sub _report
{
    my ($string, $verbosity) = @_;
    $verbosity = 1 unless defined $verbosity;

    if ($gVerbosityLevel >= $verbosity)
    {
        print $string . "\n";
    }
}


# Pass dates in 'yyyy-mm-dd' format.
#
sub _parseCommandLine
{
    my ($settings) = @_;

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

    $settings->{clientID} = $opt{c} if $opt{c};

    if (defined $opt{V})
    {
        $gVerbosityLevel = $opt{V};
    }
}

sub _getLocalClientIDs
{
    # We have a nifty little script that will give us a list of client databases.
    #
    my $clientListString = `/app/tools/rps/bin/db/list_client_dbs.pl`;
    chomp $clientListString;


    # Find the client id that matches each database.
    #
    my @ids;
    my @dbNames = split(/ /, $clientListString);
    foreach my $dbName (@dbNames)
    {
        my $id = Common::RSDB::DBNameToClientID($dbName);
        push @ids, $id;

    }

    return @ids;
}

