#!/usr/bin/perl
use Data::Dumper;
my $cli = "/usr/sbin/MegaCli";
my $adp = "a0";
my $data = {};

my $enc = "";
my $slots = "";
my $pos = "";
foreach(`$cli encinfo $adp`){
  chomp;
  my($key,$value) = split('\s*:\s*');
  if($key =~ m/Device ID/){
    $enc = $value;
  }elsif($key =~ m/Number of Slots/){
    $slots = $value;
  }elsif($key =~ m/Position/){
    $pos = $value;
  }elsif($key =~ m/Connector Name/){
    #here is where we map it
    $data->{$value}->{$pos}->{name} = $enc;
    $data->{$value}->{$pos}->{slots} = $slots;
#  }else{
#    print "$key => $value\n";
  }
}
#print Dumper($data);

foreach my $con (sort keys %$data){
  next if $con eq "Unavailable";
  print "#connector: $con\n";
#  print Dumper($data->{$con});
  my $prevslots = 0;
  my $prevencl = 0;
  foreach my $pos (sort keys %{$data->{$con}}){
#    print "\nPos: $pos Prev slots: $prevslots Prev Encl: $prevencl\n";
    print "\n# enclosure (".$data->{$con}->{$pos}->{name}.") at $con => position $pos has ".$data->{$con}->{$pos}->{slots}." slots\n";

    if($prevslots == 24 and $data->{$con}->{$pos}->{slots} == 24){  #prev encl was 24 ports and we are 24 ports, prev encl was it's own chassis
      make24port($prevencl);
      $prevencl = $data->{$con}->{$pos}->{name};
      $prevslots = $data->{$con}->{$pos}->{slots};
    }elsif($prevslots == 24 and $data->{$con}->{$pos}->{slots} == 12){  #prev encl was 24 ports and we are 12 ports, we are a 36 port chassis
      make36port($prevencl,$data->{$con}->{$pos}->{name});
      $prevencl = 0;
      $prevslots = 0;
    }elsif($prevslots == 24 and $data->{$con}->{$pos}->{slots} == 21){  #prev encl was 24 ports and we are 21 ports, we are a 45 port chassis
      make45port($prevencl,$data->{$con}->{$pos}->{name});
      $prevencl = 0;
      $prevslots = 0;
    }elsif($prevslots == 0 and $data->{$con}->{$pos}->{slots} == 24 ){ #we are a front chassis and haven't seen the back yet
      $prevencl = $data->{$con}->{$pos}->{name};
      $prevslots = $data->{$con}->{$pos}->{slots};
    }elsif($prevslots == 0){
      print "\n# Something is cabled wrong!\n";
      print "# our previous enclosure was configured or we are starting the chain and yet the enclosure has ".$data->{$con}->{$pos}->{slots}." slots\n";
      $prevencl = $data->{$con}->{$pos}->{name};
      $prevslots = $data->{$con}->{$pos}->{slots};
    }else{
      print "\n# Something feels wrong!\n";
      print "# the previous enclosure has $prevslots slots and this one has ".$data->{$con}->{$pos}->{slots}." slots\n";
      $prevencl = $data->{$con}->{$pos}->{name};
      $prevslots = $data->{$con}->{$pos}->{slots};
    }
  }
  if($prevencl != 0 and $prevslots == 24){  #did we miss an enclosure because they were all 24 porters?
    make24port($prevencl);
  }
  
}

sub make24port {
  my $front = shift;
  print "\n# configuring $front as 24 port chassis\n";
  runcmd("$cli CfgLdAdd r5[$front:0,$front:1,$front:2,$front:3,$front:4,$front:5] $adp");
  runcmd("$cli CfgLdAdd r5[$front:6,$front:7,$front:8,$front:9,$front:10,$front:11] $adp");
  runcmd("$cli CfgLdAdd r5[$front:12,$front:13,$front:14,$front:15,$front:16] $adp");
  runcmd("$cli CfgLdAdd r5[$front:17,$front:18,$front:19,$front:20,$front:21] $adp");
  #two spares on front
}
sub make36port {
  my $front = shift;
  my $back = shift;
  print "\n# configuring $front and $back as 36 port chassis\n";
  runcmd("$cli CfgLdAdd r5[$front:0,$front:1,$front:2,$front:3,$front:4,$front:5] $adp");
  runcmd("$cli CfgLdAdd r5[$front:6,$front:7,$front:8,$front:9,$front:10,$front:11] $adp");
  runcmd("$cli CfgLdAdd r5[$front:12,$front:13,$front:14,$front:15,$front:16,$front:17] $adp");
  runcmd("$cli CfgLdAdd r5[$front:18,$front:19,$front:20,$front:21,$front:22] $adp");
  #one spare on front
  runcmd("$cli CfgLdAdd r5[$back:0,$back:1,$back:2,$back:3,$back:4,$back:5] $adp");
  runcmd("$cli CfgLdAdd r5[$back:6,$back:7,$back:8,$back:9,$back:10] $adp");
  #one spare on back
}
sub make45port {
  my $front = shift;
  my $back = shift;
  print "\n# configuring $front and $back as 45 port chassis\n";
  runcmd("$cli CfgLdAdd r5[$front:0,$front:1,$front:2,$front:3,$front:4,$front:5] $adp");
  runcmd("$cli CfgLdAdd r5[$front:6,$front:7,$front:8,$front:9,$front:10,$front:11] $adp");
  runcmd("$cli CfgLdAdd r5[$front:12,$front:13,$front:14,$front:15,$front:16,$front:17] $adp");
  runcmd("$cli CfgLdAdd r5[$front:18,$front:19,$front:20,$front:21,$front:22,$front:23] $adp");

  runcmd("$cli CfgLdAdd r5[$back:0,$back:1,$back:2,$back:3,$back:4,$back:5] $adp");
  runcmd("$cli CfgLdAdd r5[$back:6,$back:7,$back:8,$back:9,$back:10,$back:11] $adp");
  runcmd("$cli CfgLdAdd r5[$back:12,$back:13,$back:14,$back:15,$back:16,$back:17] $adp");
  #2 spares on back
  #1 bay empty
}
sub runcmd {
  my $cmd = shift;
  print "$cmd\n";
#  system($cmd);
}

