#!/usr/bin/perl

use strict;
use warnings;

use FindBin qw($RealBin);
use lib "$RealBin/../lib";

use Config::General;
use Getopt::Long;
use Log::Log4perl qw(:easy);

use perfSONAR_PS::MeshConfig::Utils qw(load_mesh);

Log::Log4perl->easy_init($DEBUG);

my $CONFIG_URL;
my $LOGGER_CONF;
my $DEBUGFLAG;
my $HELP;

my ( $status, $res );

$status = GetOptions(
    'config_url=s'  => \$CONFIG_URL,
    'logger=s'      => \$LOGGER_CONF,
    'verbose'       => \$DEBUGFLAG,
    'help'          => \$HELP
);

my $logger;
unless ( $LOGGER_CONF ) {
    use Log::Log4perl qw(:easy);

    my $output_level = $INFO;
    if ( $DEBUGFLAG ) {
        $output_level = $DEBUG;
    }

    my %logger_opts = (
        level  => $output_level,
        layout => '%d (%P) %p> %F{1}:%L %M - %m%n',
    );

    Log::Log4perl->easy_init( \%logger_opts );
}
else {
    use Log::Log4perl qw(get_logger :levels);

    Log::Log4perl->init( $LOGGER_CONF );
}

$logger = get_logger( "perfSONAR_PS" );
$logger->level( $DEBUG ) if $DEBUGFLAG;

unless ($CONFIG_URL) {
    print "Need to specify a url to load the mesh to test from\n";
    exit (-1);
}

($status, $res) = load_mesh({ configuration_url => $CONFIG_URL });
if ($status != 0) {
    print "Problem loading configuration: $res\n";
    exit (-1);
}

foreach my $mesh (@$res) {
    $mesh->validate_mesh();
}
