#!/usr/bin/perl
# cpanel - abrt_argparse                          Copyright(c) 2015 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

BEGIN { unshift @INC, '/usr/local/cpanel'; }

use strict;

use Cpanel::SafeRun;

sub is_os_affected {

    # Deal with very old versions of cPanel & WHM.
    eval { require Cpanel::GenSysInfo; } or return;
    my $version = Cpanel::GenSysInfo::get_rpm_distro_version();
    return $version == 6;
}

sub has_package {
    my ($package) = @_;
    local $ENV{'LC_ALL'} = 'C';
    my $output = Cpanel::SafeRun::saferunallerrors( '/bin/rpm', '-q', $package );
    return 0 if $output =~ /not installed/;
    return 1 if $output =~ /^\Q$package\E/;
    return 0;
}

sub is_affected {
    return has_package('abrt') && !has_package('python-argparse');
}

sub fix {
    system( '/usr/bin/yum', '-y', 'install', 'python-argparse' );
    system('/usr/libexec/abrt-action-generate-machine-id');
    return !is_affected();
}

sub script {
    return 0 unless is_os_affected();
    return 0 unless is_affected();

    my $is_fixed = fix();
    print "abrt fix was " . ( $is_fixed ? "successful" : "unsuccessful" ), "\n";
    return !$is_fixed;
}

exit script() unless caller;
