perl<<'EOF'
use strict;
use lib '/usr/local/cpanel';
use JSON;

# the placeholder text gets swapped out in EIGsh::Command::suspbackup with a sed style replacement
# when loading in code via EIGsh::LoadCode->Load()
# not sure why it doesn't just use @ARGV but that's how it's written
my $user = 'user_placeholder';
my $ticketid = 'ticket_placeholder';
my $unique = $ticketid . '_' . time();

# $server isn't used in this script, but gets used in EIGsh::Command::suspbackup
chomp(my $server = `hostname`);

# Location for legacy bh uapi, this SHOULD be universal but in the event it's not we should find locations and update accordingly
my $uapi = '/usr/bin/uapi';

if ( ! -e $uapi ) {
    print "[!] $uapi does not exist on this server. Please escalate this to have the backup created manually.\n";
    exit;
}

# Get our usage data from cPanel UAPI over using cPanel::PublicAPI
# TODO: Convert this to cPanel LIVEAPI script so we can stop using this 
my $out = qx($uapi --user=$user StatsBar get_stats display="diskusage" warnings=0 warninglevel=high warnout=0 --output=json);
$out = decode_json($out);

if ( $out->{'result'}->{'status'} == 0 ) {
        print "[!] Error getting diskuage stats from cPanel UAPI. Please escalate this to have the backup created manually.\n";
        print "[!] $out->{'result'}->{'errors'}\n";
        exit;
}

my ($data) = @{ $out->{'result'}->{'data'} };
my $size = $data->{'_count'};
my $units = $data->{'units'};

if ( $units eq 'GB' ) {
    $size = sprintf("%.2f", ( $size * 1024 ));
}

if ( $size > 10240 ) {
    print "[!] Account is over 10GB. Please escalate this to have the backup created manually.\n";
    exit();
}

if ( ! -f '/root/bin/suspbackup' ) {
    print "[!] /root/bin/suspbackup is not found on this server. Please escalate this to have the backup created manually.\n";
    exit();
}

print "[*] Account is under size limit, generating backup...\n";

qx(/root/bin/suspbackup $user -t $ticketid);
EOF
