#!/usr/bin/perl -w

use strict;

use lib '/app/tools/rps/lib';
use RPS::Catalog::MatchAsset;

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

use Getopt::Std;
use File::Basename;
use Data::Dumper;

my %opt;
usage() unless getopts( 'r:x:c:dlh', \%opt );

if ( $opt{l} ) {
    list_clients( $ARGV[0] );
}

my $matcher = new RPS::Catalog::MatchAsset(
    client_id    => $opt{c},
    debug        => $opt{d} ? 1 : undef,
    report_email => $opt{r},
    error_email  => $opt{x}
);

$matcher->run();

sub usage {
    my $prog = basename $0;

    print <<EofUSAGE;

usage: $prog [-l <client_pattern>] [-r report_email_address] [-x error_email_address] [-h] [-d]
            -r - send summary matching reports to this email address
            -x - send error output to this email address
            -l - list all clients, client_pattern is optional
            -d - verbose output
            -h - display this screen
EofUSAGE

    exit 1;
}

sub list_clients {
    my $host = shift;
    my $key;
    foreach $key ( sort { $a <=> $b } keys(%Common::RSDB::CLIENT_DB) ) {
        printf( "%-20s %-10s ID: %s\n", $Common::RSDB::CLIENT_DB{$key}->{db_name}, "($Common::RSDB::CLIENT_DB{$key}->{host_id})", $key )
          if ( !defined($host) || $Common::RSDB::CLIENT_DB{$key}->{db_name} =~ /$host/i );
    }
    exit 1;
}
