#!/usr/bin/env perl

use strict;
use warnings;

# fix lib paths, some may be relative
BEGIN {
    require File::Spec;
    my @libs = (
        "lib", 
        "local/lib",
        "../libcif/lib", # in case we're in -dev mode
    );
    my $bin_path;

    for my $lib (@libs) {
        unless ( File::Spec->file_name_is_absolute($lib) ) {
            unless ($bin_path) {
                if ( File::Spec->file_name_is_absolute(__FILE__) ) {
                    $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
                }
                else {
                    require FindBin;
                    no warnings "once";
                    $bin_path = $FindBin::Bin;
                }
            }
            $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
        }
        unshift @INC, $lib;
    }
}

use Cikl::Client::Factory;
use Getopt::Std;
use Cikl qw/debug/;

my %opts;
getopt('C:', \%opts);

our $debug       = ($opts{'d'}) ? 1 : 0;

# config opts
my $config_file      = $opts{'C'} || $ENV{'HOME'}.'/.cikl';

die usage() if($opts{'h'});

sub usage {
    return <<EOF;
Usage: perl $0

Standard Options:
    -h  --help:             this message
    -C  --config:           specify cofiguration file, default: $config_file
    
EOF
}

my $config = Config::Simple->new($config_file) or die("Failed to open config file $config_file: " . $!);
my $client_config = $config->get_block('client');
my $cli = Cikl::Client::Factory->instantiate($client_config);

print "Waiting for responses....\n";
my $responses = $cli->ping();
$cli->shutdown();
print "\n";

my $count = 0;
foreach my $hostinfo (@$responses) {
  $count += 1;
  print "#$count " . $hostinfo->to_string() . "\n";
}
print "-------------\n";
print "$count responses total\n";

