#!/usr/bin/perl
use strict;
use DBI;
use CGI;
use YAML qw(Dump LoadFile);
use Data::Dumper;
my $cgi = new CGI;
print $cgi->header();
my %var = $cgi->Vars;

#my $ratio = (2 * 1.25);
my $ratio = (1);
#my $ratio = (2 * 1.01);
#my $ratio = (2 * 1.10);
#my $ratio = (2 * 1.08);

exit unless(defined($var{hostname}));
#exit unless($var{hostname} =~ m/^(\w+\.|)(\w+)\.((bluehost\.com)|(hostmonster\.com)|(fastdomain\.com)|(justhost\.com)|(beta..\.com)|(rhost..\.com)|((\w+\.|)unifiedlayer\.com))$/);

my $config_settings = LoadFile("/var/www/conf/backup_master_config.yaml") || die "Failed to load config: $!\n";
my $dbh = DBI->connect("DBI:mysql:database=".$config_settings->{backup_db}.";host=localhost",$config_settings->{backup_user}, $config_settings->{backup_pass}) or die("couldnt connect to db $!"); 

  my $mounts = {};
  getcurrentmounts($var{hostname} ,"blah",\$mounts);
  correctpartitionsize(\$mounts);

my $found = 0;
foreach my $entry( sort { $mounts->{$a}->{plabel} cmp $mounts->{$b}->{plabel}} keys %$mounts){
  $found = 1;
  $mounts->{$entry}->{need} = $mounts->{$entry}->{homesize} * $ratio;
  next if ( !$mounts->{$entry}->{backnetip} );
  $mounts->{$entry}->{backnetip} = join '.', unpack 'C4', pack 'N', $mounts->{$entry}->{backnetip};
}
print Dump $mounts;

sub correctpartitionsize {
  my $obj_ref = shift;
  foreach my $ent (sort keys %{$$obj_ref}){
    my $num = 0;
    if($$obj_ref->{$ent}->{cmount} =~ m/backup(\d+)/){
      $num = $1;
    }else{
      next;
    }
    my $query = "SELECT homesize,homeused,backupused FROM backupstatepartitions WHERE hostname = ? and pnum = ?";
    my $sth = $dbh->prepare($query);
    $sth->bind_param(1,$$obj_ref->{$ent}->{chostname}, {TYPE => &DBI::SQL_VARCHAR}) or warn $sth->errstr;
    $sth->bind_param(2,$num,{ TYPE => &DBI::SQL_INTEGER }) or warn $sth->errstr;
    $sth->execute or warn $sth->errstr;
    while(my $ref = $sth->fetchrow_hashref()){
      my $size = $ref->{homeused} * 1.5;
        $size = 480000000 if (480000000 > $size);
        $size = 550000000 if (550000000 > $size and $ref->{homesize} >  700000000);
        $size = 660000000 if (660000000 > $size and $ref->{homesize} > 1400000000);
        $size = 750000000 if (750000000 > $size and $ref->{homesize} > 1800000000);
      if(($ref->{backupused} * 1.1) > $size){
        $size = $ref->{backupused} * 1.25;
      }
      if(($ref->{homesize} * 1.5) < $size){
       $size = $ref->{homesize} * 1.5;
      }
      $$obj_ref->{$ent}->{homesize} = (int((($size * 1.12) / (1024 * 1024 * 1024))*100) / 100) * 1.25;
    }
  }
}

sub getcurrentmounts {
  my $hostname = shift;
  my $type = shift;
  my $obj_ref = shift;
  my $query = "SELECT pubkeys.pubkey,chostname,server.serverid,newbacknetip.backnetip,cmount,bhostname,plabel,homesize FROM iscsiview LEFT JOIN `server` on iscsiview.chostname = server.hostname LEFT JOIN `pubkeys` on server.serverid = pubkeys.serverid LEFT JOIN `newbacknetip` on server.serverid = newbacknetip.serverid WHERE chostname = ? or bhostname = ? ORDER BY plabel";
  my $sth = $dbh->prepare($query);
  $sth->execute($hostname, $hostname) or warn $sth->errstr;
  my $count = 0;
  while(my $ref = $sth->fetchrow_hashref()){
    $count++;
    foreach(keys %$ref){
      $$obj_ref->{$count}->{$_} = $ref->{$_};
    }
  }
  $sth->finish;
}



