#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - bin/empty_user_horde_temp_files         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

package bin::empty_user_horde_temp_files;

use strict;
use warnings;

use Getopt::Long                qw(:config pass_through);
use Pod::Usage                  ();
use Cpanel::Config::CpConfGuard ();
use Cpanel::Config::Users       ();
use Cpanel::ServerTasks         ();
use Cpanel::PwCache             ();

if ( not caller() ) {
    my $ret = run(@ARGV);
    if ( not $ret ) {
        exit 1;
    }
}

sub run {
    my @args = @_;

    # TODO: If you add any more arguments, please make this a hash.

    my ( @users, $all, $help, $quiet );
    my $ret = Getopt::Long::GetOptionsFromArray(
        \@args,
        "all|a"   => \$all,
        "help|h"  => \$help,
        "quiet|q" => \$quiet,
    );

    @users = @args;

    return _show_help($quiet) if defined $help || ( !defined $all && scalar @users == 0 );

    # take the rest without dashes as a username, if any unknown args with dashes are left; return undef to indicate error; send help to STDERR
    return _show_help_error($quiet) if ( grep { m/-/ } @users );

    my $cpanel_config = Cpanel::Config::CpConfGuard->new();
    my $skip_horde    = $cpanel_config->{data}{skiphorde} // 0;
    my $older_than    = $cpanel_config->{data}{horde_cache_empty_days};

    return _show_disabled_notice($quiet) if $skip_horde == 1 or $older_than eq 'disabled' or $older_than == 0;

    if ( ( scalar @users == 0 ) || defined $all ) {
        print "Deleting temp Horde files for all users.\n" if !defined $quiet;
        @users = Cpanel::Config::Users::getcpusers();
    }

  QUEUE_TASK:
    foreach my $user (@users) {
        my $home = Cpanel::PwCache::gethomedir($user);
        next QUEUE_TASK                                if not defined $home or not _horde_temp_dir_exists($home);
        print "Deleting temp Horde files for: $user\n" if !defined $quiet;
        Cpanel::ServerTasks::queue_task( ['FilemanTasks'], "empty_user_horde_temp_files $user $older_than" );
    }

    return 1;
}

sub _horde_temp_dir_exists {
    my $base = shift;
    return -d qq{$base/tmp/horde};
}

sub _show_help {
    return 1 if defined shift;
    Pod::Usage::pod2usage(
        '-exitval'   => 'NOEXIT',
        '-verbose'   => 2,
        '-noperldoc' => 1,
        -output      => \*STDOUT,
    );
    return 1;
}

sub _show_help_error {
    return 1 if defined shift;
    Pod::Usage::pod2usage(
        '-exitval'   => 'NOEXIT',
        '-verbose'   => 2,
        '-noperldoc' => 1,
        -output      => \*STDERR,
    );
    return undef;
}

sub _show_disabled_notice {
    return 1 if defined shift;
    print <<EOM;
Automatic temp Horde file deletion is disabled. To enable it, open the /var/cpanel/cpanel.config file and set the horde_cache_empty_days key's value to any positive integer. Also, make certain that the skiphorde key's value is not set to 1.
EOM

    return 1;
}

1;

__END__

=pod

=encoding utf-8

=head1 NAME

empty_user_horde_temp_files - Purge temp Horde files older than a configured number of days.

=head1 SYNOPSIS

    empty_user_horde_temp_files [-h|--help] [-q|--quiet] (-a|--all|<username>)

    Options:

        -q|--quiet
            Suppress output.

        -h|--help
            Show this help output.

        -a|--all
            Purge $HOME/tmp/horde/ content for all users.

        username
            Remove temp Horde files for the specified username.

=head1 DESCRIPTION

This utility checks the for Horde temp files for the specified user (or all
users if --all is passed instead) and purges files older than the
number of days specified by the horde_cache_empty_days configuration directive in
/var/cpanel/cpanel.config.

=cut
