package cPanelSyncUtil;

use strict;
use warnings;
use File::Slurp;
use version;our $VERSION = qv('0.0.1'); 

require Exporter;
our @ISA       = qw(Exporter);
our @EXPORT_OK = qw(_raw_dir       _tuxify_pwd_recursively 
                    _get_opts_hash _sync_touchlock_pwd);

sub _tuxify_pwd_recursively {
    system 'chown', '-R', 'tux:tux', '.';
}

sub _raw_dir {
    require Archive::Tar;
    require Cwd;

    my($base, $archive, $verbose, @files) = @_;
    my $bz2_opt = $verbose ? '-kv' : '-k';
    my $pwd     = Cwd::cwd(); 
    chdir $base or return;

    my $tar = Archive::Tar->new();
    for my $file (read_dir($archive)) {
        next if $file !~ m{\.bz2\.bz2$} || $file =~ m{\.bz2$};
        $tar->add_files("$archive/$file");
    }
    $tar->write("$archive.tar");
    
    system 'bzip2', $bz2_opt, "$archive.tar";
    if(@files) {
        chdir $archive or return;
        for my $file(@files) {
            system 'bzip2', $bz2_opt, $file if -f $file;
        }
    }
    _sync_touchlock_pwd();

    chdir "$base/$archive" or return;
    _sync_touchlock_pwd();
    
    chdir $pwd or return;    
    
    1;
}

sub _get_opts_hash {
    require Getopt::Std;
    my($args, $opts_ref) = @_;
    $opts_ref = {} if ref $opts_ref ne 'HASH';
    Getopt::Std::getopts($args, $opts_ref);
    return wantarray ? %{ $opts_ref } : $opts_ref;
}

sub _sync_touchlock_pwd {
    system '/home/cpanel/buildcpanelsync';
    system 'touch', '.cpanelsync.lock';
}

1;

__END__

Do some POD Dan
