#!/usr/bin/perl -w

use strict;

use Hermes;
use Argos::Path;
use Argos::Conf;
use Argos::Ctrl;
use Vulcan::OptConf;

$| ++;

$Vulcan::OptConf::THIS = 'argos';

my $option = Vulcan::OptConf->load();
my %o = $option->get( qw( time=s pause=s exclude=s ) )->dump();

my $path = Argos::Path->new( $o{path} );
die "argos not set up.\n" unless $path->check();

=head1 SYNOPSIS

Control argos processes.

=cut
my $ctrl = $path->path( run => '.ctrl' );
exit 0 unless -f $ctrl || -l $ctrl;
$ctrl = Argos::Ctrl->new( $ctrl );

my $range = Hermes->new( $option->dump( 'range' ) );
my ( @info, @list ) = ( Argos::Conf->time( $o{time} || 0 ), operator() );

if ( @list = grep { $o{$_} } qw( pause exclude ) )
{
    @list = $range->load( $o{ $list[0] } )->list();
}

for my $name ( map { $range->load( $_ )->list() } @ARGV )
{
=head3 pause/resume

$0 name .. --pause watcher [ --time 20min ]

=cut
    if ( $o{pause} ) { map { $ctrl->pause( $_, @info ) } @list }

=head3 exclude/unexclude

$0 name .. --exclude nodes [ --time 20min ]

=cut
    elsif ( $o{exclude} ) { map { $ctrl->exclude( $_, @info ) } @list }

=head3 exclude/unexclude

$0 name .. --exclude nodes [ --time 20min ]

=cut
} 

my %stat;
$stat{paused} = $range->dump() if $range->load( $ctrl->stuck() )->list;
$stat{excluded} = $range->dump() if $range->load( $ctrl->excluded() )->list;

YAML::XS::DumpFile STDOUT \%stat if %stat;
exit 0;

sub operator
{
    #return unless my @p = qx( klist | grep Default );
    #return $p[0] =~ /: ([^@]+)@/ ? $1 : undef;
    my $name = `logname`; chop $name; return $name;
}
