#!/usr/bin/perl
use strict;
use DBI;
use CGI;
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+)\.((bluehost\.com)|(hostmonster\.com)|(fastdomain\.com)|(justhost\.com)|(beta..\.com)|(rhost..\.com))$/);

use YAML qw( LoadFile );
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 $serverid = getserverid($var{hostname});

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

#print Dumper($mounts);
#  foreach my $uid( sort{ $tmphash->{$b} <=> $tmphash->{$a}} keys %$tmphash){
my $found = 0;
foreach my $entry( sort { $mounts->{$a}->{plabel} cmp $mounts->{$b}->{plabel}} keys %$mounts){
  $found = 1;
#  print "entry: $entry\n";
#  my $need = sprintf("%4f",$mounts->{$entry}->{homesize} * $ratio);
#  $need =~ s/[\.?0]+$/ /g;
  my $need = $mounts->{$entry}->{homesize} * $ratio;

  printf("Label: %10s   Size:  %-4s T\n",$mounts->{$entry}->{plabel},$need);
#  print "Label: ".$mounts->{$entry}->{plabel}." Size:  ".$need." T\n";
#  print Dumper($mounts->{$entry})."\n\n";

}
print "None\n" if $found == 0;

sub correctpartitionsize {
  my $obj_ref = shift;
  foreach my $ent (sort keys %{$$obj_ref}){
#    print Dumper($$obj_ref->{$ent});
    $$obj_ref->{$ent}->{cmount} =~ m/backup(\d+)/;
    my $num = $1;
    next unless $num;
#    print $$obj_ref->{$ent}->{chostname}." ".$$obj_ref->{$ent}->{cmount}." $num\n";
    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()){
#      print join(" - ",$ref->{homesize},$ref->{homeused},$ref->{backupused})."\n";
#print "before : ".$ref->{homesize}."\n";
      my $size = $ref->{homeused} * 1.5;
#      if(480000000 > $size){
        $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;
      }
#      print "int $size\n";
      $$obj_ref->{$ent}->{homesize} = (int((($size * 1.12) / (1024 * 1024 * 1024))*100) / 100) * 1.25;
#      $$obj_ref->{$ent}->{homesize} = int((($size * 1.12) / (1024 * 1024 * 1024))*100) / 100;
#      print "result: ".$$obj_ref->{$ent}->{homesize}."\n";
    }
  }
}

sub getcurrentmounts {
  my $hostname = shift;
  my $type = shift;
  my $obj_ref = shift;
  my $query = "SELECT chostname,cmount,bhostname,plabel,homesize FROM iscsiview WHERE chostname = ? or bhostname = ? ORDER BY plabel";
#  $query = "SELECT chostname,cmount,bhostname,plabel,homesize FROM iscsiview WHERE bhostname = '$hostname' ORDER BY plabel" if $type eq "backup";
#  print "$query\n";
  my $sth = $dbh->prepare($query);
  $sth->execute($hostname, $hostname) or warn $sth->errstr;
  my $count = 0;
  while(my $ref = $sth->fetchrow_hashref()){
    $count++;
#    print Dumper($ref);
    foreach(keys %$ref){
      $$obj_ref->{$count}->{$_} = $ref->{$_};
    }
  }
  $sth->finish;
}



