#!/usr/bin/perl

# cpanel - cp_util/test_setup.pl                  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

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

use strict;
use warnings;

use File::Find ();
use YAML       ();

my $CPANEL = '/var/cpanel';
my $cwd    = `pwd`;
chomp $cwd;

my $source_dir = $cwd;
if ( $source_dir =~ m{/t$} ) {
    $source_dir =~ s{/t$}{};
}
if ( !-d "$source_dir/.git" or !-d "$source_dir/Cpanel/Easy" ) {
    die "'$source_dir' does not appear to be an EasyApache git repository";
}
chdir $source_dir;

my $easy_md5_dir = "$CPANEL/easy/apache/path_md5";

setup_clean_start();
setup_easy_root();

if ( open my $targz_fh, '<', "$source_dir/targz.yaml" ) {
    my $yaml = join( '', <$targz_fh> );
    close $targz_fh;
    my $targz = ( YAML::Load($yaml) )[0];

    foreach my $path ( keys %$targz ) {
        my $md5sum   = $targz->{$path};
        my $md5_path = $path;
        $md5_path =~ s{/}{_slash_}g;
        $md5_path =~ s{^_}{};
        $md5_path = "$easy_md5_dir/$md5_path";
        if ( open my $md5_fh, '>', $md5_path ) {
            print {$md5_fh} "$md5sum\n";
            close $md5_fh;
        }
        else {
            die "Couldn't open $md5_path for writing: $!";
        }
    }
}

sub setup_clean_start {
    system 'touch', "$CPANEL/easy_no_targz_yaml", "$CPANEL/easy_skip_cpanelsync";
    unlink "$CPANEL/easy/apache/state.yaml" if -e "$CPANEL/easy/apache/state.yaml";
}

sub setup_easy_root {
    my $easy_root = "$CPANEL/perl/easy";
    my $rsync = ( map { -x $_ && $_ } qw( /usr/bin/rsync /usr/local/bin/rsync ) )[0];

    # don't bother doing anything unless rsync available
    die "Missing rsync dependency" unless defined $rsync;

    # setup directories if they're not there already
    system 'mkdir', '-p', "$easy_root/Cpanel" unless -d "$easy_root/Cpanel";
    system 'mkdir', '-p', $easy_md5_dir       unless -d $easy_md5_dir;

    print "Installing EasyApache code from git working directory $source_dir to $easy_root ...\n";

    # Start sync'ing (hopefully we don't get any errors because I'm not
    # checking for them right now)
    # NOTE: --delete is the magic sauce for removing extraneous files that
    #       we aren't copying over (e.g. deprecated files).  Only do this on
    #       this first rsync.  It was chosen because this is the largest
    #       sync we're going to do.  The other copies don't take much more
    #       than a second or two to re-copy each time.
    my @ex = map { ( '--exclude', "$_" ) } qw( *.tar.gz.d *.ptar *.gitignore );
    system $rsync, '-acXsq', '--delete', '--delete-excluded', @ex, "$source_dir/targz/Cpanel", "$easy_root/";
    system $rsync, '-acXsq', "$source_dir/Cpanel/", "$easy_root/Cpanel/";
    system 'mkdir', '-p', "$CPANEL/easy" unless -d "$CPANEL/easy";
    system $rsync, '-acXsq', "$source_dir/profiles/easy/", "$CPANEL/easy/";
    system 'mkdir', '-p', "$CPANEL/rpm.versions.d" unless -d "$CPANEL/rpm.versions.d";
    system $rsync, '-acXsq', "$source_dir/profiles/rpm.versions.d/", "$CPANEL/rpm.versions.d/";
}
