#!/usr/bin/perl

use strict;
use Getopt::Long;
use Sys::Hostname;
use YAML qw( Load );

my $local_hostname = hostname;
my ($host,$source_ip,$cmd);
GetOptions ("host=s"            => \$host),
  or die("Error in command line arguments\n");
&usage and exit if ( !$host );
die "Invalid host $host\n" if ( $host !~ m/^([\w\d\-\_]+)/);

my $blob = `wget -O - -q http://backupmaster.bluehost.com/cgi-bin/getbackupserverlist?hostname=$local_hostname`;
my $local_data = Load($blob);
my %local_mounts;
foreach my $entry ( keys %$local_data ) {
  $local_mounts{$$local_data{$entry}{chostname}} = 1;
}
if ( !$local_mounts{$host} ) {
  die "Refusing to convert $host which is not local according to backupmaster\n";
}

$source_ip = `wget --timeout=10 -t 1 -q -O - 'http://74.220.198.194/cgi-bin/newbacknetip?hostname=$host'`;
die "Invalid source_ip $source_ip\n" if ( $source_ip !~ m/^\d+\.\d+\.\d+\.\d+$/ );
my $shorthost = $1 if ( $host =~ m/^([\w\d\-]+)\./ );
die "Couldn't determine short hostname from $host\n" unless ($shorthost);

print "Starting conversion for $shorthost\n";

# Get list of partitions for the host
my %partitions = ();
foreach my $line ( `ls /dev/backup` ) {
    chomp $line;
    if ( $line =~ m/^$shorthost\.(\d+)\w$/ ) {
        $partitions{$1} = $line;
    }
}

# Load current fstab
open(FSTAB, "+</etc/fstab");
my %entries;
foreach my $line (<FSTAB>) {
    $entries{$1} = 1 if ($line =~ m/^LABEL=([\w\d\.\-\_]+)\s/);
}

# Load current exports
open(EXPORTS, "+</etc/exports");
my %exports;
foreach my $line (<EXPORTS>) {
    $exports{$host} = 1 if ($line =~ m/^\/home\/$host\s+$source_ip/);
    $exports{"$host$1"} = "exists" if ($line =~ m/^\/home\/$host\/backup(\d+)\s+$source_ip/);
}
if ( !$exports{$host} ) {
    print "Adding base export for host to /etc/exports\n";
    print EXPORTS "/home/$host $source_ip(ro,fsid=0,no_subtree_check,sync)\n";
}

my $mount_all = 0;

foreach my $partition ( keys %partitions ) {
    print "$host:/backup$partition -> $partitions{$partition} \n";

    # Make mount points
    $cmd = "mkdir -p /home/$host/backup$partition";
    system($cmd) == 0 or die "Command failed: $!\n";

    # Add entries to fstab
    if ( !$entries{$partitions{$partition}} ) {
        print "Adding $partitions{$partition} to fstab\n";
        print FSTAB "LABEL=$partitions{$partition}		/home/$host/backup$partition		ext4	defaults,noatime,nodiratime	0 0\n";
        $mount_all = 1;
    }

    # Add entries to exports
    if ( !$exports{"$host$partition"} ) {
        print "Adding /home/$host/backup$partition $source_ip(ro,sync,no_root_squash) to exports\n";
        print EXPORTS "/home/$host/backup$partition $source_ip(ro,sync,no_root_squash)\n";
    }
}

close(FSTAB);
close(EXPORTS);

# Mount partitions if needed
if ( $mount_all ) {
    system("mount -a") == 0 or die "Failed to mount all partitions: $!\n";
}

# Load exports
system("exportfs -a") == 0 or die "Failed to export all: $!\n";

# Setup pubkeys and SSH
system("/opt/eig_linux/bin/setuppubkeys") == 0 or die "Failed to setup pubkeys: $!\n";

# Update targets.conf to remove mounted systems
system("/opt/eig_linux/bin/createtargetsfile") == 0 or die "Failed to setup targets.conf: $!\n";

sub usage {
print qq{$0 usage:

     $0 --host=<remote hostname>

};
}

