#!/usr/bin/perl
use strict;
use DBI;
my $bratio = 2 * 1.25;

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 $cbox = "";
my $chost = "";
my $cboxdef = "box231.bluehost.com";
my $cboxid = 0;
my $bbox = "";
my $bboxdef = "backup1.bluehost.com";
my $bboxid = 0;
my $cmountnum = 0;
my $cmount = "";
my $cmountdef = "/backup";
my $plabel = "";
my $plabeldef = "";
my $homesize = 0;
my $homesizedef = 0;




print "\nctrl-c or q on any question will quit\n";
while(1){
#get customer boxes to add
  do {
    $cbox = promptUser("what customer box",$cboxdef);
    $cboxid = getserverid($cbox);
    if($cboxid == 0){
      print "I couldn't get a serverid # for that hostname\n\n";
    }else{
      if($cbox ne $cboxdef){
        $cmountnum = 1;
      } 
      $cboxdef = $cbox;
    } 
    if($cbox =~ m/^(\w*)\./){
      $chost = $1;
    } 
  } while($cboxid == 0);

  my $mounts = {};
  getcurrentmounts($cbox,"customer",\$mounts);
  #print Dumper($mounts);
  my $cmountspace = 0;
  print "currently in DB:\n\n";
  my $labelprinted = 0;
  foreach my $entry (keys %$mounts){
    $cmountspace += ($mounts->{$entry}->{homesize} * $bratio);
    unless($labelprinted == 1){
      foreach(sort keys %{$mounts->{$entry}}){
        print "\t$_";
      }
      print "\n";
      $labelprinted = 1;
    }
    foreach(sort keys %{$mounts->{$entry}}){
      print "\t".$mounts->{$entry}->{$_};
    }
    print "\n";
  }
  print "\n";
  do {
    $bbox = promptUser("what backup box", $bboxdef);
    $bboxid = getserverid($bbox);
    if($bboxid == 0){
      print "I couldn't get a serverid # for that hostname\n";
    }else{
      $bboxdef = $bbox;
    }
  } while($bboxid == 0);
  my $bmounts = {};
  getcurrentmounts($bbox,"backup",\$bmounts);
#  print "currently in DB:\n\n";


#  $labelprinted = 0;
  my $bmountcount = 0 ;
  my $bmountspace = 0 ;
  foreach my $entry (keys %$bmounts){
    $bmountcount++;
    $bmountspace += ($bmounts->{$entry}->{homesize} * $bratio);
#    unless($labelprinted == 1){
#      foreach(sort keys %{$bmounts->{$entry}}){
#        print "\t$_";
#      }
#      print "\n";
#      $labelprinted = 1;
#    }
#    foreach(sort keys %{$bmounts->{$entry}}){
#      print "\t".$bmounts->{$entry}->{$_};
#    }
#    print "\n";
  }
  print "\n";
  print "current mounts allocated to that backup box:     $bmountcount\n";
  print "current space needed for those mounts: $bmountspace T\n\n";
  print "additional space needed for this customer box: $cmountspace T\n\n ";
  print "total space needed on this backup box for you to say \"yes\": ".( $cmountspace + $bmountspace)." T\n\n";


  print "does this look correct?\n";
  my $response = promptUser("yes or no", "yes");
  if($response =~ m/^y(es)?/){
    forcechangecustbox($cboxid, $bboxid);
    print "done\n";
  }else{
    print "no changes made!\n";
  }
}

sub forcechangecustbox {
  my $cboxid = shift;
  my $bboxid = shift;
  my $query = "UPDATE iscsimounts SET bboxid = $bboxid WHERE cboxid = $cboxid and (bboxid = 1341 or bboxid = 1342 or bboxid = 1343)";
  my $sth = $dbh->prepare($query);
  $sth->execute or warn $sth->errstr;
  $sth->finish;
}

sub getserverid {
  my $name = shift;
  my $query = "SELECT serverid,hostname,host FROM server WHERE hostname = '$name'";
  my $sth = $dbh->prepare($query);
  $sth->execute or warn $sth->errstr;
  if(my $ref = $sth->fetchrow_hashref()){
    $sth->finish;
    return $ref->{serverid};
  }else{
    return 0;
  }
}
sub promptUser {
   my $promptString = shift;
   my $default = shift;
   print $promptString, " ( $default ) : ";
   $| = 1;               # force a flush after our print
   $_ = <STDIN>;         # get the input from STDIN (presumably the keyboard)
   chomp;
   $_ = $default if($_ eq "");
   exit if($_ eq "q");
   return $_;
}
sub getcurrentmounts {
  my $hostname = shift;
  my $type = shift;
  my $obj_ref = shift;
  my $query = "SELECT chostname,cmount,bhostname,plabel,homesize FROM iscsiview WHERE chostname = '$hostname' and bhostname like 'notsetup%' ORDER BY cmount";
  $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 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;
}

