#!/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 = 2 * 1.25;

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 = 1;
      }
      $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";


  print "are you sure you want to remove all .old mounts?\n";
  my $response = promptUser("yes or no", "yes");
  next unless ($response =~ m/^y/i);

  foreach my $id (keys %$mounts){
    if( $mounts->{$id}->{cmount} =~ m/old$/){
#      updatemntpoint($mounts->{$id}->{cmount},$mounts->{$id}->{plabel}) unless $mounts->{$id}->{cmount} =~ m/old$/;
#      print "ID: $id\n";
#      print Dumper($mounts->{$id});
      deletemntpoint($mounts->{$id}->{plabel});
    }
#    addbackupmount($cboxid,$bboxid,$mounts->{$id}->{cmount},$mounts->{$id}->{plabel}."x",$mounts->{$id}->{homesize});
  }
#  $cmountnum++; #so we get the next backup mount point the next time around
}

sub updatemntpoint {
  my $cmount = shift;
  my $plabel = shift;
  my $query = "UPDATE iscsimounts SET cmount = '$cmount.old' where plabel = '$plabel'";
#  print "\n\nQUERY: $query\n\n";
  my $sth = $dbh->prepare($query);
  $sth->execute or warn $sth->errstr;
}
sub deletemntpoint {
  my $plabel = shift;
  my $query = "DELETE FROM iscsimounts WHERE plabel = '$plabel'";
#  print "\n\nQUERY: $query\n\n";
  my $sth = $dbh->prepare($query);
  $sth->execute or warn $sth->errstr;
  
}

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')";
#  print "\n\nQUERY: $query\n\n";
  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 $_;
}


