#!/usr/local/cpanel/3rdparty/bin/perl
use lib '/usr/local/cpanel';
use Cpanel::Version;
use version;
use File::Copy;
use File::Path qw(mkpath);

my $feature_list_update_log = '/var/cpanel/logs/feature_list_update_' . time() . '.log';

my $LOG_LEVEL = 'terse';    # verbose | terse

print "\nRunning pre-checks before installing autoinstallssl plugin...\n";

# make sure we're running as root
if ( $< != 0 ) {
    print "\nError: You must be root to install autoinstallssl plugin.\n";
    exit(0);
}

# check if the version is valid to install plugin
if ( !valid_cpanel_version_for_plugin() ) {
    exit 1;
}

my $theme_base = '/usr/local/cpanel/base/frontend/default';
my $theme_base1 = '/usr/local/cpanel/base/frontend/x3';
my $theme_base2 = '/usr/local/cpanel/base/frontend/paper_lantern';

print_line_break();

# check for previous installation
uninstall_previous_version();

print_line_break();

# register autoinstallssl plugin with cPanel
register_autoinstallssl_plugin();

opendir( my $themedirs, $theme_base );
opendir( my $themedirs, $theme_base1 );
opendir( my $themedirs, $theme_base2 );
print_line_break();

copy_files();

# register autoinstallssl plugin with Paper lantern theme
register_autoinstallssl_plugin_to_paperlantern();

#create separate process to update
print_line_break();

# delete unwanted files generated by register_cpanelplugin script


print_line_break();

print "\nPlugin installation successful!\n";
exit;


sub copy_files {
    print "\n Copying plugin files... \n";
  	print `/bin/cp -a -f -v data/usr/local/cpanel/base/frontend/* $theme_base/`;
	print `/bin/cp -a -f -v data/usr/local/cpanel/base/frontend/* $theme_base1/`;
	print `/bin/cp -a -f -v data/usr/local/cpanel/base/frontend/* $theme_base2/`;
	print `chmod 777 $theme_base/autoinstallssl/*`;
	print `chmod 777 $theme_base1/autoinstallssl/*`;
	print `chmod 777 $theme_base2/autoinstallssl/*`;
}

# Validates that the cPanel version supports the APIS for autoinstallssl plugin
sub valid_cpanel_version_for_plugin {

    my $current_version  = Cpanel::Version::getversionnumber();
    my $baseline_version = '11.42.0';
    my $is_valid_version = 0;

    print("\nValidating if your current cPanel version supports plugin...\n");

    my ( $current, $baseline ) = ( version->parse("$current_version"), version->parse("$baseline_version") );

    if ( $current < $baseline ) {
        print "Error: This plugin is not valid with your version of cPanel, please update to $baseline_version or later.\n";
        $is_valid_version = 0;
    }
    else {
        print "This version of cPanel supports the plugin!  \nContinuing install...\n";
        $is_valid_version = 1;
    }

    return $is_valid_version;
}

sub uninstall_previous_version {
    if ( -x "$theme_base/autoinstallssl") {
        print "\nautoinstallssl is installed - deleting previous version...\n";
        print `./uninstall`;
    }
	if ( -x "$theme_base1/autoinstallssl") {
        print "\nautoinstallssl is installed - deleting previous version...\n";
        print `./uninstall`;
    }
	if ( -x "$theme_base2/autoinstallssl") {
        print "\nautoinstallssl is installed - deleting previous version from paper_lantern...\n";
        print `./uninstall`;
    }
}

# register autoinstallssl plugin using register_cpanelplugin script
sub register_autoinstallssl_plugin {
    if ( -x "/usr/local/cpanel/bin/register_cpanelplugin" ) {
        print "\nRegistering plugin with cPanel...\n";
        `/usr/local/cpanel/bin/register_cpanelplugin autoinstallssl.cpanelplugin`;
    }
    else {
        print "\nFailed to register plugin with cPanel\n";
    }
}

# register autoinstallssl plugin to Paper_Lantern Theme
sub register_autoinstallssl_plugin_to_paperlantern {
    if ( -x "/usr/local/cpanel/bin/register_cpanelplugin" ) {
        print "\nRegistering plugin with paper lantern theme...\n";
        `/usr/local/cpanel/scripts/install_plugin /usr/local/cpanel/base/frontend/paper_lantern/autoinstallssl`;
    }
    else {
        print "\nFailed to register plugin with paper lantern theme\n";
    }
}

sub print_line_break {
    print "=" x 50 . "\n";
}

sub log_feature_list_update_messages {
    my (@messages) = @_;

    open( my $LOGFILE, '>>', $feature_list_update_log ) || die "Unable to open log file for writing.\n";
    print $LOGFILE (@messages);
    close $LOGFILE;
}