#!/usr/bin/perl
# cpanel - IonCubeLoader-AutoUpdater              Copyright(c) 2013 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
#
# This script will download the new packages, untar the old packages, copy the cpanel-* files,
# and retar with new download/cpanel-* files.
#
# Usage:
#    --verbose        Verbose output

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

use strict;
use warnings;
use YAML ();

my $verbose = 0;

if ( grep( /^-?-verbose/, @ARGV ) ) {
    $verbose = 1;
    @ARGV = grep( !/^-?-verbose/, @ARGV );
}

# Path setup
my $cwd = `pwd`;
chomp $cwd;
my ($source_dir) = $cwd =~ m{^(.*?easy(?:apache)?)/?.*$}i;    # git repo root dir

if ( !-d "$source_dir/.git" or !-d "$source_dir/Cpanel/Easy" ) {
    die "'$source_dir' does not appear to be an EasyApache git repository";
}

my $path     = "$source_dir/targz/Cpanel/Easy";               # where the ioncubes lounge
my $path_tmp = "$path/ioncube_tmp";                           # work directory
my $pkg_path = '/var/cpanel/perl/easy/Cpanel/Easy';

my $pkgs = {
    'IonCubeLoader.pm.tar.gz.freebsd-4-32' => 'http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_fre_4_x86.tar.gz',
    'IonCubeLoader.pm.tar.gz.freebsd-6-32' => 'http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_fre_6_x86.tar.gz',
    'IonCubeLoader.pm.tar.gz.freebsd-6-64' => 'http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_fre_6_x86-64.tar.gz',
    'IonCubeLoader.pm.tar.gz.freebsd-7-32' => 'http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_fre_7_x86.tar.gz',
    'IonCubeLoader.pm.tar.gz.freebsd-7-64' => 'http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_fre_7_x86-64.tar.gz',
    'IonCubeLoader.pm.tar.gz.freebsd-8-32' => 'http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_fre_8_x86.tar.gz',
    'IonCubeLoader.pm.tar.gz.freebsd-8-64' => 'http://downloads2.ioncube.com/loader_downloads/ioncube_loaders_fre_8_x86-64.tar.gz'
};

my @cP_files = qw( cpanel-install cpanel-status cpanel-uninstall );

system( "rm", "-rf", $path_tmp ) if ( -d $path_tmp );
mkdir($path_tmp)        or die "Cannot mkdir $path_tmp: $!";
mkdir("$path_tmp/done") or die "Cannot mkdir $path_tmp/done: $!";
chdir($path_tmp)        or die "Cannot chdir to $path_tmp: $!";

for ( sort keys(%$pkgs) ) {
    my $key = $_;             # Name of file
    my $url = $pkgs->{$_};    # URL to grab new update

    print "Repackaging $key\n";

    # Create temp directory of version we're repackaging
    mkdir($key);
    chdir($key);

    if ($verbose) {
        system( "wget", $url );
    }
    else {
        system( "wget", "--quiet", $url );
    }

    # Get filename from URL
    my ($downloaded_file) = $url =~ m{^.*/(\w.*)$}i;
    if ( !-e $downloaded_file || -z _ ) {
        die "Problem with $downloaded_file. Does not exist or is zero bytes";
    }

    # Untar new file
    print "Untarring $downloaded_file.\n" if $verbose;
    system( "tar", "xzf", $downloaded_file );

    # Back up one directory
    chdir('..');

    # Untar old file and copy cpanel installers to new directory
    print "Untarring old $key and copying cpanel scripts to the downloaded tarball extracted.\n" if $verbose;
    system( "tar", "xzf", "../$key" );
    for (@cP_files) {
        system( "cp", "ioncube/$_", "$key/ioncube" );
    }

    # Delete old local copy tmp untar
    print "Cleaning up old files\n" if $verbose;
    system( "rm", "-rf", "ioncube" );

    # Tar new, get md5sum new file, move to $path_tmp/done
    print "Tar gz'ing new package and saving in $path_tmp\n" if $verbose;
    chdir($key);
    system( "tar", "czf", $key, "ioncube" );
    my $md5sum = `md5sum $key | cut -d " " -f1`;
    system( "mv", "$key", "../../" );

    # Update the %$pkgs url with the md5sum of the new tarball
    ( $pkgs->{$key} ) = $md5sum;

    chdir($path_tmp);
    print "Cleaning up old files\n" if $verbose;
    system( "rm", "-rf", "$key" );
}

print "Updating targz.yaml\n" if $verbose;

foreach my $md5 ( keys(%$pkgs) ) {
    chomp( $pkgs->{$md5} );
}

system( "rm", "-rf", $path_tmp );
print "Done.\n\n";
exit;
