#!/bin/bash
eval '/usr/local/cpanel/scripts/fix-cpanel-perl && exec /usr/local/cpanel/3rdparty/bin/perl -x -- $0 ${1+"$@"}'    ## no critic qw(ProhibitStringyEval RequireUseStrict RequireUseWarnings)
  if 0;

#!/usr/bin/perl
# cpanel - scripts/check_domain_recommendations_store
#                                      Copyright 2026 WebPros International, LLC
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cpanel license. Unauthorized copying is prohibited

package scripts::check_domain_recommendations_store;

use cPstrict;

use Cpanel::Usage ();

=encoding utf-8

=head1 NAME

check_domain_recommendations_store - notify the WHM admin when the
domain-recommendation feature is enabled but the WHMCS store is unreachable

=head1 SYNOPSIS

    check_domain_recommendations_store [--notify]

=head1 DESCRIPTION

Re-evaluates the domain-recommendation store state and, with C<--notify>,
raises the WHM admin notification when the feature is enabled in one or more
feature lists but the configured store fails its connection test. Without
C<--notify> it only reports the state. Intended to be run from
C<scripts/maintenance> and on demand; it is a no-op (and sends nothing) once
the store is reachable, so the notification clears on its own.

Exits 2 when the misconfigured state is present (a notification is warranted),
0 otherwise.

=cut

exit( run(@ARGV) // 0 ) unless caller;

sub run (@argv) {
    my $notify = 0;
    my %opts   = ( 'notify' => \$notify );

    # wrap_options returns a status > 2 for a malformed command line (e.g. an
    # unknown option under strict); reject those with a non-zero exit.
    my $status = Cpanel::Usage::wrap_options( { strict => 1 }, \@argv, \&usage, \%opts ) || 0;
    usage(1) if $status > 2;

    require Cpanel::DomainRecommendations::Notify;
    my $feature_lists_ar = Cpanel::DomainRecommendations::Notify::misconfigured_feature_lists();
    my $misconfigured    = scalar @$feature_lists_ar;

    if ( $misconfigured && $notify ) {
        Cpanel::DomainRecommendations::Notify::notify();
    }

    return $misconfigured ? 2 : 0;
}

sub usage ( $exit_code = 0 ) {
    print <<'END_USAGE';
Usage: check_domain_recommendations_store [options]

Notify the WHM administrator when the "Domain Recommendation & Purchase"
feature is enabled but the configured WHMCS store is unreachable.

Options:
    --notify    Raise the WHM admin notification when the misconfigured state
                is detected. Without this flag the state is only evaluated.
    --help      Show this help and exit.
END_USAGE

    exit $exit_code;
}

1;
