#!/usr/bin/perl
use Getopt::Std;

sub usage {
  print "Usage: dexRW -e [enc] -f <first> -c [counter_report] -w <write> -r <read> -m [mins] -l <last>  -n [no] <count> -T <test>\n";
}

$LOG="/Dex/Log";
$REP="/Dex/Report";

if (!getopts("e:c:wrm:fln:T", \%opts)) {
   usage();
   die("Abort: $Getopt::Std::ERROR \n");
}
$mins = $opts{m} || 10;
$enc  = $opts{e} || "b1";
$counter_file = $opts{c};
$count = $opts{n};

if ($opts{r}) {
  $mode = "-r";
} elsif ($opts{w}) {
  $mode = "-w";
} else {
  $mode = "-w";
}
if ($mode eq "-r") {
  $COM = "/Dex/dex -v -R16 100% 0 ${mins}m $mode -x 64k ";
} else {
  $COM = "/Dex/dex -v -R16 100% 0 ${mins}m $mode -x 64k ";
}

if ($opts{T}) {
  print "Testing mode\n";
  $testing = 1;
}



if ($opts{f}) {  # irst
  print "Running dex on first drive (c1t96d0s6).\n";
  if ($counter_file) {
     $before = &get_counters($enc);
     &sys( "$COM /dev/rdsk/c1t96d0s6 >> $LOG/first&");

     &waiting($mins);
     $after = &get_counters($enc);
     print_report($counter_file, $before, $after, $com);

  } else {
     &sys("$COM /dev/rdsk/c1t96d0s6 >> $LOG/first&");
  }
  exit;

} elsif ($opts{l}) { # last
  print "Running dex on last drive (c1t122d0s6)\n";
  if ($counter_file) {
     $before = &get_counters($enc);
     &sys( "$COM /dev/rdsk/c1t122d0s6 >> $LOG/last&");

     &waiting($mins);
     $after = &get_counters($enc);
     print_report($counter_file, $before, $after, $com);

  } else {
     &sys("$COM /dev/rdsk/c1t122d0s6 >> $LOG/last&");
  }
  exit;

} elsif ($opts{n}) { # many
  print "Running dex on $opts{n} drives.\n";
  if ($opts{n} > 22) {
    print "Usage: parm must greater than 0 and less then 23 \n";
    exit;
  }
  $cnt = 0;
  if ($counter_file) {
     $before = &get_counters($enc);
  }
  foreach $n (96,97,98,99,100,101,102,103,104,105,106,112,113,114,115,116,117,118,119,120,121,122) {
      $cnt++;
      last if ($cnt > $opts{n});     
      $dev = "/dev/rdsk/c1t${n}d0s6";
      &sys( "$COM $dev >> $LOG/$n&");
      sleep 1;
  }

  if ($counter_file) {
    &waiting($mins);
    $after = &get_counters($enc);
    print_report($counter_file, $before, $after, $COM);
  }
  
} else {
  usage();
}


sub waiting {
  my($mins) = @_;
  print "sleeping " . ($mins * 60 + 30) . " secs... \n";
  sleep($mins * 60 + 30);
}

sub sys {
  my($com) = @_;

  print $com . "\n";
  return if ($testing);
  system($com);
}

#Link Error Status information for loop:/devices/sbus@1f,0/SUNW,socal@0,0:0
#al_pa   lnk fail    sync loss   signal loss   sequence err   invalid word   CRC
#2       -1          -1          -1            -1             -1             -1          
#29      0           9           23            0              32             0           
#3a      4           303463      0             0              23609          4           

sub get_counters {
  my($enc) = @_;
  my(@SUM, $in, @x);
  print "Reading counters on $enc... \n";
  open(O, "/usr/sbin/luxadm -e rdls $enc|");
  while ($l = <O>) {
     chop($l);
     if ($l =~ /al_pa/) {
        $in = 1;
     } elsif ($in) {
        @x = split(/\s+/, $l);
        for ($c = 1; $c <= 6; $c++) {
           $SUM[$c] += $x[$c];
        }
     }
  }
  close(O);
  return \@SUM;
}

sub print_report {
   my($file, $before, $after, $com) = @_;
   print "Writing report to $REP/$file\n";
   open(O , ">$REP/$file");
   
   print O "Created " . `date` . "\n";
   print O "Dex Report ($mins mins, $count devices): $com \n\n";

   @T = (0, 'lnk_fail','sync_loss','signal_loss','sequence_err','invalid_word','CRC');

   for ($x=1; $x <= 6; $x++ ) {
      print O sprintf("%-15.15s: ", $T[$x]) .  ($after->[$x] - $before->[$x]) . "\n";
   }
   close(O);
}

