platform:serial8250
     ›ˆÑÀƒu¦›Ã¾     ›#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/process_site_templates          Copyright 2022 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;

ProcessSiteTemplates::run(@ARGV) unless caller;

package ProcessSiteTemplates;

use Getopt::Long qw(GetOptionsFromArray);
use Try::Tiny;

use File::Basename;

use Cpanel::Template        ();
use Cpanel::JSON            ();
use Cpanel::PwCache         ();
use Cpanel::SafeDir::MK     ();
use Cpanel::SafeRun::Errors ();
use Cpanel::Tar             ();

sub usage {
    my $exit_code = shift || 0;
    print <<EO_USAGE;
process_site_templates --source=/path/to/site/templates --target=/path/to/target/directory

Process Site Publisher templates from a source directory in order to generate HTML files in the target directory.
This script overwrites any existing files in the target directory.

    Options:
      --define para1=val1 Provide a value for a parameter that the template uses.

EO_USAGE

    require POSIX;
    POSIX::_exit($exit_code);
    return;
}

sub bail_out {
    my ($msg) = @_;
    print STDERR "Error: $msg\n\n";
    usage(1);
    return;
}

my ( $source, $target, $config, %parameters, $help, $tt, $bu_file, $config_file, $homedir );

sub run {
    my (@args) = @_;

    die "Do not run this script as the root user" if ( $> == 0 );

    $homedir = Cpanel::PwCache::gethomedir();

    my %parameters = ();

    GetOptionsFromArray(
        \@args,
        'source=s' => \$source,
        'target=s' => \$target,
        'config=s' => \$config,
        'define=s' => \%parameters,
        'help'     => \$help,
    ) || usage(1);

    if ($help) {
        usage(0);
    }

    unless ( $target && -d $target && $target =~ m/^\// ) {
        bail_out("The target directory ($target) is invalid.");
    }

    my $config_dir = $homedir . '/site_publisher/configurations';
    unless ( -d $config_dir ) {
        Cpanel::SafeDir::MK::safemkdir($config_dir);
    }
    chmod 0700, $config_dir;

    $config_file = join( '-', split( /\/+/, $target ) );
    $config_file =~ s{^-}{};
    $config_file .= '.json';
    $config_file = $config_dir . '/' . $config_file;

    my $data_file = $target . '/configurations.json';

    my $current_settings = {};

    if ( -e $config_file ) {
        eval { $current_settings = Cpanel::JSON::LoadFile($config_file) };
    }
    elsif ( -e $data_file ) {
        eval { $current_settings = Cpanel::JSON::LoadFile($data_file) };
        save_settings($current_settings);
    }
    unlink $data_file;

    try {
        do_archive();
    }
    catch {
        die "Unable to create a backup of the target directory: $_";
    };

    if ($config) {
        if ( -e $config ) {
            eval { $current_settings = Cpanel::JSON::LoadFile($config) };
        }
        else {
            die "Unable to read config file: $config";
        }
    }

    unless ( $source && -d $source ) {
        if ( $current_settings->{path} && $current_settings->{template} ) {
            $source = $current_settings->{path} . '/' . $current_settings->{template};
        }

        bail_out("The source directory ($source) is invalid.") unless $source && -d $source;
    }

    foreach my $k ( keys %parameters ) {
        $current_settings->{$k} = $parameters{$k};
    }

    try {
        process_templates($current_settings);
    }
    catch {
        my $msg = $_;

        if ( $bu_file && -e $bu_file ) {
            restore_target();
        }

        die "Publication of site template failed: $msg";
    };

    my $template = basename($source);
    my $path     = dirname($source);
    $current_settings->{'template'} = $template;
    $current_settings->{'path'}     = $path;

    save_settings($current_settings);

    return 1;
}

sub process_templates {
    my ( $data, $current_dir ) = @_;

    my $template_dir = $sourc