#!/usr/bin/perl -w

use strict;

use Getopt::Std;
use File::Basename;

use lib '/app/tools/mediaserve/lib';
use MediaServe::Image::Processor;

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

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

if ( $opt{h} ) {
    usage();
}

my $matcher = new MediaServe::Image::Processor(
    client_id    => $opt{c},
    report_email => $opt{r},
    error_email  => $opt{x},
    quiet        => $opt{q},
    force        => $opt{f},
    verbose      => $opt{v},
    debug        => $opt{d}
);

$matcher->run();

sub usage {
    my $prog = basename $0;

    print <<EofUSAGE;

usage: $prog [-c client_id] [-r report_email_address] [-x error_email_address] [-l client_name/pattern] [-h] [-d]
            -c - Client ID (if not specified then run for all clients)
            -r - Send summary report to this email instead of STDOUT
            -x - Send error report to this email instead of STDERR
            -l - List client ids (host pattern is optional)
            -d - display debug information to STDERR
            -f - force import, ignore current data check
            -v - display verbose debug information to STDERR
            -q - Do not output summary report
            -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;
}
