#!/usr/bin/env perl
use strict;
use warnings;
use JSON::XS;

my $eig_backup_config_file = '/opt/eig_linux/etc/eig_backup.conf';
my $mount_cache_file = '/opt/eig_linux/data/backup_mount_cache.json';
my $log_file = '/var/log/eig_backup/dr_failover.log';
my $debug = defined $ARGV[0] ? 1 : 0;

#Read in our configs
my %eig_backup_config;
open (my $config, '<', $eig_backup_config_file)
    or die "Unable to open EIG Backup config file $eig_backup_config_file for reading: $!";
while ($_ = <$config>) {
    chomp($_);
    $eig_backup_config{$1} = $2 if /^([^=]+?)=(.+)$/
}
close $config;
open(my $log, '>>', $log_file);
if (! $eig_backup_config{'dr_failover'}){
    logger("dr_failover not enabled");
    exit 1;
}
system("/opt/eig_linux/bin/updatebackupinfo") if ! -f $mount_cache_file;
die "Unable to determine home labels" if ! -f $mount_cache_file;

#Mount up the homes if not mounted
my $active_mounts = get_active_mounts();
open( my $fh, '<', $mount_cache_file);
my $backup_mounts = decode_json(<$fh>);
foreach my $mount (keys %$backup_mounts){
    my $label = $backup_mounts->{$mount}->{'plabel'};
    die ("Mount $label not found to mount!") if (! -e "/dev/disk/by-label/$label");
    (my $home_mount = $mount) =~ s/backup/home/g;
    system("mv $mount $mount.dr") if ( -d $mount);
    system("ln -s $home_mount $mount") if ( ! -e $mount); 
    if (!$active_mounts->{$mount}){
        system("mount -L $label $mount");
        logger("Mounting $label to $mount");
    }
}
$active_mounts = get_active_mounts();

#lets ensure homedirs exist and are moved over
my @users = get_cpanel_users();
foreach my $user (@users){
    my $homedir = (getpwnam $user)[7];
    my $homenum = $homedir =~ m|^(/home\d+)| ? $1 : "";
    if (!$active_mounts->{$homenum}){
        logger("No home mount found for $user:$homedir");
        next;
    }
    if (-d $homedir) {
        logger("$homedir already setup for $user");
        next;
    }
    my $backup_location = "$homenum/cpbackup/daily/$user/homedir";
    if (! -d $backup_location) {
        logger("No backup found for $user at $backup_location");
        next;
    }
    logger("Moving $backup_location to $homedir");
    system("mv $backup_location $homedir");
    system("chown $user: $homedir");
    system("chmod 755 $homedir");
}

#Misc perms and stuff
mkdir "/var/log/zabbix" if ! -d "/var/log/zabbix";
system("touch /var/log/zabbix/zabbix_agentd.log");
system("chown zabbix:zabbix /var/log/zabbix/zabbix_agentd.log");
system("service zabbix-agent restart");


#Subs now
sub get_active_mounts {
    my $mounts;
    open (my $fh, '<', '/proc/mounts');
    while (my $mount = <$fh>){
        chomp $mount;
        my ($dev, $mountpoint, $fs, $options) = split('\s+', $mount);
        next unless $mountpoint =~ m|^/home\d+|;
        $mounts->{$mountpoint} = 1;
    }
    return $mounts;
}
sub get_cpanel_users {
    my @users;
    open(my $fh, '<', '/etc/trueuserdomains');
        while(<$fh>) {
            chomp();
            push @users,$1 if ( /^.*:\s(.*)$/ );
        }
    close($fh);
    return(@users);
}
sub logger {
    my $message = shift;
    print "$message\n" if $debug;;
    print $log localtime(time) . ": $message\n";
}
