PCI-MSIX-0000:00:02.0
  #   !ˆl–Ãa¾”*e¶…8 Cq§.õ1hßÈ„O�ï¿Æ :<    !#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/generate_maildirsize            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;
use warnings;

use Cpanel::Usage                        ();
use Cpanel::PwCache::Helpers             ();
use Cpanel::PwCache::Build               ();
use Cpanel::PwCache                      ();
use Cpanel::JSON                         ();    # PPI USE OK - speed up loaduserdomains
use Cpanel::AccessIds::ReducedPrivileges ();
use Cpanel::Config::LoadCpUserFile       ();
use Cpanel::Config::HasCpUserFile        ();
use Cpanel::Config::Users                ();
use Cpanel::Config::LoadCpConf           ();
use Cpanel::Config::LoadUserDomains      ();
use Cpanel::Email::DiskUsage             ();
use Cpanel::Email::Maildir::Utils        ();
use Cpanel::Email::Maildir               ();
use Cpanel::Email::Mailbox               ();
use Cpanel::AdminBin::Serializer         ();    # PPI USE OK - speed up loaduserdomains
use Try::Tiny;

my $onlyrecalculate = 0;
my $verbose         = 0;
my $rename          = 0;
my $confirm         = 0;
my $allaccounts     = 0;

# Max quota is actually 1 byte less than get_max_email_quota
# but we'll silently fix the 1 byte issue below
my $max_quota = Cpanel::Email::Maildir::get_max_email_quota();

# Argument processing
my %opts = (
    'onlyrecalculate' => \$onlyrecalculate,
    'verbose'         => \$verbose,
    'rename'          => \$rename,
    'confirm'         => \$confirm,
    'allaccounts'     => \$allaccounts,
);

Cpanel::Usage::wrap_options( \@ARGV, \&usage, \%opts );

# When we are regenerating files we must tell Cpanel::Email::DiskUsage to ignore
# the existing maildirsize files or they will be used to regenerate themselves.
local $Cpanel::Email::DiskUsage::IGNORE_MAILDIRSIZE_FILES = 1;
local $Cpanel::Email::DiskUsage::VERBOSE                  = $verbose;

if ( $> == 0 && !$confirm ) {
    print "Must specify \"--confirm\" to begin. Please read and understand the usage.\n\n";
    usage(1);
}
umask(0077);    # Keep maildirsize file perms consistent with Exim

my $cpconf = Cpanel::Config::LoadCpConf::loadcpconf_not_copy();

my $pwcache_ref;
my %CPUSERS;
my $userdomains_ref = {};
my $suid            = 0;

if ( $> == 0 ) {
    $suid = 1;
    Cpanel::PwCache::Helpers::no_uid_cache();    #uid cache only needed if we are going to make lots of getpwuid calls
    Cpanel::PwCache::Build::init_passwdless_pwcache();
    $pwcache_ref = Cpanel::PwCache::Build::fetch_pwcache();
    my $users_arr_ref = Cpanel::Config::Users::getcpusers();
    $userdomains_ref = Cpanel::Config::LoadUserDomains::loaduserdomains( undef, 0, 1 );

    %CPUSERS = map { $_ => undef } @{$users_arr_ref};
    if ( @ARGV && $ARGV[-1] !~ m/^-/ ) {
        if ( exists $CPUSERS{ $ARGV[-1] } ) {
            %CPUSERS     = ( $ARGV[-1] => 1 );    #only do one user
            $allaccounts = 1;                     # Specified because a user was provided and they may or may not be using boxtrapper
        }
        else {
            %CPUSERS = ();
        }
    }
}
else {
    $rename      = 1;
    $allaccounts = 1;
    my @PW = Cpanel::PwCache::getpwuid_noshadow($>);
    $pwcache_ref = [ \@PW ];
    %CPUSERS     = ( $PW[0] => 1 );
    die "Unable to load cPanel user data.\n" unless Cpanel::Config::HasCpUserFile::has_cpuser_file( $PW[0] );
    my $user_info = Cpanel::Config::LoadCpUserFile::loadcpuserfile( $PW[0] );    #we want to load the default so we can use the storable cache

    if ( !scalar keys %{$user_info} ) {
        die "Unable to load cPanel user data.\n";
    }

    my @DOMAINS = ( $user_info->{'DOMAIN'} );

    if ( ref $user_info->{'DOMAINS'} ) {
        push @DOMAINS, @{ $user_info->{'DOMAINS'} };
    }
    $userdomains_ref->{ $PW[0] } = \@DOMAINS;
}

my $mailgid = ( Cpanel::PwCac