#!/usr/bin/perl

use strict;

if(grep(/background/, @ARGV)){
  require Proc::Daemon;
  Proc::Daemon::Init();
}
my $hostname = `hostname`;
chomp $hostname;
my $backup = 0;
$backup = 1 if $hostname =~ m/^backup/;
my $CONFFILE = "/etc/lsi_settings.conf";
my $MEGACLI = "/usr/sbin/MegaCli64";
my $DEBUG = 0;
my $DOFIX = 1;
my $conf = {};
my @t = localtime();
my $realtime = $t[2]*60 + $t[1];

#defaults
my $defaultsettings = '

adpallinfo:Auto Rebuild:Enabled:adpautorbld enable
adpallinfo:Rebuild Rate:30%:adpsetprop RebuildRate 30
adpallinfo:PR Rate:30%:adpsetprop PatrolReadRate 30
adpgetprop bgirate:BGI Rate:30%:adpsetprop BgiRate 30
adpgetprop NCQDsply:NCQ Status is:Enabled:adpsetprop NCQEnbl
adpgetprop AutoEnhancedImportDsply:Auto Enhanced Import is:Enabled:adpsetprop AutoEnhancedImportEnbl

AdpCcSched info:Operation Mode:Disabled:AdpCcSched dsbl
adppr info:Patrol Read Mode:Disabled:adppr dsbl

AdpBIOS dsply:BIOS will:Bypass error.:AdpBIOS be

DirectPdMapping dsply:Direct Pd Mapping is:Disabled:DirectPdMapping dsbl

';

if($DOFIX){
  print "changing settings we never ever want set differently!\n" if $DEBUG;
  runfix("$MEGACLI adpsetprop defaultldpspolicy none aall nolog");
  runfix("$MEGACLI ldsetpowerpolicy none lall aall nolog");
  if($backup){
    runfix("$MEGACLI ldsetprop CachedBadBBU lall aall nolog");
    runfix("$MEGACLI ldsetprop DisDskCache lall aall nolog");
    runfix("$MEGACLI ldsetprop wb lall aall nolog");
    runfix("$MEGACLI ldsetprop direct lall aall nolog");
    $defaultsettings .= 'adpgetprop BootWithPinnedCache:Boot with Pinned Cache :Enabled:adpsetprop BootWithPinnedCache 1';
  }
}


loadconf($defaultsettings);

if( -e $CONFFILE ){
  open (CONF, "<$CONFFILE");
  my @fromconf = <CONF>;
  close(CONF);
  loadconf(join("\n",@fromconf));
}

if($DEBUG){
  require Data::Dumper;
  print Dumper($conf);
}

#get number of lsi cards
my $ccount = getadpcount();

my $errors = checkadapters();
if($DOFIX && $errors ne ""){
  $errors = checkadapters();
}
print $errors;
exit 1 if $errors;

##### subs only below this line
sub loadconf {
  my $data = shift;  
  my $skiplines = 0;
  foreach(split('\n',$data)){
    next if m/^\s*#/;
    next if m/^\s*$/;
    chomp;
    if(m/timeframe\s*(\d+):(\d+)-(\d+):(\d+)/){
        my $stime = $1*60+$2;
        my $etime = $3*60+$4;
      if($realtime > $stime and $realtime < $etime){
        $skiplines = 0;
      }else{
        $skiplines = 1;
      }
      next;
    }elsif($skiplines){
      next;
    }
    my @p = split(":");
    my $cmd = lc($p[0]);
    $conf->{$cmd}->{$p[1]}->{expected}=$p[2];
    $p[3] =~ m/(a-z0-9\s)/i;
    my $fix = $1;
    $conf->{$cmd}->{$p[1]}->{fix}=$p[3];
  }
}


sub checkadapters {
  my @errors;
  for my $adp (0 .. ($ccount - 1)){
    print "ADAPTER $adp\n" if $DEBUG;
    foreach my $cmdp (keys %$conf){
      my $cmd = "$MEGACLI $cmdp a$adp nolog";
      print "\n\n$cmd\n" if $DEBUG;
      my $response = `$cmd`;
      foreach my $setting (keys %{$conf->{$cmdp}}){
        $response =~ m/$setting\s*:?=?\s*(.*)\n/;
        print "SETTING: $setting\n" if $DEBUG;
        my $found = $1;
        my $expected = $conf->{$cmdp}->{$setting}->{expected};
        if($found =~ m/$expected/){
          $conf->{$cmdp}->{$setting}->{correct} = 1;
          print "CORRECT\n" if $DEBUG;
        }else{
          push(@errors, "a$adp $setting ne ".$conf->{$cmdp}->{$setting}->{expected});
          print "$setting incorrect\n" if $DEBUG;
          print "FIXING\n" if $DEBUG;
          runfix("$MEGACLI ".$conf->{$cmdp}->{$setting}->{fix}." a$adp nolog") if $DOFIX;
        }
      }
    }
  }
  return join(" ", @errors) if @errors;
  return "";
}

sub runfix {
  my $cmd = shift;
  print "Running $cmd\n" if $DEBUG;
  my $response = `$cmd`;
  print $response if $DEBUG;
}

sub getadpcount {
  my $cmd = "$MEGACLI adpcount nolog";
  my $response = `$cmd`;
  return $1 if($response =~ m/Controller Count: (\d+)/);
  return 0;
}


