#!/usr/bin/perl  
#
# This script displays a list of partitions, opening a virtual
#   terminal on the one selected.
#
# If more than one managed systems exists, a list of them is
#   displayed first.  After the user selects of a managed system, 
#   a list of all logical partitions on that managed system is 
#   displayed.
#
#djm0203

# MAIN
#

# Bail out if  commands do not exist
if (! -e "/opt/hsc/bin/lssyscfg" ) {
   print("\n\n Cannot find commands. This script requires");
   print(    " commands such as \"lssyscfg\" and \"mkvterm\".\n\n");
   exit;
}
# Get Name of Managed System
print("\n\n\n\n Retrieving name of managed system(s) . . . ");

$cmd = "/opt/hsc/bin/lssyscfg -r sys  -Fname";
open(CECS,"$cmd |");
$i=1;
while ($cec=<CECS>) {
   $ManagedSystems[$i]=$cec;
   chomp($ManagedSystems[$i]);
   print("$ManagedSystems[$i]   ");
   $i++;
}

$numManagedSystems = @ManagedSystems-1; #subtract 1 since started array index at 1, not 0

system('clear');
LISTCECS:
# If more than 1 managed system, display partition list and prompt.
if ( $numManagedSystems == 1 ) {
   $ManagedSystem = $ManagedSystems[1];  #Remember, did not start at 0
} else {
   cec_menu(@ManagedSystems);
   print("\n\n Enter Number of Managed System.   (q to quit): ");
   $cec_number_entered = <STDIN>;
   chomp($cec_number_entered);

   if (  $cec_number_entered eq "q" ) {
      print("\n\n Bye.\n\n");
      exit;

   } elsif ( ($cec_number_entered > $numManagedSystems) || ($cec_number_entered < 1) ) {
      system('clear');
      print("\n\n    Invalid Managed System Number.  Enter a Number Between 1 and ${numManagedSystems}");
      goto LISTCECS;
   }else {
   $ManagedSystem = $ManagedSystems[$cec_number_entered];
   }
}

close(CECS);

# Get List of Partitions in Managed System
$cmd="LANG=en_US /opt/hsc/bin/lssyscfg -m \"$ManagedSystem\" -r lpar  -Fname:state:lpar_env  | grep -v os400";
# Load Array With Partition Names
open(PARTITION_LIST,"$cmd |") || print("\n\n  ERROR:  Cannot run $cmd \n");
undef @partitions;
undef @states;

$i=1;  #Start array index at 1
while ($line=<PARTITION_LIST>) {
   chomp($line);
   $line =~ m/(.+):(.+):(.+)/;
   if($line eq "No results were found."){
       system('clear');
       print("\n\nNo AIX or Linux partitions found for $ManagedSystem \n\n");
       goto LISTCECS;      
       
   } 

   $partitions[$i] = $1;
   $states[$i] = $2;
   #print("$states[$i]\n");
   # If state is returned as a number, map to appropriate text.
   #if ($states[$i] == 1) {
   #   $states[$i] = "Ready";
   #} elsif ($states[$i] == 5) {
   #   $states[$i] = "Starting";
   #} elsif ($states[$i] == 8) {
   #   $states[$i] = "Running";
   #} elsif ($states[$i] == 9) {
   #   $states[$i] = "Open Firmware";
   #}
   $i++;
}
$number_of_partitions=@partitions-1; #Subtract 1 since index 0 not used
close(PARTITION_LIST);

system('clear');
# Poll user for partition number
while (TRUE) {
   # Display list of partitions and prompt.
   partition_menu(\@partitions,\@states);
   print("\n\n Enter Number of Running Partition (q to quit): ");
   $number_entered = <STDIN>;
   # Does user want to bail?
   chomp($number_entered);
   print($number_entered);
   if (  "$number_entered" eq "q" ) {
      if ( $numManagedSystems == 1  ) {
                 exit 0;
      } else {
           system('clear');
           goto LISTCECS;
      }
   } elsif ( ($number_entered > $number_of_partitions) || ($number_entered < 1) ) {
      # Did user enter an invalid partition number?
      system('clear');
      print("\n\n    Invalid Partition Number.  Enter a Number Between 1 and ${number_of_partitions}");
      print("\n\n    Hit <Enter> To Continue, q to quit: ");
      $response = <STDIN>;
      chomp($response);
      if ( $response eq "q" ) {
         exit 0;
      }
   } else {
      # Everything's cool...open a vterm
      open_vterm($partitions[$number_entered]);
      print("\n\n    Hit <Enter> To Continue, q to quit: ");
      $response = <STDIN>;
      chomp($response);
      if ( $response eq "q" ) {
           if ( $numManagedSystems == 1  ) {
                 last;
            } else {
                  goto LISTCECS;
            }
      }
   }
}
print("\n\n Bye.\n\n");
# END MAIN


# Open virtual terminal on partition name passed in
sub open_vterm {
   my ($partition_name) = $_[0];

   $cmd = "/opt/hsc/bin/mkvterm -m \"$ManagedSystem\" -p \"$partition_name\"";
   print("\n    Opening Virtual Terminal On Partition $partition_name . . .\n\n");
   $rc = system($cmd);
} 
# List partition names (of array passed in).  
sub partition_menu {
   my ($p_list_p) = $_[0]; 
   my ($s_list_p) = $_[1]; 
   my (@p_list);
   my (@s_list);
   my ($i,$j,$tmp);
   my ($num_partitions);
   @p_list = @$p_list_p;
   @s_list = @$s_list_p;

   format PARTITION_LIST =
   @<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<<
   $tmp, $p_list[$i],                         $s_list[$i]
.
   $~ = "PARTITION_LIST";

   print("\n\n ----------------------------------------------------------");
   print(  "\n  Partitions On Managed System:  $ManagedSystem");
   print(  "\n  OS/400 Partitions not listed");
   print(  "\n ----------------------------------------------------------\n");
   $num_partitions = @p_list-1;  # Don't use subscript 0 so subtract 1
   # List out partition names and states.
   for ($i=1; $i<=$num_partitions; $i++) {
      $tmp="$i" . ")";
      write();
   }
}
# List names of managed systems 
sub cec_menu {
   my (@c_list) = @_;
   my ($i);
   my ($num_cecs);
   
   $num_cecs = @c_list-1;  # Don't use subscript 0 so subtract 1
   print("\n\n ----------------------------------------------------------");
   print(  "\n  Managed Systems:");
   print(  "\n ----------------------------------------------------------");
   for ($i=1; $i<=$num_cecs; $i++) {
      print("\n   ${i}) $c_list[$i]");
   }
}


