#!/usr/bin/env perl

use strict;
use warnings;

use YAML::XS;
use File::Spec;

use MYDan::Util::ExpSSH;
use MYDan::Util::OptConf;
use MYDan::Util::Go;

$| ++;

$MYDan::Util::OptConf::THIS = 'util';

=head1 SYNOPSIS

 $0 host [user]
 $0 --user dan host [user]

=cut

my $option = MYDan::Util::OptConf->load();
my %o = $option->get( qw( user=s ) )->dump();

my ( $go, $pass ) = map{ -f "$o{conf}/$_.private" ? "$_.private" : $_ }qw( go pass );

if( ! @ARGV || $ARGV[0] =~ s/^\/// )
{
    $option->assert() unless "$o{conf}/$go" && -e "$o{conf}/$go";
    MYDan::Util::Go->new( "$o{conf}/$go" )->go( $ARGV[0] );
    exit;
}

die "cannot resolve host\n" unless MYDan::Util::ExpSSH->new()
    ->conn( pass => "$o{conf}/$pass", user => $o{user}, host => shift, sudo => shift );