#!/usr/bin/env perl

use strict;
use YAML qw( Load Dump LoadFile DumpFile );
use Sys::Hostname;

use FindBin qw($Bin);
use lib "$Bin/../lib";
use BackupServer;

my $bk = new BackupServer;

my $hostname = hostname;
my $date = time();

print "Checking in with backupmaster...\n";
system("$Bin/updatelocal") == 0 or die "Update from backupmaster failed: $!\n";
my $current_config = $bk->load_partitions();
print "\nConfiguring the following volumes:\n\n";
my %list;
foreach my $key ( sort { $$current_config{$a}{plabel} cmp $$current_config{$b}{plabel} } keys %$current_config ) {
  push @{ $list{$$current_config{$key}{chostname}} }, $$current_config{$key}{plabel};
}
foreach my $host ( keys %list ) {
  print "$host: " . join(', ', @{ $list{$host} } ) . "\n";
}
print "\nContinue? (y|N): ";
exit unless ( <STDIN> =~ m/^y/i );

#Partition Drives
my $partition_script = $bk->get_config_value('type') eq "zfs" ? "setupzfsslices" : "partitiondrives";
system("$Bin/$partition_script");
if ( $? == 0 ) {
  print "\n\nWould you like to create these partitions? (y|N): ";
  exit unless ( <STDIN> =~ m/^y/i );
  print "Partitioning drives...\n";
  system("$Bin/$partition_script run") == 0 or die "Failed to run $partition_script: $!\n";
} elsif ( $? == 512 ) {
  print "All partitions already created\n";
} else {
  die "Bad exit code from $partition_script: $!\n";
}

#Format Partitions
if($bk->get_config_value('type') ne "zfs"){
  system("$Bin/formatthemall");
  if ( $? == 0 ) {
    print "\n\nWould you like to format these partitions? (y|N): ";
    exit unless ( <STDIN> =~ m/^y/i );
    print "Formatting drives...\n";
    system("$Bin/formatthemall run");
  } elsif ( $? == 512 ) {
    print "All partitions already formatted\n";
  } else {
    die "Bad exit code from formatthemall: $!\n";
  }
}

#Setup directories, mount and exports
if ($bk->get_config_value('type') ne "zfs"){
  system("$Bin/setupexports");
  if ( $? == 0 ) {
    print "\n\nWould you like to setup these directories, exports and mounts? (y|N): ";
    exit unless ( <STDIN> =~ m/^y/i );
    print "Setting up exports...\n";
    system("$Bin/setupexports run") == 0 or die "Failed to run setupexports $!\n";
  } elsif ( $? == 512 ) {
    print "All exports already set up\n";
  } else {
    die "Bad exit code from setupexports: $!\n";
  }
} else {
    print "ZFS - Skipping exports! \n";
}

if ($bk->get_config_value('puppet_master') eq 'henson'){
  print "Running puppet to update firewalls and pubkeys\n\n";
  system("puppet agent -t --tags firewall,sshd_eig");
} else {
  #Setup pubkey
  print "Checking pubkeys\n";
  system("$Bin/setuppubkeys run") == 0 or die "Failed to run setuppubkeys $!\n";

  #Setup iptables
  system("$Bin/setupiptables");
  if ( $? == 0 ) {
    print "\n\nWould you like to setup these new iptables rules? (y|N): ";
    exit unless ( <STDIN> =~ m/^y/i );
    print "Setting up iptables...\n";
    system("$Bin/setupiptables run") == 512 or die "Failed to run setupiptables $!\n";
  } elsif ( $? == 512 ) {
    print "Iptables already set up\n";
  } else {
    die "Bad exit status from setupiptables: $!\n";
  }
}

print "\nBackup Setup Complete!!!\n\n";
