#!/var/opt/STORtools/bin/perl

require  "subroutines.pm";
&st_globals();

$PROGNAME = "cdmenu";
$TMP = "/tmp/cdmenu.tmp";
####################################################################
#
# look for the specified socal, sf, ses, or array
# photon array
# look for photon array by box name
# get all entries for that box name and put into an array
#
# Copyright (c) 1998  Sun Microsystems Computer Corporation, Inc.
####################################################################
$VERSION = "Alpha 1";
####################################################################
# inputs:
#   component   socal, sf, array
#   comp_name   
#

#$DEBUG = "TRUE";

&check_root_access( $PROGNAME );

$SIG{INT} = 'int';

# process the inputs
&proc_cli;

&get_rawdump;
if ($live eq "") {
    $golden_snapshot = &get_golden_snapshot;
    &make_port_list("$LOGDIR/$golden_snapshot");
} else {
    &make_port_list;
}

    foreach $e (@LD_DD_PORTS) 
    {
        if (!grep { /$SOCNUM{$e}/ } @socal_list) 
	{
            push @socal_list, $SOCNUM{$e};
        }
        push @sf_list, $SFNUM{$e};
    }

#    foreach $e (@LD_DD_PORTS) {
#        push @sf_list, $SFNUM{$e};
#    }

    foreach $line (@goldraw) {
        (@line) = split(' ', $line);
        if ($line =~ /^(\S+),[AB][01]/) {
            $tmp = $1;
            $a1 = grep {/$tmp/} @a5k_list;
            if (! $a1) {
                push @a5k_list, $tmp;
            }
        }
    }

    &pr_config_summary;

####################################################################
#    SUBROUTINES
#
####################################################################
sub int {
    system "/usr/bin/rm -f $TMP_RAW";
    exit 2;
}

####################################################################
# print out a summary of the fibre channel components    
####################################################################
sub pr_config_summary {

    my $comp_choice;
D_SBUS_SUM:
    if ($DISP_ONLY ne "TRUE") {
        system_clear();
        &st_header();
        if ($live) {
            print "Menu: Display Configuration Summary (live)\n";
            print "------------------------------------------\n\n";
        } else {
            print "Menu: Display Configuration Summary\n";
            print "-----------------------------------\n\n";
        }
    } else {
        print "(Online Components Only)\n\n";
        &cd_display_sbus_summary;

        if ($DISP_ONLY eq "TRUE") {
            exit;
        }
    }
    print "View more details by selecting a component number [x] or quit:\n";
        print " [1] Display Configuration Summary\n";
        print " [2] Display Loop Map Summary\n";
        print " [3] Host Adapter #\n";
        print " [4] Port #\n";
        print " [5] A5K Enclosure\n";
        print " [6] LUN/Disk Drive List\n";
        print " [7] LUN/Disk Inquiry\n";
        print " [8] Dump Raw Data \n";
        print " [9] Cluster Check\n";
    if ($live eq "-live") {
        print " [s] Save Configuration \n";
    }
    print "\n";
    print " [q] Quit\n";
    print "\nEnter Choice: ";
    $comp_choice = &get_response;
    return if ($comp_choice =~ /^q/i);
    if ( $comp_choice eq 1 ) {
        open MORE, "| more";
        select MORE;
        system_clear();
        &st_header();
        print "Menu: Display Configuration, Config Summary\n";
        print "-------------------------------------------\n";
        print "\n(Online Components Only)\n\n";
        &cd_display_sbus_summary;
        select STDOUT;
        close MORE;
        printf("\nPress <return> to continue... ");
        &get_response;

    } elsif ( $comp_choice eq 2 ) {
         system_clear();
         &st_header();
         print "Menu: Display Configuration, Array Map\n";
         print "--------------------------------------\n\n";

         if ($live eq "-live") {
             `${BINDIR}/storconfig -map > /tmp/current_map`;
             system("/usr/bin/more /tmp/current_map");
         } else {
             open MORE, "| more";
             select MORE;
             seek GOLDRAW, 0, 0;
             $found_section = "FALSE";
             while ($line=<GOLDRAW>) {
                 if ($line =~ /^LOOP SUMMARY:/) {
                     $found_section = "TRUE";
                     next;
                 }
                 next if ($found_section eq "FALSE");
                 last if ($line =~ /^============/);
                 print "$line"; 
             }
             select STDOUT;
             close MORE;
         }
         printf("\nPress <return> to continue... ");
         &get_response;

    } elsif ($comp_choice eq 3) {
        $loop = 1;
        while ($loop ) {
            system_clear();
            &st_header();
            print "Menu: Display Configuration, Host Adapters\n";
            print "------------------------------------------\n\n";

            $usr_ch = &make_arr_menu(sort(@socal_list));
	    # if the returned string doesn't start with q(quit) 
	    # or if the returned string starts with ql(qlc) 
	    # then go ahead and call sfdmp with the returned string 
            if (($usr_ch !~ /^q/i) || ($usr_ch =~ /^ql/i)){
                `/usr/bin/rm -f $TMP`;

                `${BINDIR}/sfdmp -name ${usr_ch} ${dmp_arg} > $TMP`;
                system "/usr/bin/more $TMP";
                `/usr/bin/rm -f $TMP`;
            } elsif ($usr_ch =~ /^q/i) {
                $loop = 0;
                next;
            } else {
                printf("Undefined option\n");
                printf("\nPress <return> to continue... ");
                &get_response;
            }


            printf("\nPress <return> to continue... ");
            &get_response;
        }
    } elsif ( $comp_choice eq 4 ) {
        $loop = 1;
        while($loop) {
            system_clear();
            &st_header();
            print "Menu: Display Configuration, Port(s)\n";
            print "---------------------------------------\n\n";
            foreach $sf_comp (@sf_list) {
                &proc_sf($sf_comp);
            }
            $usr_ch = &make_arr_menu(@sf_list);
            if ($usr_ch !~ /^q/i) {
                `/usr/bin/rm -f $TMP`;
                `${BINDIR}/sfdmp -name ${usr_ch} ${dmp_arg} > $TMP`;
                system "/usr/bin/more $TMP";
                system "/usr/bin/rm -f $TMP";
                print "\nPress <return> to continue...";
                &get_response;
            } elsif ($usr_ch =~ /^q/i) {
                $loop = 0;
                next;
            } else {
                printf("Undefined option\n");
                printf("\nPress <return> to continue... ");
                &get_response;
            }
        }
    } elsif ( $comp_choice eq 5 ) {
        $contin = 1;
        while($contin) {
            system_clear();
            &st_header();
            print "Menu: Display Configuration, Enclosure(s)\n";
            print "-----------------------------------------\n\n";
            #foreach $a5k_comp (@a5k_list) {
                #print "$a5k_comp\n";
            #}       
            $usr_ch = &make_arr_menu(@a5k_list);
            if ($usr_ch !~ /^q/i) {
        
                `/usr/bin/rm -f $TMP`;
                $result = `${BINDIR}/araydmp -name ${usr_ch} ${dmp_arg} > $TMP`;
                system "/usr/bin/more $TMP";
                system "/usr/bin/rm -f $TMP";
                print "\nPress <return> to continue...";
                &get_response;
            } else {
                $contin = 0;
            }
        }
    } elsif ($comp_choice eq 6) {
        system_clear();
        &st_header();
        print "Menu: Display Configuration, LUN/Disk Drive List\n";
        print "------------------------------------------------\n\n";
        if ($live) {
          `/usr/bin/rm -f $TMP`;
          `${BINDIR}/disklist > $TMP`;
          system "/usr/bin/more $TMP";
          system "/usr/bin/rm -f $TMP";
        } else {
          $output = &read_section("$LOGDIR/$golden_snapshot", "DISKLIST");
          if (open MORE, "| more") {
            print MORE $output;
            close MORE;
          }
        }
        print "\nPress <return> to continue...";
        &get_response;
    } elsif ($comp_choice eq 7) {
        system_clear();
        &st_header();
        print "Menu: Display Configuration, LUN/Disk Inquiry\n";
        print "---------------------------------------------\n\n";
        if ($live) {
            `rm -f $TMP`;
            `/usr/bin/ksh -c '${BINDIR}/disk_inquiry -d > $TMP 2> /dev/null'`;
            system "/usr/bin/more $TMP";
            system "/usr/bin/rm -f $TMP";
        } else {
          $output = &read_section("$LOGDIR/$golden_snapshot", "DISK INQUIRY");
          $output = &format_disk_inquiry($output);
          if (open MORE, "| more") {
            print MORE $output;
            close MORE;
          }
        }
        print "\nPress <return> to continue...";
        &get_response;
    } elsif ( $comp_choice eq 8 ) {
        system_clear();
        &st_header();
        print "Menu: Display Configuration, Dump Raw Data\n";
        print "------------------------------------------\n\n";
        if ($live) {
            `/usr/bin/rm -f $TMP`;
            `${BINDIR}/rawdump > $TMP`;
            system "/usr/bin/more $TMP";
            system "/usr/bin/rm -f $TMP";
        } else {
            $output = &read_section("$LOGDIR/$golden_snapshot", "RAWDUMP");
            if (open MORE, "| more") {
              print MORE $output;
              close MORE;
            }
        }
        print "\nPress <return> to continue...";
        &get_response;
    } elsif ($comp_choice eq 9) {
        my $clustername;
        my $hostn = `/usr/bin/hostname`;
        chomp $hostn;
	system_clear();
        &st_header();
        print "Cluster Check\n";
        print "-------------\n";
        if ($clustername = &in_cluster) {
            print "\n$hostn is currently in cluster $clustername\n";
        } else {
            print "\n$hostn is currently not in a running cluster\n";
        }
        print "\nPress <return> to continue...";
        &get_response;
    } elsif ( $comp_choice eq "s" ) {
        if ($live eq "-live") {
            &save_config;
        }
    } else {
        print "\n$comp_choice is an invalid option!\n";
    }
    goto D_SBUS_SUM;
}

####################################################################
# make a menu out of an array
####################################################################
sub make_arr_menu {
    (@in_array) = @_;
D_MAMENU:
    $count = 1;
    print "\nView more details by selecting a number or [q] to quit\n";
    foreach $elem (@in_array) {
        print " [${count}]\t${elem}\n";
        $count += 1;
    }
    print " [q]\tQuit\n";
    print "\nEnter Choice: ";
    $choice = &get_response;
    if ($choice =~ /^q/i) {
        return $choice;
    } elsif ($choice !~ /^\d+/) {
        print "\nUndefined option! \n\n";
        printf("\nPress <return> to continue... ");
        &get_response;
    } else {
	if (($choice-1 <= $#in_array)  && ($choice > 0)) {

            return $in_array[$choice-1];
        } else {
            print "\nUndefined option!\n\n";
            printf("\nPress <return> to continue... ");
            &get_response;
        }
    }
    goto D_MAMENU;
}
####################################################################
# print the sf (socal port ) information 
####################################################################
################################################################
# process this loop array
# if the loop is in this loop array,
# search all loops in the rawdump for the given box/loop
#
# input:
#   [AB]    loop id
# 
################################################################
sub proc_sf {
    ($srch_sf) = @_; 
    undef @sfcnum;
    undef @this_a5k;
    $prevloop = 0;
    $fndloop = "FALSE";

    $prevloop = &get_next_looparr($prevloop);
    while ($prevloop <= $#goldraw) {
        $found = "FALSE";
        foreach $line (@looparr) {
            (@line) = split(' ', $line);
            if ($line[9] =~ /^${srch_sf}/) {
                $found = TRUE;
                last;
            }
        }
        if ($found eq "TRUE") {
            # socal and sf
            $this_ddcount = 0;
            foreach $line (@looparr) {
                (@line) = split(' ', $line);
                if ($line[3] =~ /(c\d+)t\d+/) {
                    $this_cnum = $1;
                }
                if ($line[7] =~ /DD/) {
                    $this_ddcount += 1;
                }
            }
            foreach $line (@looparr) {
                (@line) = split(' ', $line);
                if ($line[9] =~ /^${srch_sf}/) {
                    $this_sf = $line[9];
                    $this_socal = $line[8];
                    $this_soc_wwn = $line[1];
                    $this_sf_wwn = $line[2];
                    $this_port_path = $line[10];
                    if ($this_port_path =~ /^(.*socal.*)\/sf\@(\d+)/) {
                        $this_sfport = $2;
                    } elsif ($this_port_path =~ /^(.*usoc.*)\/fp\@(\d+)/) {
                        $this_sfport = $2;
                    } elsif ($this_port_path =~ /^(.*qlc.*)\/fp\@(\d+)/) {
                        $this_sfport = $2;
                    } elsif ($this_port_path =~ /^(.*pci.*ifp\@)(\d+)/) {
                        $this_sfport = "0";
                    } elsif ($this_port_path =~ /^(.*pci.*)\/scsi\@(\d+)/) {
                        $this_sfport = $2;
                    } else {
                        print "Error: unknown Host Adapter port $this_port_path\n";
                    }
            
                    &pr_sf;
                }
            }
            # box names
            undef $this_cnum;

            foreach $line (@looparr) {
                (@line) = split(' ', $line);
                if ($line[0] =~ /(\S+),[AB][01]/) {
                    $tmp = $1;
                    $a1 = grep {/$tmp/} @this_a5k;
                    if (! $a1) {
                        push @this_a5k, $tmp;
                        $this_name = $tmp;
                        $this_a5k_wwn = $line[1];
                        ($this_a5k_lp) = $line[0] =~ /\S+,([AB])/;
                    }
                }
            }           
            $fndloop = "TRUE";
        }
        undef @this_a5k;
        if ($prevloop == $#goldraw) {
            last;
        }
        $prevloop = &get_next_looparr($prevloop);
    }
    if ($fndloop eq "FALSE") {
        print "\tLoop Not Found.\n";
    }

}

################################################################
# print the box name line
################################################################
sub pr_a5k {
    # format for box line
    #          0   1   2
    @pfa5k = (25, 18, 15);
    printf("%-$pfa5k[0]s%-$pfa5k[1]s\n",
     "            $this_name", "Loop ${this_a5k_lp}");

}


sub pr_sf {
    # format for sf line
    #         0   1   2   3   4
    @pfsf = (12, 8, 10, 10, 10);
    $this_LUN_count = $P_DDCNT{$this_port_path} + $A_DDCNT{$this_port_path};
    $this_drive_count = $A5_DDCNT{$this_port_path} + $OTHER_DDCNT{$this_port_path};
    printf("%-$pfsf[0]s%-$pfsf[1]s%-$pfsf[2]s%-$pfsf[3]s%-s\n",
     "      $this_sf", "port $this_sfport", $this_cnum, "$this_drive_count Drive(s) $this_LUN_count LUN(s)");
}


##############################################################
# now parse the raw data by loop
# rawdump output is sorted by loop
# the next blank line indicates the end of the loop
# everything in between are the items in the loop
# find a block and put it into an array.  
# inputs:
#   bginsrch    start the search at this line
# outputs:
#   lastha      last ha line searched
##############################################################
sub get_next_looparr {
	($index) = @_;
	if (($index < 0) || ($index > $#goldraw)) {
		die "ERROR 4000";
	}

	undef @looparr;

	# Skip any initial blank lines
	while (($index <= $#goldraw) && ($goldraw[$index] =~ /^\s*\n$/)) {
		$index++;
	}

	for ($i = $index; $i <= $#goldraw; $i++) {
		if ($goldraw[$i] =~ /^\s*\n*$/) {
			# Loop information in the rawdump output are
			# separated by blank lines.
			# Stop storing when we find a blank line.
			last;
		}
		# store this 'rawdump' output line in the array 'looparr'
		push @looparr, $goldraw[$i];
	}

	if (@looparr) {
		return ($i);
	} else {
		return $#goldraw;
	}
}
####################################################################
# allow the user to save the configuration
#
####################################################################
sub save_config {
    my $cmd; 
    my $res;
    print "This takes a few minutes.  Continue?  [ <y>,  n ]: ";
    $cmd = &get_response( 'y' );
    if ($cmd =~ /^y/i) {
        $res = `${BINDIR}/storconfig -save -log -rd ${TMP_RAW} -v `;
        print $res;
        print "Press <return> to continue... ";
        &get_response;
    }
}
####################################################################
# Get rawdump data and put it into an array
#
####################################################################
sub get_rawdump {
# if live, then call rawdump and put output into an array
#  save the output into a file in case we need to pass it on

    if ($live eq "-live") {
        print "Live: probing system... (please allow 30 seconds for each Enclosure)\n";
           @goldraw = `${BINDIR}/rawdump`;
           # Check for missing program
           if ($goldraw[0] =~ /not\s+found/i) {
               print "ERROR : ${BINDIR}/rawdump not found!\n";
           }
        unless (open (TMPFILE, ">${TMP_RAW}")) { 
            print STDERR "Unable to open temporary file: ${TMP_RAW}\n";
            print "\nPress <return> to continue... ";
            &get_response;
            exit;
        }
        print TMPFILE @goldraw;
        close(TMPFILE);
        $dmp_arg = "-file";
    } elsif ($rawfile eq "TRUE") {
        open (TMPFILE, "${TMP_RAW}");
        while ($line=<TMPFILE>) {
            push @goldraw, $line;
        }
        close(TMPFILE);
        $dmp_arg = "-file";
    } else {

# if not live, extract the rawdump information from the snapshot

        $golden_log = &get_golden_snapshot;
        if ($golden_log) {
            unless (open(GOLDRAW, "${GOLD_PATH}/$golden_log")) {
                print STDERR "Unable to open golden logfile: ${GOLD_PATH}/$golden_log\n";
                print STDERR "\nPress <return> to continue... ";
                &get_response;
                exit;
            }
        } else {
            print STDERR "Unable to find Golden logfile. Create Configuration Document.\n";
            print STDERR "\nPress <return> to continue... ";
            &get_response;
            exit;
        }

        $found_section = "FALSE";

        while ($line=<GOLDRAW>) {
            if ($line =~ /^RAWDUMP:/) {
                $found_section = "TRUE";
                next;
            }
            next if ($found_section eq "FALSE");
            last if ($line =~ /^============/);
            push @goldraw, $line;
        }
        $dmp_arg = "";
    }

}

sub proc_cli {

    # print the usage
    $usage[0]  = "$PROGNAME -help | [-live | -file] |-name <name> | -all | -do \n";
    $usage[5]  = "help - display usage\n";
    $usage[6]  = "live - use the current data (loop disruptive)\n";
    $usage[7]  = "name - name of component\n";
    $usage[8]  = "all  - process all Storage arrays\n";
    $usage[9]  = "file - use file (${TMP_RAW}) for input\n";
    $usage[10] = "do   - display only - no menu \n";

    if ($ARGV < 0) {
        die @usage;
    }
    # init vars
    my $do_something = "FALSE";
    $live = "";
    $all = "TRUE";
    $in_key = "";
    undef $rawfile; 
    undef $DISP_ONLY;

    $arg = shift @ARGV;
    while($arg) {

    if ($arg =~ /-name/) {
        $in_key = shift @ARGV;
        $do_something = "TRUE";
        $all = "FALSE";
    } elsif ($arg =~ /-live/) {
        if ($rawfile eq "TRUE") {
            die @usage;
        }
        $live = "-live";
        $do_something = "TRUE";
    } elsif ($arg =~ /[?|h]/i) {
        die @usage;
    } elsif ($arg =~ /debug/i) {
        $DEBUG = "TRUE";
    } elsif ($arg =~ /-do/i) {
        $DISP_ONLY = "TRUE";
    } elsif ($arg =~ /-file/i) {
        if ($live eq "-live") {
            die @usage;
        }
        $rawfile = "TRUE";
    } elsif ($arg =~ /-all/) {
        if ($in_key ne "") {
            die @usage;
        }
        $all = "TRUE";
        $do_something = "TRUE";
    } else {
        die "\n$arg is an invalid option!\n\n @usage";
    }
        $arg = shift @ARGV;
    }
    $GOLD_PATH  = "${LOGDIR}";
    if ($in_key eq "") {
        $all = "TRUE";
    }
}

sub cd_convert_wwn2loop {

    my $wwn = $_[0];
    $last_digit = chop( $wwn );

PI:
    {
        if ($last_digit eq "1") {$ibport = A0; last PI;}
        if ($last_digit eq "2") {$ibport = A1; last PI;}
        if ($last_digit eq "3") {$ibport = B0; last PI;}
        if ($last_digit eq "4") {$ibport = B1; last PI;}
        if ($last_digit eq "9") {$ibport = A0; last PI;}
        if ($last_digit eq "a") {$ibport = A1; last PI;}
        if ($last_digit eq "b") {$ibport = B0; last PI;}
        if ($last_digit eq "c") {$ibport = B1; last PI;}
    }

    chop( $ibport );
    return $ibport;

}

sub cd_display_sbus_summary {
    $count = 1;
    my @rawdump = @goldraw;

    foreach $e (@LD_DD_PORTS) {

        &display_summary( $e );

        # code to display boxes per loop per hba

        # $e contains current path 
        $sf_socal_path = $e;
        $sf_socal_path =~ s/\S+(\d+):(\d)/\/sf\@$2,0/;
        $ib_socal_path = $e;
        $ib_socal_path =~ s/(\S+\d+):\d/$1/;
        $ib_socal_path .= $sf_socal_path;

        $path_found = "FALSE";
        $bc = 0;
        # look in rawdump for path
        undef @ha_dump;
        foreach $ha_line (@rawdump) {
            if ( $ha_line =~ /$ib_socal_path/ ) {
                push @ha_dump, $ha_line;
            }
        }
        foreach $raw_line (@ha_dump) {
            # we have a valid path per the HBA, port, wwn, name
            for ( $i = 0; $i < $box_count; $i++ ) {
                # find box name(s) attached to sf
                if ( $raw_line =~ /$box_wwn[ $i ]/ ) {
                    $hba_box_names[ $bc ] = $box_name[ $i ];
                    # box wwn, Loop A or B
                    (@raw_line) = split(' ', $raw_line);
                    $local_wwn = $raw_line[ 2 ];
                    $hba_loop[ $bc ] = &cd_convert_wwn2loop( $local_wwn );
                    $bc++;
                }
            }
        }
        $count++;
        if ( $bc > 0 ) {
            # remove duplicates
            for ( $i = 0; $i < $bc; $i++ ) {
                print "\t\tEnclosure $hba_box_names[ $i ]";
                print "\tLoop $hba_loop[ $i ]\n";
                if ( $hba_box_names[ $i ] == $hba_box_names[ $i + 1 ] ) {
                    if ( $hba_loop[ $i ] == $hba_loop[ $i + 1 ] ) {
                        $i++;
                    }
                }
            }
        }
        print "\n";
    }
}

sub display_summary {

    my $e = $_[0];
    my ($show2, $cnt, $dr_cnt, @our_drives);

    $show = sprintf "%-10s%-6s","$SOCNUM{$e}", "$SFNUM{$e}";
    print "$show   $e\n";
    $show2 = "    Port $PORTNUM{$e}";
    $cnt = $A5000_LIST{$e};
    $dr_cnt = $A5_DDCNT{$e} + $OTHER_DDCNT{$e};
    $cnt = split / /, $cnt;
    $T300_LUN_count = $P_DDCNT{$e} + $A_DDCNT{$e};
    $show2 .= "  Controller $CNUM{$e}  $dr_cnt disks(s)  $cnt A5K enclosure(s)  $T300_CNT{$e} T300(s)  $T300_LUN_count LUN(s)\n";
    foreach $encl_name (split /\s+/, $A5000_LIST{$e}) {
        $show = sprintf "%-17s%-13s", "$encl_name", "$A5000_CNT{$encl_name} disk(s)";
        print "         A5K Enclosure: $show\n";
    }
    if ($T300_CNT{$e}) {
        my (@primary_drives, @alternate_drives, $drive_cnt, $dnum, %T300_TARGETS);

        @disklist = (keys %T300);
        undef @our_drives;  # drives only on this loop
        undef %T300_TARGETS;
        ################################################
        # collect all drives on this loop, then        #
        # separate by target id number. This           #
        # gives us separate lists of LUNs by T300 box. #
        ################################################
        foreach $drive (@disklist) {
	#
	#  Fix for Bug 4310247
	#
	   ($drive_cnum) = $drive =~ /(c\d+)t\d+d\d+/;
           if ($drive_cnum eq $CNUM{$e}) {
               my ($cur_target, $cur_disk) =  $drive =~ /c\d+t(\d+)d(\d+)/;
               $T300_TARGETS{$cur_target} .= "$drive "; # LUNs in this target T300 box
           }
        }
        ###############################
        # Loop once for each T300 box #
        ###############################
        foreach $target (sort (keys %T300_TARGETS)) {
            undef @primary_drives;
            undef @alternate_drives;
            chop $T300_TARGETS{$target}; # remove trailing space
            @our_drives = split / /, $T300_TARGETS{$target};
            foreach $drive (@our_drives) {
               if ( $T300{$drive} eq "p" ) {
                   push @primary_drives, $drive;
               } elsif ( $T300{$drive} eq "a" ) {
                   push @alternate_drives, $drive;
               }
               $drive_cnt ++;
            }
            $show = sprintf "%-21s%-21s", "$P_DDCNT{ $e } Primary LUN(s)", "$A_DDCNT{ $e } Alternate LUN(s)";
            print "         T300 $CNUM{$e}t$target: $show\n";
            ##########################################
            # output a pretty display with 2 columns #
            ##########################################
            #for ($dnum = 0; $dnum < $drive_cnt; $dnum ++ ) {
            #    last if (($primary_drives[$dnum] eq "") && ($alternate_drives[$dnum] eq ""));
            #    $show = sprintf "%-21s%-21s", "$primary_drives[$dnum]", "$alternate_drives[$dnum]";
            #    print "         $show\n";
            #}
        }
    }
    print $show2;
}

sub format_disk_inquiry {
  ##############################################
  # take disk_inquiry -q -P output and make it #
  # look like disk_inquiry -d                  #
  ##############################################
  my ($cmd) = @_;

  my ($results, $i);
  $results .= "AVAILABLE SCSI DEVICES:\n\n";
  $results .= "   Location     Vendor          Product         Rev  Serial #   Dual Port\n\n";

  foreach $i (split /\n/, $cmd) {
      chomp $i;
      my ($ctd, $vendor, $rest) = ($i =~ /^\s+(\S+)\s+(\S+)\s+(.*)$/);
      ###########################################
      # shave off first 16 chars for disk label #
      # we have to do it this way because a     #
      # space char might be inside label        #
      ###########################################
      my ($label, $j, $ch, $rsd);
      foreach $j (split //, $rest) {
          $ch ++;
          if ($ch <= 16) {
              $label .= $j;
          } else {
              $rsd .= $j;
          }
      }
      my ($space, $rev, $serial, $dual) = split /\s+/, $rsd;
      if ($ctd eq "") {
          $results .= "$i\n";
      } else {
          $results .= "   " . sprintf("%-13s%-12s%-20s%-5s%-11s%-9s", "$ctd", "$vendor", "$label", "$rev", "$serial", "$dual") . "\n";
      }
  }
  return $results;
}
