#!/usr/bin/perl -w

#
# "SystemImager" 
#
#  Copyright (C) 2008 Thomas Krause 
#
#  $Id: si_power 4255 2008-04-29 22:11:00Z tkrause $
#

# set some variables
$VERSION="SYSTEMIMAGER_VERSION_STRING";
my $program_name="si_power";
my $get_help = "  Try \"$program_name --help\" for more options.";

# declare modules
use lib "/usr/lib/systemimager/perl";
use strict;
use Net::Ping;
use Net::SSH qw(ssh);
use Getopt::Long;
use SystemImager::Options;
use SystemImager::HostRange;

use vars qw($config $VERSION);

# set version information
my $version_info = <<"EOF";
$program_name (part of SystemImager) version $VERSION

EOF

$version_info .= SystemImager::Options->copyright();

# Help stuff
my $help_info = <<"EOF";
usage $program_name [--query|--restart|--off|--on] --clients <clients>

EOF

GetOptions( 
    "help"              => \my $help,
    "version"           => \my $version,
    "clients=s"         => \my $clients,
    "on"                => \my $on,
    "off"               => \my $off,
    "restart"           => \my $restart,
    "test"           => \my $test,
    "query"             => \my $query
) or die qq($help_info);

# if requested, print help information
if($help) {
    print qq($help_info);
    exit 0;
}

# if requested, print version and copyright information
if($version) {
    print qq($version_info);
    exit 0;
}

unless($clients) {
    print qq(FATAL: Please specify one or more clients with --clients.\n);
    print qq($get_help\n);
    exit 1;
}

unless( ($on) or ($off) or ($query) or ($restart) or ($test) ) {
    print qq(FATAL: Please specify --on, --off, --restart or --query.\n);
    print qq($get_help\n);
    exit 1;
}

# Make array from --clients "hostnames and ip addresses" -BEF-
my @array = SystemImager::HostRange::expand_groups($clients);

if($on) {
    print qq("This command is not supported yet!");
    exit 0;
}
elsif($off)
{
    run_remote_command("init 0", @array);
}
elsif($test)
{
    run_remote_command("hostname", @array);
}
elsif($restart)
{
    run_remote_command("init 6", @array);
}
elsif($query)
{
    show_status(@array);
}


################################################################################
#
#   Subroutines
#
################################################################################


################################################################################
#
# Description:
# Pings each host in the list and returns the clients and the status ("ON"|"OFF")
# in a hashtable.
#
# Usage:
# show_status(@clients);
sub show_status {
    my @clients = @_;
    
    my $p = Net::Ping->new(); #timeout = 1s
    foreach my $client (@clients) {
        my $ping_result = $p->ping($client, 1);
        
        if($ping_result) { print "$client ON\n"; }
        elsif(defined($ping_result))  { print "$client OFF\n"; }
        else { print "$client ERR\n"; } #hostname not found or error in ip address
    }
    $p->close();
}

################################################################################
#
# Description:
# Run $command on all $clients.
#
# Usage:
# run_remote_command($command, @clients);
sub run_remote_command {
    my ($command, @clients) = @_;

    my $p = Net::Ping->new();
    foreach my $client (@clients) {
        if($p->ping($client, 1)) {
            ssh('root@' . $client, $command);
        }
    }
    $p->close();
}


