#!/usr/bin/perl
#-------------------------------------------------------------------------------
# cpanel - buildtree2                             Copyright(c) 2011 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
#-------------------------------------------------------------------------------
# Note:
# For actual WHM plugin publishing, this script is expected to be at
# the following location:
#     httpupdate0:/home/cpanel/cpanelsync/addons/buildtree2
#
#-------------------------------------------------------------------------------

use File::Find qw(find);
use Archive::Tar;
use Getopt::Std;

my %options = ();

getopts( "qa:f:t:r:", \%options );

# a = type of addon
# f = addon name
# t = action (bz2|lock|unlock)
# r = run type (publish|qa|test)

my $root        = undef;
my $path_prefix = undef;

if ( $options{'r'} eq '' ) {

    # Maintain the same path for compatibility with usage in previous version
    $path_prefix = "/home/cpanel";
}
elsif ( $options{'r'} =~ m/^publish$/i ) {
    $path_prefix = "/home/cpanel";
}
elsif ( $options{'r'} =~ m/^qa$/i ) {
    $path_prefix = "/home/qabuild";
}
elsif ( $options{'r'} =~ m/^test$/i ) {
    print "\"$0\" has been invoked.  No further action is performed with runtype \"$options{'r'}\".\n";
    exit;
}
else {
    die "Error: Encountered unexpected runtype \"$options{'r'}\". \"-r\" option may only be (publish|qa|test).\n";
}

$root = "$path_prefix/cpanelsync/addons";

my (@FILES);

if ( $options{'t'} eq '' ) {
    die '-t action (bz2|lock|unlock) is required';
}
if ( $options{'a'} ne "" && !-d "${root}/$options{'a'}" ) {
    die '-a may only be (cpanel|modules|scripts|whm)';
}

my ( $dir, $subdir, $tar ) = undef;

opendir( DD, $root );
while ( $dir = readdir(DD) ) {
    next if ( $dir =~ /\.bak/ );
    next if ( $dir =~ /^\./ );
    next if ( !-d "${root}/${dir}" );
    next if ( -l "${root}/${dir}" );

    next if ( $options{'a'} ne "" && $dir ne $options{'a'} );

    chdir("${root}/${dir}");
    opendir( DDL, "${root}/${dir}" );
    while ( $subdir = readdir(DDL) ) {
        next if ( $subdir =~ /^\./ );
        next if ( !-d "${root}/${dir}/${subdir}" );
        next if ( -l "${root}/${dir}/${subdir}" );

        next if ( $options{'f'} ne "" && $subdir !~ /^$options{'f'}/ );

        @FILES = ();
        if ( $options{'t'} eq "bz2" ) {
            chdir("${root}/${dir}");
            find( \&wanted, $subdir );
            $tar = Archive::Tar->new;
            $tar->add_files(@FILES);
            print "Writing new tarball ${root}/${subdir}.tar \n";
            $tar->write("${root}/${subdir}.tar");
            undef $tar;
            print "Running bzip2 -v ${root}/${subdir}.tar \n";
            system( "bzip2", "-v", "${root}/${subdir}.tar" );
            print "\n Moving ${root}/${subdir}.tar.bz2 to ${root}/${dir}/${subdir}.tar.bz2 , chdir to ${root}/${dir}/${subdir} \n";
            system( "mv", "-fv", "${root}/${subdir}.tar.bz2", "${root}/${dir}/${subdir}.tar.bz2" );
            print "chdir into ${root}/${dir}/${subdir}\n";
            chdir("${root}/${dir}/${subdir}");

            print "Calling $path_prefix/buildcpanelsync\n";
            system("$path_prefix/buildcpanelsync");

        }
        elsif ( $options{'t'} eq "lock" ) {
            print "Writing ${root}/${dir}/${subdir}/.cpanelsync.lock\n";
            open( LOCK, ">", "${root}/${dir}/${subdir}/.cpanelsync.lock" );
            print LOCK "locked";
            close(LOCK);
            next();
        }
        elsif ( $options{'t'} eq "unlock" ) {
            print "Writing ${root}/${dir}/${subdir}/.cpanelsync.lock\n";
            open( LOCK, ">", "${root}/${dir}/${subdir}/.cpanelsync.lock" );
            close(LOCK);
            next();
        }

    }
    closedir(DDL);
}
closedir(DD);

sub wanted {
    my $file = "$File::Find::dir/$_";
    return if ( $file =~ /\.bz2$/ && $file !~ /\.bz2\.bz2$/ );
    push( @FILES, $file );
}
