#!/usr/bin/perl
use strict;
use Data::Dumper;
use DBI;
$| = 1;

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 $bratio = 1.7;

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){
  print "\n\n";
  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 = 0;
      }
      $cboxdef = $cbox;
    }
    if($cbox =~ m/^(\w*)\./){
      $chost = $1;
    }
  } while($cboxid == 0);

  my $mounts = {};
  getcurrentmounts($cbox,"customer",\$mounts);
  #print Dumper($mounts);

  print "currently in DB:\n\n";
  my $labelprinted = 0;
  foreach my $entry (sort keys %$mounts){
    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 (sort 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 on that backup box:     $bmountcount\n";
  print "current space needed for those mounts: $bmountspace\n\n";


#  do {
    $cmountdef = "backupz";
    $cmount = promptUser("what customer mountpoint", $cmountdef);
#  }while(
  $plabeldef = $chost.".".$cmountnum."z";
  $plabel = promptUser("what partition label", $plabeldef);
  my $validhomesize = 0;
  do {
    $homesize = promptUser("how many terrabytes of home partition space will this backup", $homesize);
    unless($homesize =~ m/^[\d\.]+$/){
      print "that doesn't look like a number to me, try again\n";
    }else{
      $validhomesize = 1;
    }
  }while($validhomesize == 0);

  print "does this look correct?\n";
  print "\n$cbox, $cmount, $plabel, $homesize T, $bbox\n";
  my $response = promptUser("yes or no", "yes");
  next unless ($response =~ m/^y/i);
  addbackupmount($cboxid,$bboxid,$cmount,$plabel,$homesize);
  $cmountnum++; #so we get the next backup mount point the next time around
}




sub addbackupmount {
  my $cboxid = shift;
  my $bboxid = shift;
  my $cmount = shift;
  my $plabel = shift;
  my $homesize = shift;
  my $query = "INSERT INTO iscsimounts (cboxid, cmount, plabel, bboxid, homesize) values ( $cboxid, '$cmount', '$plabel', $bboxid, '$homesize')";
  my $sth = $dbh->prepare($query);
  $sth->execute or warn $sth->errstr;
}

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' 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 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;
}

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 $_;
}


