#!/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;
&make_port_list( $HBA );

if ( $STORAGE eq "A5" ) {
    foreach $e (@LD_A5_PORTS) {
        if (!grep { /$SOCNUM{$e}/ } @socal_list) {
            push @socal_list, $SOCNUM{$e};
        }
    }

    foreach $e (@LD_A5_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;
            }
        }
    }
}

if ( $HBA eq "S" ) {
    &pr_sbus_summary;
} elsif ( $HBA eq "P" ) {
    &pr_pci_summary;
}


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

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

    my $comp_choice;
D_SBUS_SUM:
    if ($DISP_ONLY ne "TRUE") {
        system_clear();
        &st_header();
        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";
    if ( $STORAGE eq "A5" ) {
        print " [1] Display Configuration Summary\n";
        print " [2] Display Array Map\n";
        print " [3] Host Adaptor / socal#\n";
        print " [4] sf # \n";
        print " [5] 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";
    } elsif ( $STORAGE eq "T3" ) {
        print " [1] Display Configuration Summary\n";
        print " [3] Host Adaptor / socal#\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 ) && ( $STORAGE eq "A5" ) ) {
         system_clear();
         &st_header();
         print "Menu: Display Configuration, Array Map\n";
         print "--------------------------------------\n\n";

         if ($live eq "-live") {
             `${BINDIR}/storconfig -$STORAGE -$HBA -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 Adaptors\n";
            print "------------------------------------------\n\n";
            if ( $STORAGE eq "A5" ) {
                $usr_ch = &make_arr_menu(sort(@socal_list));
                if ($usr_ch !~ /^q/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;
                }
            } elsif ( $STORAGE eq "T3" ) {
                $loop = 0;
                foreach $path (@LD_T3_PORTS) {
                    print " ---- Online: ${path} ----\n";
                    print " \t($CNUM{$path}) ($SFNUM{$path})  ($SOCNUM{$path}:Port $PORTNUM{$path}) \n";
                }
                printf("\nPress <return> to continue... ");
                &get_response;
            }
        }
    } elsif ( ( $comp_choice eq 4 ) && ( $STORAGE eq "A5" ) ) {
        $loop = 1;
        while($loop) {
            system_clear();
            &st_header();
            print "Menu: Display Configuration, sf 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 ) && ( $STORAGE eq "A5" ) ) {
        $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";
        `/usr/bin/rm -f $TMP`;
        `${BINDIR}/disklist > $TMP`;
        system "/usr/bin/more $TMP";
        system "/usr/bin/rm -f $TMP";
        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";
        `rm -f $TMP`;
        if ( $STORAGE eq "A5" ) {
            `/usr/bin/ksh -c '${BINDIR}/disk_inquiry > $TMP 2> /dev/null'`;
        } elsif ( $STORAGE eq "T3" ) {
            `/usr/bin/ksh -c '${BINDIR}/disk_inquiry -d -p > $TMP 2> /dev/null'`;
        }
        system "/usr/bin/more $TMP";
        system "/usr/bin/rm -f $TMP";
        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";
        `/usr/bin/rm -f $TMP`;
        if ( $STORAGE eq "A5" ) {
            `${BINDIR}/sfdmp -rd ${dmp_arg} > $TMP`;
        } elsif ( $STORAGE eq "T3" ) {
            `${BINDIR}/rawdump -$HBA -$STORAGE > $TMP`;
        }
        system "/usr/bin/more $TMP";
        system "/usr/bin/rm -f $TMP";
        print $result;
        print "\nPress <return> to continue...";
        &get_response;
    } elsif ($comp_choice eq 9) {
        my $clustername;
        my $hostn = `/usr/bin/hostname`;
        chomp $hostn;
        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;
}

sub pr_pci_summary {

    my $comp_choice;
D_PCI_SUM:
    if ($DISP_ONLY ne "TRUE") {
        system_clear();
        &st_header();
        print "Menu: Display Configuration Summary\n";
        print "-----------------------------------\n\n";
    } else {
        print "(Online Components Only)\n\n";
        &cd_display_pci_summary;
        if ($DISP_ONLY eq "TRUE") {
            exit;
        }
    }
    print "View more details by selecting a component number [x] or quit:\n";
    if ( $STORAGE eq "A5" ) {
        print " [1] Display Configuration Summary\n";
        print " [2] Display Array Map\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";
    } elsif ( $STORAGE eq "T3" ) {
        print " [1] Display Configuration Summary\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_pci_summary;
        select STDOUT;
        close MORE;
        printf("\nPress <return> to continue... ");
        &get_response;

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

         if ($live eq "-live") {
             `${BINDIR}/storconfig -$STORAGE -$HBA -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 6 ) {
        system_clear();
        &st_header();
        print "Menu: Display Configuration, LUN/Disk Drive List\n";
        print "------------------------------------------------\n\n";
        `rm -f $TMP`;
        `${BINDIR}/disklist > $TMP`;
        system "/usr/bin/more $TMP";
        system "/usr/bin/rm -f $TMP";
        print "\nPress <return> for previous menu ...\n";
        &get_response;
    } elsif ( $comp_choice eq 7 ) {
        system_clear();
        &st_header();
        print "Menu: Display Configuration, LUN/Disk Inquiry\n";
        print "---------------------------------------------\n\n";
        `/usr/bin/rm -f $TMP`;
        if ( $STORAGE eq "A5" ) {
            `/usr/bin/ksh -c '${BINDIR}/disk_inquiry > $TMP 2> /dev/null'`;
        } elsif ( $STORAGE eq "T3" ) {
            `/usr/bin/ksh -c '${BINDIR}/disk_inquiry -d -p > $TMP 2> /dev/null'`;
        }
        system "/usr/bin/more $TMP";
        system "/usr/bin/rm -f $TMP";
        print "\nPress <return> for previous menu ...\n";
        &get_response;

    } elsif ( $comp_choice eq 8 ) {
        system_clear();
        &st_header();
        print "Menu: Display Configuration, Dump Raw Data\n";
        print "------------------------------------------\n\n";
        `/usr/bin/rm -f $TMP`;
        if ( $STORAGE eq "A5" ) {
            `${BINDIR}/pcidump > $TMP`;
        } elsif ( $STORAGE eq "T3" ) {
            `${BINDIR}/rawdump -$HBA -$STORAGE > $TMP`;
        }
        system "/usr/bin/more $TMP";
        system "/usr/bin/rm -f $TMP";
        print $result;
        print "\nPress <return> to continue...";
        &get_response;
    } elsif ($comp_choice eq 9) {
        my $clustername;
        my $hostn = `/usr/bin/hostname`;
        chomp $hostn;
        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 "Undefined Choice.  Try again.\n\n";
    }
    goto D_PCI_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) {
            return $in_array[$choice-1];
        } else {
            print "\nUndefined option!\n\n";
            printf("\nPress <return> to continue... ");
            &get_response;
        }
    }
    goto D_MAMENU;
}
####################################################################
# print the HA information for this ha
####################################################################
################################################################
# process this loop array
# if the loop is in this loop array, then print out 
# the ha and disk information.
# search all loops in the rawdump for the given box/loop
#
# input:
#   [AB]    loop id
# 
################################################################
sub proc_socal {
    ($srch_soc) = @_; 
    undef @sfcnum;
    undef @this_a5k;
    $prevloop = 0;
    $this_ddcount = 0;
    $fndloop = "FALSE";

    $prevloop = &get_next_looparr($prevloop);

    while ($prevloop <= $#goldraw) {
        $found = "FALSE";
        foreach $line (@looparr) {
            (@line) = split(' ', $line);
            if ($line[8] =~ /^${srch_soc}/) {
                $found = TRUE;
                last;
            }
        }
        if ($found eq "TRUE") {
            # find the c number
            $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++;
                }
            }
            # find socal and sf information
            foreach $line (@looparr) {
                (@line) = split(' ', $line);
                if ($line[8] =~ /^${srch_soc}/) {
                    $this_sf = $line[9];
                    $this_socal = $line[8];
                    $this_soc_wwn = $line[1];
                    $this_sf_wwn = $line[2];
                    ($this_soc_sbus) = $line[10] =~ /(sbus\@[a-f0-9][a-f0-9])/;
                    ($this_sfport) = $line[10] =~ /:(\d)/;
                    $this_sf_phys = $line[10];
                    ($this_soc_phys) = $line[10] =~ /(\/devices\/.*:\d)$/;
                    &pr_socal;
                    &pr_sf;
                }
            }

            undef $this_cnum;

            # box names
            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])/;
                        &pr_a5k;
                    }
                }
            }
            $fndloop = "TRUE";
        }
        undef @this_a5k;
        if ($prevloop == $#goldraw) {
            last;
        }
        $prevloop = &get_next_looparr($prevloop);
        print "\n";
    }
    if ($fndloop eq "FALSE") {
        print "\tLoop Not Found.\n";
    }

}

####################################################################
# 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";

#   print "This is the search string = $srch_sf\n";
    $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_soc_sbus) = $line[10] =~ /(sbus\@[a-f0-9][a-f0-9])/;
                    ($this_sfport) = $line[10] =~ /:(\d)/;
                    $this_sf_phys = $line[10];
            # if we find another instance of this socal on this loop, then
            # we may want to flag a warning as a possible configuration error
            # 2 socal ports to the same loop is not very redundant
                    &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])/;
                    }
                }
            }           
#       print "\t\t${this_ddcount} drives on this loop.\t${this_cnum}\n\n";
            $fndloop = "TRUE";
        }
        undef @this_a5k;
        if ($prevloop == $#goldraw) {
            last;
        }
        $prevloop = &get_next_looparr($prevloop);
#       print "\n";
    }
    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}");

}
################################################################
# print the HA information for this looparr
################################################################
sub pr_socal {
    # format for socal line
    #          0  1   2  
    @pfsoc = (25, 18, 10);
    
    printf("%-$pfsoc[0]s%-$pfsoc[1]s\n", 
     $this_socal, $this_soc_phys);
}

sub pr_sf {
    # format for sf line
    #         0   1   2   3   4
    @pfsf = (25, 18, 10, 10, 10);
    printf("%-$pfsf[0]s%-$pfsf[1]s%-$pfsf[2]s%-$pfsf[3]s%-s\n",
     "      $this_sf", "Port ${this_sfport}", $this_cnum, "${this_ddcount} ${LUN_DISK}(s)");
}
##############################################################
# now parse the raw data by loop
# rawdump output is sorted by loop
# the start of a loop contains the string *HA*
# 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";
    }

    $startarr = "FALSE";
    undef @looparr;

    for ($i = $index; $i <= $#goldraw; $i++) {
        if ($goldraw[$i] !~ /^\s*\n*$/) {
            if ($goldraw[$i] =~ /^[\*HA\*|\*UK\*]/) {
                push @looparr, $goldraw[$i];
                $startarr = "TRUE";
#               print "\nDEBUG:  FOUND a LOOP!!  \n\n";
                next;
            }
            if ($startarr eq "TRUE") {
                push @looparr, $goldraw[$i];
                next;
            }
        } else {
            if ($startarr eq "TRUE") {
                last;
            }
        }
    }
    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) {
        if ( $HBA eq "S" ) {
            $res = `${BINDIR}/storconfig -$STORAGE -$HBA -save -log -rd ${TMP_RAW} -v `;
        } elsif ( $HBA eq "P" ) {
            $res = `${BINDIR}/storconfig -$STORAGE -$HBA -save -log ${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";
        if ( ( $HBA eq "S" ) || ( $STORAGE eq "T3" ) ) {
           @goldraw = `${BINDIR}/rawdump -$HBA -$STORAGE`;
           # Check for missing program
           if ($goldraw[0] =~ /not\s+found/i) {
               print "ERROR : ${BINDIR}/rawdump not found!\n";
           }
        } elsif ( ( $HBA eq "P" ) && ( $STORAGE eq "A5" ) ) {
           @goldraw = `${BINDIR}/pcidump`;
           if ($goldraw[0] =~ /not\s+found/i) {
               print "ERROR : ${BINDIR}/pcidump 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( $STORAGE );
        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 | [-S | -P] | [-A5 | -T3] | [-live | -file] |-name <name> | -all | -do \n";
    $usage[1]  = "S    - SBUS HBA\n";
    $usage[2]  = "P    - PCI HBA\n";
    $usage[3]  = "A5   - A5K Series Storage Array (default)\n";
    $usage[4]  = "T3   - T300 Series Storage Array\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
	$STORAGE = "A5";
    my $do_something = "FALSE";
    $live = "";
    $all = "TRUE";
    $in_key = "";
    undef $rawfile; 
    undef $DISP_ONLY;

    $arg = shift @ARGV;
    while($arg) {
    if ($arg =~ /^-S/) {
        $HBA = "S";
        check_bus_option( $HBA );
    } elsif ($arg =~ /^-P/) {
        $HBA = "P";
        check_bus_option( $HBA );
    } elsif ($arg =~ /^-A5/) {
        $STORAGE = "A5";
        $LUN_DISK = "Drive";
    } elsif ($arg =~ /^-T3/) {
        $STORAGE = "T3";
        $LUN_DISK = "LUN";
    } elsif ($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}/${STORAGE}";
    if ($in_key eq "") {
        $all = "TRUE";
    }
    if ( $HBA ne "P" && $HBA ne "S" ) {
        &get_fcal_hbas();
        &select_fcal_hbas();
    }

}

# copied for storstat. Want to move to subroutines.pm in next release
sub cd_luxadm_probe {

    # Probe once and store in a global variable
    $LUXADM_PROBE = `/usr/bin/ksh -c '/usr/sbin/luxadm probe 2>&1'`;
    if ( $STORAGE eq "A5" ) {
        if ( $LUXADM_PROBE =~ /No Network Array enclosures found/ ) {
            print "\t\n",$LUXADM_PROBE,"\n";
            return -1;
        }
    }
    return 0;
}

# copied for storstat. Want to move to subroutines.pm in next release
sub cd_get_enclosure_names {

    my @data = split(/\n/, $LUXADM_PROBE);

    my ($line, $name, $enc_wwn);

    $box_count = 0;
    foreach $line (@data) {
        chomp $line;
        next if ($line !~ /WWN/);
        next if ($line !~ /Name/);
        # Example line SENA may change
        # SENA               Name:z   Node WWN:50800200000001a0   
        ($name, $enc_wwn) = $line =~ /Name:(\S+)\s.*WWN:(\w+)/;
        $box_name[$box_count] = $name;
        $box_wwn[$box_count] = $enc_wwn;
        $box_count++;
    }
}

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_pci_summary {
    $count = 1;
    my @pcidump = @goldraw;

    # do a "/usr/sbin/luxadm probe" and save all wwn and name
    if (&cd_luxadm_probe() == -1) {
        return;
    }
    &cd_get_enclosure_names;    
    if ( $STORAGE eq "A5" ) {
        (@in_array) = @LD_A5_PORTS;
    } elsif ( $STORAGE eq "T3" ) {
        (@in_array) = @LD_T3_PORTS;
    }
    foreach $e (@in_array) {

        &display_summary( $e );

        # code to display boxes per loop per hba

        # $e contains current path - need to strip ,0:0
        $current_ifp_path = $e;
        $current_ifp_path =~ s/,0:0/:devctl/;

        # look in pcidump for path
        $path_found = "FALSE";
        $bc = 0;
        foreach $pci_line (@pcidump) {
            if ( $path_found eq "TRUE" ) {
                if ( $pci_line =~ /^Information for/ ) {
                        $path_found = "FALSE";
                        next;
                }
                # we have a valid path per the HBA, port, wwn, name
                for ( $i = 0; $i < $box_count; $i++ ) {
                    # find box name(s) attached to hba
                    if ( $pci_line =~ /$box_wwn[ $i ]/ ) {
                        $hba_box_names[ $bc ] = $box_name[ $i ];
                        # box wwn, Loop A or B
                        (@pci_line) = split(' ', $pci_line);
                        $local_wwn = $pci_line[ 2 ];
                        $hba_loop[ $bc ] = &cd_convert_wwn2loop( $local_wwn );
                        $bc++;
                    }
                }
            }
            if ( $pci_line =~ /^Information for $current_ifp_path/ ) {
                # found path, now look for wwn of IB
                $path_found = "TRUE";
            }
        }
        $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 cd_display_sbus_summary {
    $count = 1;
    my @rawdump = @goldraw;

    # do a "/usr/sbin/luxadm probe" and save all wwn and name
    if (&cd_luxadm_probe == -1) {
        return;
    }
    &cd_get_enclosure_names;    
    if ( $STORAGE eq "A5" ) {
        (@in_array) = @LD_A5_PORTS;
    } elsif ( $STORAGE eq "T3" ) {
        (@in_array) = @LD_T3_PORTS;
    }
    foreach $e (@in_array) {

        &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];

    print " $SOCNUM{$e} \t${e} \n";
    if ( $HBA eq "S" ) {
        print " \t$SFNUM{$e}";
    }
    print " \tPort $PORTNUM{$e}";
    print " \tController $CNUM{$e} \t\t$DDCNT{$e} ${LUN_DISK}(s)\n";
    if ( $STORAGE eq "T3" ) {
        print " \t\t\t\t\tPrimary: $P_DDCNT{ $e }\tAlternate: $A_DDCNT{ $e }\n";
        my @primary_drives = ();
        my @alternate_drives = ();
        my @port_status = ();
        my $drive_cnt = 0;
        $dl = `${BINDIR}/disk_inquiry -q -d 2>&1`;
        @disklist = split ('\n', $dl);
        foreach $line (@disklist) {
           ($drive, $vendor, $product, $revision, $serial_no, $dual_port) = split(" ", $line );
           ($drive_cnum) = $drive =~ /(c\d+)t\d+d\d+/;
           if ($drive_cnum eq $CNUM{$e}) {
               if ( $dual_port eq $PRIMARY ) {
                   push @primary_drives, $drive;
               } elsif ( $dual_port eq $ALTERNATE ) {
                   push @alternate_drives, $drive;
               }
               $drive_cnt++;
           }
        }
        my $dnum;
        for ($dnum = 0; $dnum <= $drive_cnt; $dnum ++ ) {
            print "\t\t\t\t\t$primary_drives[$dnum]";
            print "\t\t$alternate_drives[$dnum]\n";
            if ( ( ! $primary_drives[$dnum] ) && ( ! $primary_drives[$dnum] ) ) {
                last;
            }
        }

    }

}
