#!/var/opt/STORtools/bin/perl
#       @(#)storcron.pl 1.1 98/08/25 SMI
#       Copyright (c) 1998 by Sun Microsystems, Inc.

$ENV{EDITOR} = "/bin/ed";

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

$PROGNAME    = "storcron";
$ROOT_CRON_FILE = "/var/spool/cron/crontabs/root";
$TOOLS_CRON_FILE = "$LOGDIR/tools.cron";

#$DEBUG = "TRUE";

###############################################################
# storcron 
#
# creates cron setup for status checks with email notification 
# on changes and failures 
# other features: interrupt handler and error checking
###############################################################

###############################################################
# main crontab menu/loop
# 
# takes input from the user to add, delete, edit, list or search
# the crontab file
# currently only add, delete, edit and list are available
###############################################################

&check_root_access( $PROGNAME );
&proc_cli;

my @menu;

if ( $STORAGE eq "A5" ) {
    @menu =
     (
      "Menu: Automate Status/Message Checks with Crontab Utility\n",
      "---------------------------------------------------------\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      " [1] Enable A5K Status Checking\n",
      " [2] Disable A5K Status Checking\n",
      " [3] Enable ${STOREDGE} ${SYSLOG} Checking\n",
      " [4] Disable ${STOREDGE} ${SYSLOG} Checking\n",
      " [c] Check all ${PRODUCT} Crontab Entries\n"
     );
} elsif ( $STORAGE eq "T3" ) {
    @menu =
     (
      "Menu: Automate Status/Message Checks with Crontab Utility\n",
      "---------------------------------------------------------\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      " [1] Enable T300 Status Checking\n",
      " [2] Disable T300 Status Checking\n",
      " [3] Enable ${STOREDGE} ${SYSLOG} Checking\n",
      " [4] Disable ${STOREDGE} ${SYSLOG} Checking\n",
      " [5] Enable T300 syslogd ${T300_MESSAGES} Checking\n",
      " [6] Disable T300 syslogd ${T300_MESSAGES} Checking\n",
      " [c] Check all ${PRODUCT} Crontab Entries\n"
     );
}


while ($input !~ /^q/i) {
    if (&check_for_obsolete_crontab() ) {
       $OBSOLETE_CRON = "TRUE";
       print "Obsolete ${PRODUCT} crontab entries detected!\n";
       print "The current ${PRODUCT} will not support these entries.\n";
       while (1) {
           print "Would you like ${PRODUCT} to update these entries? [<y>, n]";
           $ans = &get_response( "y" );
           if ( $ans eq "y" ) {

               $cron_line = &display_entry("${STORAGE} Status", $OBSOLETE_CRON);
               &delete_entry("${STORAGE} Status");
               &add_entry("${STORAGE} Status", $cron_line );
               &display_entry("${STORAGE} Status");
               &continue();

               $cron_line = &display_entry("${STOREDGE} Messages", $OBSOLETE_CRON);
               &delete_entry("${STOREDGE} Messages");
               &add_entry("${STOREDGE} Messages", $cron_line);
               &display_entry("${STOREDGE} Messages");
               &continue();

               &delete_obsolete('#STORtools');
               $OBSOLETE_CRON = "FALSE";

               last;
           } elsif ( $ans eq "n" ) {
               print "The current ${PRODUCT} will not support these entries.\n";
               print "You must delete the entries by hand from $TOOLS_CRON_FILE.\n";
               print "Then execute - crontab $TOOLS_CRON_FILE -\n";
               last;
           } else {
               print "\n";
               print "$ans is an invalid option!\n";
           }
       }
    }

    if (&check_crontab("${STORAGE} Status")) {
        $cron_status = "ENABLED";
    } else {
        $cron_status = "DISABLED";
    }
    splice(@menu, 3, 1, "${STORAGE} Storage status checking is $cron_status\n");

    if (&check_crontab("${STOREDGE} Messages")) {
        $cron_status = "ENABLED";
    } else {
        $cron_status = "DISABLED";
    }
    splice(@menu, 4, 1, "${STOREDGE} Messages checking is $cron_status\n");

    if ( $STORAGE eq "T3" ) {
        if (&check_crontab("${STORAGE} syslogd")) {
            $cron_status = "ENABLED";
        } else {
            $cron_status = "DISABLED";
        }
        splice(@menu, 5, 1, "${STORAGE} syslogd checking is $cron_status\n");
    }


    $input = do_menu(\@menu);

    if ($input == 1) {
        &add_entry("${STORAGE} Status");
        $golden_log = &get_golden_snapshot( $GOLD_PATH );
        if (! ${golden_log}) {
            print "There is no golden snapshot available to compare with!\n";
            print "Please verify the system is configured correctly and \n";
            print "then run '${PROGNAME} -save' for future comparisons.\n";
            while (1) {
                print "Continue with crontab setup? [<y>, n] ";
                $t_ans = &get_response( "y" );
                if ( $t_ans eq "y" ) {
                    last;
                } elsif ( $t_ans eq "n" ) {
                    last;
                }
            }
        }
    } elsif ($input == 2) {
        &delete_entry("${STORAGE} Status");
    }  elsif ($input == 3) {
        &add_entry("${STOREDGE} Messages");
        $golden_log = &get_golden_snapshot( $GOLD_PATH );
        if (! ${golden_log}) {
            print "There is no golden snapshot available to summarize messages!\n";
            print "Please verify the system is configured correctly and \n";
            print "then run '${PROGNAME} -save' for future comparisons.\n";
            while (1) {
                print "Continue with crontab setup? [<y>, n] ";
                $t_ans = &get_response( "y" );
                if ( $t_ans eq "y" ) {
                    last;
                } elsif ( $t_ans eq "n" ) {
                    last;
                }
            }
        }
    } elsif ($input == 4) {
        &delete_entry("${STOREDGE} Messages");
    } elsif ( ( $input == 5 ) && ( $STORAGE eq "T3" ) ) {
        $t_ans = "y";
        if ( ! ( -e "${T300_MESSAGES}" ) ) {
            print "\n";
            print "\n${T300_MESSAGES} not present!\n";
            print "T300 message checking requires that all T300 messages\n";
            print "reside in the file ${T300_MESSAGES}.\n";
            print "Reference the ${PRODUCT} User Guide for more information.\n";
            while (1) {
                print "Continue with crontab setup? [<y>, n] ";
                $t_ans = &get_response( "y" );
                if ( $t_ans eq "y" ) {
                    last;
                } elsif ( $t_ans eq "n" ) {
                    last;
                }
            }
        }
        if ( $t_ans eq "y" ) {
            &add_entry("${STORAGE} syslogd");
        }
    } elsif ( ( $input == 6 ) && ( $STORAGE eq "T3" ) ) {
        &delete_entry("${STORAGE} syslogd");
    } elsif ($input eq "c" ) {
        system_clear();
        &st_header();
        print "Menu: Show all ${PKGNAME} Crontab Entries\n";
        print "----------------------------------------\n";
        if ( $STORAGE eq "A5" ) {
            &display_entry("A5 Status");
        } elsif ( $STORAGE eq "T3" ) {
            &display_entry("T3 Status");
        }
        &display_entry("${STOREDGE} Messages");
        if ( $STORAGE eq "T3" ) {
            &display_entry("T3 syslogd");
        }
        print "\nTo change your scheduled entry check, disable the\n",
          "current entry and schedule a new entry using the previous menu.\n",
          "\nPress <return> to continue...";
        &get_response;
    } elsif ($input eq "q" ) {
        next;
    } else {
        print "$input is an invalid option!\n";
    }
}

###############################################################
# add_entry
#
# checks if crontab entry already exists. if exists, tell user
# and return to main menu.  otherwise, check if a golden 
# snapshot exists, and then allow user to add an
# entry for status checking.  prompt and check dates.
###############################################################
sub add_entry {
    my $task = shift;
    my $cron_line = shift;
    system_clear();
    &st_header();
    print "Menu: Add  ${PRODUCT} $task Checking\n";
    print "------------------------------------------\n\n";

    #convert pre STORtools 3.2 contrab entries to 3.2 format
    if ( defined $cron_line ) {
        if($task eq "${STORAGE} Status") {
            ($min, $hr, $day, $month, $week_day, $cmd, $flag0, $flag1, $flag2, $email) = split(/\s/,$cron_line);
            $cmd .= " $flag0";
            $cmd .= " -$STORAGE";
            $cmd .= " $flag1";
            $cmd .= " $flag2";
            $cmd .= " -mail";
            $cmd .= " $email";
        } elsif ($task eq "${STOREDGE} Messages") {
            ($min, $hr, $day, $month, $week_day, $cmd, $flag1, $flag2, $email) = split(/\s/,$cron_line);
            $cmd .= " $flag1";
            $cmd .= " -f";
            $cmd .= " $flag2";
            $cmd .= " -mail";
            $cmd .= " $email";
        }
        if(writeCronFile("$TOOLS_CRON_FILE", "# ${PRODUCT} $task Checking Entry\n$min $hr $day $month $week_day $cmd\n") < 0 ) {
            print "writeCronFile() error writing $TOOLS_CRON_FILE.\n";
            return;
        }
        if (cronUpdate("$TOOLS_CRON_FILE") ) {
            print "${PRODUCT} $task Checking is being scheduled.\n";
        } else {
            print "${PRODUCT} $task Checking has not been enabled.\n";
        }
        return;
    }
    if (&check_crontab($task)) {
        print "$task checking has already been enabled.\n";
    } else {

        ($min, $hr, $day, $month, $week_day) = &get_date();
        if( $min == -1 ) {
            $cmd = "fail";
        } else {
            $cmd = &get_cmd($task);
        }
        if ($cmd eq "fail") {
            print "${PRODUCT} $task Checking will not be scheduled.\n";
        } else {
            if(writeCronFile("$TOOLS_CRON_FILE",
                 "# ${PRODUCT} $task Checking Entry\n$min $hr $day $month $week_day $cmd\n") < 0 ) {
                print "writeCronFile() error writing $TOOLS_CRON_FILE.\n";
                return;
            }
            if (cronUpdate("$TOOLS_CRON_FILE") ) {
                print "${PRODUCT} $task Checking is being scheduled.\n";
            } else {
                print "${PRODUCT} $task Checking has not been enabled.\n";
            }
        }
    }
    &continue;
}


###############################################################
# check_crontab
#
# searches the crontab file for entry
###############################################################

sub check_crontab {
    my $entry = shift;
    my @cron_list = `/usr/bin/cat $ROOT_CRON_FILE`;
    if ( @cron_list ) {
        foreach  (@cron_list) {
            return 1 if (/# ${PRODUCT} $entry Checking Entry/);
            # required for STORtools release 3.1 and below
            return 1 if (/#$entry Checking Entry/); 
        }
    } else {
        print "problem retrieving $ROOT_CRON_FILE!\n";
    }
    return 0;
}

sub check_for_obsolete_crontab {
    my $entry = shift;
    # just in case we can't access crontab
    my @cron_list = `/usr/bin/cat $ROOT_CRON_FILE`;
    if ( @cron_list ) {
        foreach  (@cron_list) {
            return 1 if (/#STORtools/);
        }
    } else {
        print "problem retrieving $ROOT_CRON_FILE!\n";
    }
    return 0;
}
###############################################################
# get_cmd
#
# for now, hard coded to use storconfig. 
# prompt user for email notification on changes.
###############################################################
sub get_cmd {
    my $task = shift;
    my $ans;
    if ($task eq "${STORAGE} Status") {
        $cmd = "$BINDIR/storconfig -$STORAGE -$HBA -cmp -log";
    } elsif ($task eq "${STOREDGE} Messages") {
        $cmd = "$BINDIR/mess_alert -$HBA -f $SYSLOG";
    } elsif ($task eq "${STORAGE} syslogd") {
        $cmd = "$BINDIR/mess_alert -$HBA -$STORAGE -f ${T300_MESSAGES}";
    }
    $SPACES="TRUE";
    while ( $SPACES eq "TRUE" ) {
        print "\n";
        print "For email notification, specify email address(es) separated\n";
        print "by a comma. NO SPACES!\n";
        print "\n";
        print "To <root> : ";     
        $email = <STDIN>;
        if ($email =~ / /) {
            $SPACES="TRUE";
            chomp($email); 
            print "\n$email is invalid. NO SPACES!\n";
        } else {
            $SPACES="FALSE";
        }
        print "\n";
    }
    if ($email =~ /\w+/) {
        chomp($email); 
    } else { 
        $email = "root";
    }
    $email =~ s/\s+//;
    $email =~ s/\s+$//;
    $cmd .= " -mail";
    $cmd .= " $email";
    if ( $task eq "${STORAGE} syslogd" ) {
        &get_scan_window($task);
    }
    print "Would you like to continue?  Please confirm. [<y>, n]: "; 
    $ans = &get_response( "y" );
    if ( $ans eq "y" ) {
        return ( $cmd );
    } else {
        return ( "fail" );
    }
}

sub get_scan_window {
    my $hours = 24;
    my $minutes = 0;
    my $hours_valid = "FALSE";
    my $minutes_valid = "FALSE";
    print "Each time the crontab entry for a scan of ${T300_MESSAGES}\n";
    print "is invoked it will scan backwards 24 hours for errors (default).\n";
    print "To change the default behavior, select a scan window to match\n";
    print "the above crontab parameters (hours & minutes).\n";
    while ( $hours_valid eq "FALSE" ) {
        print "Enter window to scan backwards in hours (default $hours): ";     
        $hours = &get_response( "$hours" );
        if ( ( $hours < 0 ) || ( $hours > 24 ) ) {
            print "\n$hours is must be between 0 and 24!\n"; 
        } else {
            $hours_valid = "TRUE";
        }
    }
    while ( $minutes_valid eq "FALSE" ) {
        print "Enter window to scan backwards in minutes (default $minutes): ";     
        $minutes = &get_response( "$minutes" );
        if ( ( $minutes < 0 ) || ( $minutes > 60 ) ) {
            print "\n$minutes is must be between 0 and 60!\n"; 
        } else {
            $minutes_valid = "TRUE";
        }
    }
    $cmd .= " -h";
    $cmd .= " $hours";
    $cmd .= " -m";
    $cmd .= " $minutes";
    return ($cmd);
}

###############################################################
# get_date
#
###############################################################
sub get_date {
    print "Status Checking will be performed by adding an entry to the\n";
    print "systems root crontab.\n";
    print "\n";
    print "When would you like to schedule your status checks?\n";
    print "The default is everyday at midnight.\n";
    print "\n";
    $week_day = "NO";
    while($week_day eq "NO" ) {
        print "Note: asterisk (*) matches all legal values.\n\n"; 
        print "Day of the Week [0(SUN)-6(SAT)  <* = all>, q ] : ";
        $week_day = <STDIN>;
        chomp $week_day;
        if($week_day eq "" ) {
            $week_day = '*';
        } elsif ( $week_day =~ /^q/i ) {
            return("-1");
        }
        if(!validNum(0, 6, $week_day)) {
            printf("Illegal Day of Week input\n");
            $week_day = "NO";
        } 
    }
    $week_day =~ s/^[0]+/0/;
    if ($week_day =~ /\*/) {    
        $month = "*";
        $day = "*";
    } else {
        $day = "NO";
        while($day eq "NO" ) {
            print "Day    [1-31 (* = all), <q>] : ";
            $day = <STDIN>;
            chomp $day;
            if($day eq "" || $day =~ /^q/i ) {
                return("-1");
            }
            if(!validNum(1, 31, $day)) {
                printf("Illegal Day input\n");
                $day = "NO";
            } 
        }
        $day =~ s/^[0]+//;    
        $month = "NO";
        while($month eq "NO" ) {
            print "Month    [1-12 (* = all), <q>] : ";
            $month = <STDIN>;
            chomp $month;
            if($month eq "" || $month =~ /^q/i ) {
                return("-1");
            }
            if(!validNum(1, 12, $month)) {
                printf("Illegal Month input\n");
                $month = "NO";
            } 
        }
        $month =~ s/^[0]+//;
    }
    $hour = "NO";
    while($hour eq "NO" ) {
        print "Hour    [0-23 (* = all) <0>, q] : ";
        $hour = <STDIN>;
        chomp $hour;
        if($hour eq "" )  { 
            $hour = 0;
        } elsif ( $hour =~ /^q/i ) {
            return("-1");
        }
        if(!validNum(0, 23, $hour)) {
            printf("Illegal Hour input\n");
            $hour = "NO";
        } 
    }
    $hour =~ s/^[0]+/0/;
    $minute = "NO";
    while($minute eq "NO" ) {
        print "Minute    [0-59 (* = all), <0>, q] : ";
        $minute = <STDIN>;
        chomp $minute;
        if($minute eq "") {
            $minute = 0;  
        } elsif ( $minute =~ /^q/i ) {
            return("-1");
        }
        if(!validNum(0, 59, $minute)) {
            printf("Illegal Minute input\n");
            $minute = "NO";
        } 
    }
    $minute =~ s/^[0]+/0/;
    @date = ($minute, $hour, $day, $month, $week_day);
    return (@date);
}

###############################################################
# delete_entry
#
# deletes the crontab entry for the status checking
###############################################################
sub delete_entry {
    my $task = shift;
    system_clear();
    &st_header();
    print "Menu: Delete Status Checking\n";
    print "----------------------------\n\n";

    if (!&check_crontab($task)) {
        print "${PRODUCT} $task Checking has not been enabled.\n";
    } else {
        if ( $task =~ /${STOREDGE} Messages/ ) {
            $match2 =  'mess_alert';
        } elsif ( $task =~ /${STORAGE} Status/ ) {
            $match2 =  'storconfig';
        } elsif ( $task =~ /${STORAGE} syslogd/ ) {
            $match2 =  'mess_alert';
        }
        deleteCronFileEntry("$TOOLS_CRON_FILE", $task, $match2);
        cronUpdate("$TOOLS_CRON_FILE");
        print "${PRODUCT} $task Checking has been deleted.\n";
    }
    &continue;
}

#delete obsolete STORtools comment
sub delete_obsolete {
    my $task = shift;
    deleteCronFileEntry("$TOOLS_CRON_FILE", $task );
    cronUpdate("$TOOLS_CRON_FILE");
}
###############################################################
# show_cmd
#
# show the email address, if one exists. 
###############################################################
sub show_email {
    print "\n";
    print "Email will be sent to: $email\n";
}

###############################################################
# show_date
#
# show the date in readable format
###############################################################
sub show_date {
    print "Day of the Week (0-6 0=SUN) : $_[0]\n";
    print "Month  (1-12) : $_[1]\n";
    print "Day    (1-31) : $_[2]\n";
    print "Hour   (0-23) : $_[3]\n";
    print "Minute (0-59) : $_[4]\n";
}

###############################################################
# display_entry 
#
#  
###############################################################
sub display_entry {
    my $entry = shift;
    my $obsolete = shift;

    my $line;
    my $cron_line;
    my @cron_list = `/usr/bin/cat $ROOT_CRON_FILE`;
    my $found = undef;
    foreach $line (@cron_list) {
        if (defined $found) {
            if ( !defined $obsolete ) {
                if($found eq "${STORAGE} Status") {
                    ($min, $hr, $day, $month, $week_day, $cmd, $flag0, $flag1, $flag2, $flag3, $flag4, $email) = split(/\s/,$line);
                } elsif ($found eq "${STOREDGE} Messages") {
                    ($min, $hr, $day, $month, $week_day, $cmd, $flag0, $flag1, $flag2, $flag3, $email) = split(/\s/,$line);
                } elsif ($found eq "${STORAGE} syslogd") {
                    ($min, $hr, $day, $month, $week_day, $cmd, $flag0, $flag1, $flag2, $flag3, $flag4, $email) = split(/\s/,$line);
                }
            } else {
                if($found eq "${STORAGE} Status") {
                    $cron_line = $line;
                    ($min, $hr, $day, $month, $week_day, $cmd, $flag0, $flag1, $flag2, $email) = split(/\s/,$line);
                } elsif ($found eq "${STOREDGE} Messages") {
                    $cron_line = $line;
                    ($min, $hr, $day, $month, $week_day, $cmd, $flag1, $flag2, $email) = split(/\s/,$line);
                }
            }
            print "\n$entry checking is scheduled for the following date(s):\n";
            print "\n";
            &show_date($week_day,$month,$day,$hr,$min);
            if ($email) { 
                &show_email($email);
            }
            last;
        }
        if ($line =~ /# ${PRODUCT} $entry Checking Entry/) {
            $found = $entry;
            next;
        } elsif ($line =~ /#$entry Checking Entry/) {
            $found = $entry;
            next;
        }
    }
    if (!defined $found) {
        print "\n ${PRODUCT} $entry checking has not been enabled.\n";
    }
    return $cron_line;
}

###############################################################
# help_entry
#
# prints out a help message for the date patterns
###############################################################
sub help_entry {
    system_clear();
    print <<ENDofHELP;
Setup of crontab entries to automate the Check Current Status 
operation and provide email notification on changes or failures.

The following lists descriptions of the operations you can perform.

[1] To setup a crontab entry to Enable Status Checking
[2] To delete the scheduled crontab Status entry
[3] To setup a crontab entry to send e-mail message alerts about
    exceeding outage parameters.
[4] To delete the scheduled crontab Messages Checking entry
[5] To setup a crontab entry to send e-mail message alerts about
    T300 errors and warnings.
[6] To delete the scheduled crontab T300 Messages Checking entry
[7] Check the crontab entries.

To edit the time or date, (using the instructions above) first delete
your scheduled entry, then setup a new crontab entry with the correct
date and time.
ENDofHELP
    &continue;
}
###############################################################
# continue
#
###############################################################
sub continue {
    print "\nPress <return> for previous menu ...";
    $continue = <STDIN>;
}

###############################################################
#  update the crontable with the table name supplied 
#
###############################################################
sub cronUpdate {
    my $table = shift;
    my $res = undef;
    ($res) = grep { /error/ } `/usr/bin/ksh -c 'crontab $table 2>&1'`;
    if ( defined $res ) {
        printf("Cron has detected bad format.  Refer to Man page on crontab.\n");
        return(0);
    }
    return(1);
}

###############################################################
# write string into crontab  at first blank line
#
###############################################################
sub writeCronFile {
    my ($table, $string) = @_;
    my $status = &openCronFile( $table );
    if ( $status == -1 ) {
        return $status;
    }
    my @array;
    $inserted = undef;
    while(<TABLE>) {    
        if(/#${PRODUCT}/) {
            push @array, $_;
            push @array, $string;
            $inserted = 1;
            next;
        }
        push @array, $_;
    }
    if(!defined($inserted)) {
        push @array, $string;
    }
    close(TABLE);
    unlink $table;
    if(!open(NEW, "> $table") ) {
        printf("storcron:  Fatal.  Cannot open $table\n");
        return(-1);
    }
    print NEW @array;
    close (NEW);
}
###############################################################
# delete string from crontab 
# There are two lines of data associated with each 
# crontab entry.  A comment line denoting the task, and
# a standard crontab entry. Both lines must be added and deleted 
# when an entry is changed. $string1 is the comment line pattern
# and $string2 is the crontab entry pattern.  It is assumed the
# comment line immediately proceeds the crontab line.
###############################################################
sub deleteCronFileEntry {
    my ($table, $string1, $string2 ) = @_;
    my $status = &openCronFile( $table );
    if ( $status == -1 ) {
        return $status;
    }
    my @array;
    if ( defined $string2 ) {
        while(<TABLE>) {    
            next if (/$string1/ ... /$string2/);
            push @array, $_;
        }
    } elsif ( $string1 eq "#STORtools" ) {
        while(<TABLE>) {    
            next if (/$string1/);
            push @array, $_;
        }
    }
    close(TABLE);
    unlink $table;
    if(!open(NEW, "> $table") ) {
        printf("storcron:  Fatal.  Cannot open $table\n");
        return(-1);
    }
    print NEW @array;
    close (NEW);
}
###############################################################
# validNum -- test for valid crontab entry numbers
###############################################################
sub validNum {
    my $i;
    ($low, $high, $numString) = @_;
    $numString =~ tr/\b//s;
    printf("low = $low high = $high string = [$numString]\n") if ($DEBUG);
    if($numString =~ /^\*$/) {
        return(1);
    }
    if(($numString =~ /^\d+$/) ) {
        printf("fell into digit only code\n") if ($DEBUG);
        if($numString  < $low || $numString > $high) {
            printf("Illegal number [$numString]\n");
            return(0);
        }
        return(1);
    }
    if(($numString =~ /\,/) || ($numString =~ /\-/)) {
        @a = split(/\,/, $numString);
        for($i = 0 ; $i <= $#a; $i++ ) {
            if($a[$i] =~ /-/) {
                @b = split(/-/, $a[$i]);
                for($p = 0 ; $p <= $#b; $p++ ) {
                    printf("checking $b[$p] against low = $low high = $high\n") if ($DEBUG);
                    if(($b[$p]  < $low || $b[$p] > $high) || !($b[$p] =~ /\d+/) ) {
                        printf("Illegal number [$b[$p]]\n");
                        return(0);
                    }
                }
            }
            printf("checking $a[$i] against low = $low high = $high\n") if ($DEBUG);
            if(($a[$i]  < $low || $a[$i] > $high) || !($a[$i] =~ /\d+/) ) {
                printf("Illegal number [$a[$i]]\n");
                return(0);
            }

        }
        return(1);
    } 
    return(0);
}

sub proc_cli {

    $HBA = "";

    $usage[0] = "$PROGNAME -S | -P | -A5 | -T3\n";
    $usage[1] = "S    - Select SBUS Host Adapters (default)\n";
    $usage[2] = "P    - Select PCI Host Adapters\n";
    $usage[3] = "A5   - Select A5K Series Storage (default)\n";
    $usage[4] = "T3   - Select T300 Series Storage\n";

    if ($#ARGV<0) {

        print "cli options:\n";
        foreach $item (@usage) {print $item};
        print "\n";
        die "\n$PROGNAME requires arguments.\n";
    }

    $STORAGE = "A5";
    $arg = shift @ARGV;
    while($arg) {
        if ($arg =~ /help/)  {
            die @usage if ($arg =~ /help/);
        } elsif ($arg =~ /^-S/) {
            $HBA = "S";
            check_bus_option( $HBA );
        } elsif ($arg =~ /^-P/) {
            $HBA = "P";
            check_bus_option( $HBA );
        } elsif ($arg =~ /^-A5/) {
            $STORAGE = "A5";
        } elsif ($arg =~ /^-T3/) {
            $STORAGE = "T3";
        }

        # Get the next value.
        # The last command in the loop
        $arg = shift @ARGV;
    }

    $GOLD_PATH  = "${LOGDIR}/${STORAGE}";

    if ( $HBA ne "P" && $HBA ne "S" ) {
        &get_fcal_hbas();
        &select_fcal_hbas();
    }

    print "HBA: $HBA\n" if ($DEBUG);
}

sub openCronFile()
{
    my $table = shift;
    if(!open(TABLE, "$table") ) {
        printf("storcron:  creating $table\n");
        if(!open(TABLE, "> $table") ) {
            printf("storcron:  Fatal.  Cannot create $table\n");
            return(-1);
        }
        close(TABLE);
        my $status = `/usr/bin/cat $ROOT_CRON_FILE > $table`;
        if(!open(TABLE, "$table") ) {
            printf("storcron:  Fatal.  Cannot open $table\n");
            return(-1);
        }
        printf("storcron:  created $table\n");
    }
}
