#!/usr/bin/perl
# cpanel - autofixer/update_archive_tar_builder   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 warnings;

use Cpanel::OSSys   ();
use Cpanel::Sys::OS ();

my $version = get_cpanel_version();
exit unless ( $version eq '11.50.2.0' );

# This fix is only for Centos 5
my $ostype    = Cpanel::Sys::OS::getos();
my $osversion = Cpanel::Sys::OS::getreleaseversion();
exit unless ( ( $ostype eq 'centos' ) and ( $osversion =~ /^5/ ) );

my $arch = ( Cpanel::OSSys::uname() )[4];
if ( $arch =~ m/64/ ) {
    $arch = 'x86_64';
}
else {
    $arch = 'i386';
}

# If there already have the correct archive, then there is nothing to do
my $current_rpm_installed = `/bin/rpm -q cpanel-perl-514-Archive-Tar-Builder`;
exit if ( $current_rpm_installed =~ /cpanel-perl-514-Archive-Tar-Builder-1.9-3/ );

# Remove the problematic version
system("/bin/rpm -e --nodeps cpanel-perl-514-Archive-Tar-Builder-2.0000");

# Install the good version from our servers
system("/bin/rpm -i http://httpupdate.cpanel.net/RPM/11.46/centos/5/$arch/cpanel-perl-514-Archive-Tar-Builder-1.9-3.cp1146.$arch.rpm");

sub get_cpanel_version {
    my $version_file = '/usr/local/cpanel/version';

    if ( -e $version_file && !-z _ ) {
        open( my $fh, "<", $version_file );
        my $current_version = <$fh> || '';
        chomp $current_version;
        close $fh;

        # Remove all white space from the line.
        $current_version =~ s/\s+//msg;

        return $current_version;
    }
    else {
        return '';
    }
}
