#!/usr/bin/env perl

#######################################################################
## Copyright (c) 2002-2004 by EqualLogic, Inc.
##
## All rights reserved.  This software may not be copied, disclosed,
## transferred, or used except in accordance with a license granted
## by EqualLogic, Inc.  This software embodies proprietary information
## and trade secrets of EqualLogic, Inc.
#######################################################################


################################################
###
###                Main program
###
################################################

use strict;

# this will store the directory containing the script into $Bin
use FindBin qw($Bin);

# this will add the directory you need to 
# @INC (list of library directories)
use lib ("$Bin");

use Getopt::Std;
use EqlScript;
##use Term::ReadPassword;

sub usage()
{
    my ($progname);
    $progname = $0;

    if ( $progname =~ /\/(.*)$/ )
    {
	$progname = $1;
    }

    print << "EOF";

usage: $progname [-?h] [-g group ] [-a account] [-p password ] -f cli_file | cli_command

 -? | h      : Describes script usage.
 -g group    : Group IP address or name.
 -a account  : Group account (default grpadmin)
 -p password : Group account password.  If no password is supplied, the script will
               prompt for a password.
 -f cli_file : Full pathname to a file containing a sequence of one or more
               CLI commands for execution.  The File must contain one
               command per line.
 cli_command : CLI command.  See the CLI Reference manual for a description
               of the CLI commands.

example: $progname -g psg_group -a grpadmin -p my_password grpparams show
example: $progname -g psg_group -a grpadmin -p my_password -f cmds.txt
example: echo "pass" | $progname -g psg_group -a grpadmin grpparams show

EOF
}

my ($debug, $numArgs, $host, $user, $pass, $script, @cmd, $cmd, $remote, @result, $retval);

getopts('?hdg:a:p:f:', \my %opt) or usage() and exit 1;

usage() and exit 0 if $opt{'?'} or $opt{'h'};
usage() and exit 1 unless $opt{g};

if ( $opt{a} eq 'root' )
{
    printf("Error: username cannot be root\n");
    exit 1;
}

$pass = undef;

$host = $opt{g} if $opt{g};

#
# default group name is "grpadmin"
#
$user = "grpadmin";
$user = $opt{a} if $opt{a};

$pass = $opt{p} if $opt{p};

$script = $opt{f} if $opt{f};

$debug = $opt{d} if $opt{d};

printf("debug mode\n") if $debug;

$cmd = join(" ", @ARGV);

usage() and exit 1 if ( !$script && !$cmd );

if ( !defined $pass )
{
    my $rin = '';
    vec($rin, fileno(STDIN), 1) = 1;

    my $nfound = select($rin, undef, undef, 0);

    if ( $nfound > 0 )
    {
	printf("reading password from STDIN\n") if ${debug};
	${pass} = <STDIN>;
	chop ${pass};
    }
}

if ( !defined $pass )
{
    printf("Please enter a password: ");
    ${pass} = <STDIN>;

##    $pass = read_password("Please enter a password: ");
##    if ( !defined $pass )
##    {
##	$pass = "";
##    }

    ${pass} =~ s/^\s+//;
    ${pass} =~ s/\s+$//;

    printf("password entered was '%s'\n", $pass) if ${debug};
}

printf("host = $host, user = $user, pass = $pass\n") if ${debug};

#
# login to the PSS
#

$remote = EqlScript->new();
if ( !$remote )
{
    printf("Error: Failed to create EqlScript object\n");
    exit 1;
}

#
# set the command/response timeout to 10 seconds
#

$remote->timeout(10);

#
# log into the pss
#

printf("Connecting to $host...\n") if $debug;

$remote->open( $host ) or printf("Error: Failed to connect to $host\n") and exit 1;

printf("Logging in with $user:$pass...\n") if $debug;

$remote->login( $user, $pass ) or printf("Error: Failed to login to $host\n") and exit 1;

printf("Logged in\n") if $debug;

$retval = 0;

#
# if we are executing a script, read each line and execute that
# command.  otherwise, just execute the supplied command.
#

if ( $script )
{
    open(SCRIPT, "< $script") or printf("Error: failed to open $script: $!") and exit 1;
    while ( <SCRIPT> )
    {
	chop;
	$cmd = $_;
	printf("\n#############################################\n") if $debug;
	printf("###   Executing '%s'\n", $cmd) if $debug;
	printf("#############################################\n") if $debug;

	@result = $remote->cmd( $cmd );
	if ( $remote->err )
	{
	    print $remote->errmsg;
	    last;
	}
	else
	{
	    print @result;
	}
    }
}
else
{
    printf("Executing '%s'\n", $cmd) if $debug;
    @result = $remote->cmd( $cmd );
    if ( $remote->err )
    {
	print $remote->errmsg;
    }
    else
    {
	print @result;
    }
}

exit $retval;
