#!/usr/bin/perl

#Master control script; ltp_master
#       3/12/02 William Jay Huie (creation)
#       3/28/02 William Jay Huie minor updates
#This will upload the ltprun script to the test system and kick off this script
#uses the Net:Telnet and Net::Ftp modules from www.cpan.org
#the ltprun script is a shell script to eliminate the need to write a script
#to install these modules on all the target systems

#FIXME: One problem is that the tests need to be run as root as as of now
#this means that LTP_USER needs to be root, which means root needs to be
#able to telnet and ftp into the machine. Couldn't figure out how to add
#a su -c to the LTP_CMD_LINE variable, ssh will probably be the solution
#but for now this works

use Net::Telnet ();
use Net::FTP ();

#Leave this set to keep perl from buffering IO
$| = 1;

#CHANGEME:
#change these for different defaults
#Think carefully because things like ltprun.out are relied upon in the ltprun
#     script, so you need to change them there too, or better yet make them
#     cmd line parms
#$LTP_USER = "ltp";
#$LTP_PASS = "ltp";
$LTP_USER = "root";
$LTP_PASS = "password";
$LTP_RUN = "ltprun";
#CHANGEME:
#Can't use ~/bin so have to explicitly hardcode this directory
$LTP_RUN_DIR = "/home/wjhuie/bin/";
$LTP_CMD_LINE = "chmod +x $LTP_RUN && nohup ./$LTP_RUN &> $LTP_RUN.out &";

#print "For this to work root must be able to ftp and telnet into the machines\n";
#print "Check #LTP_RUN_DIR/ltp_master for root password: $LTP_PASS\n";

#print "FIXME: Need to change this to su to root, not login over telnet\n";
#print "       Or run in some other way like ssh\n";

if ( $ARGV[0] eq "-f" )
{
   if ( -f $ARGV[1] )
   {
      open(FILE, $ARGV[1]) or die "Can't open $ARGV[1]";
      for ($i = 0; chomp($hosts[$i] = <FILE>); $i++) { ; }
      $#hosts--;
      close(FILE);
   }
   else { die "Please specify host list file with option -f\n"; }
}
elsif (@ARGV)
{  @hosts = @ARGV; }
else
{
   print "HOSTS separate with [ENTER] finish with [^D]\n";
   chomp(@hosts = <STDIN>);
}

$t = new Net::Telnet (Timeout => 30, Prompt => '/.+[\$#] $/'
#                     ,Dump_Log => "ltp_control.log"
#Remove the # on the line above and a copy of the
#       data exchange will be saved to the file 'ms.log'
                      );

#FIXME: Need a better retry system, this would loop forever
#$retry = 0;

for ($j = 0; $j <= $#hosts; $j++)
{
   chdir "$LTP_RUN_DIR" or die "Can't change to $LTP_RUN_DIR";
   print "\nAttempting to Upload $LTP_RUN to: $hosts[$j]";

   $ftp = Net::FTP->new($hosts[$j], Debug => 0) or
      do { print "\n\tCouldn't connect to: $hosts[$j] --- skipping\n"; next; };
   $ftp->login($LTP_USER, $LTP_PASS) or die "\nUnable to login";
   $ftp->type('I') or die "\nUnable to set type to Binary";
   $ftp->put($LTP_RUN) or die "\nUnable to put $LTP_RUN";
   $ftp->quit;
   print "\nUploading $LTP_RUN Done";

   print "\nConnecting to: $hosts[$j]";
   $t->open($hosts[$j]);
   $t->errmode("return");
   $t->login($LTP_USER, $LTP_PASS) or
                 do { print "\n\tFailed Login to $hosts[$j]! "; next;
#perhaps work in some sort of retry, but this way will loop forever
#                      if ($retry) { $j--; } next;
                    };
   @l = $t->cmd("$LTP_CMD_LINE");

   if ($j == 0)
   {
      if ($l[0]) { print " >\n@l"; @base_output = @l; }
      else { printf "\nReturned empty Buffer!\n"; }
   }
   $a = join("",@l);
   $b = join("",@base_output);
   if ($a ne $b)
   { print "!>\n@l\n"; }

   $t->buffer_empty();
   $t->close();
   print "\t Command Completed";
}
print "\nAll Done!\n";

#Use snippit below in results script to convert timing info
#     from tar file name into human readable format
#$foo = $ARGV[0];
#($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($foo);
#print "$foo\n";
#print "Hour: $hour Min: $min Sec: $sec\n";