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

#                                      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.

use strict;
use warnings;

use Cpanel::DnsUtils::AskDnsAdmin        ();
use Cpanel::DnsUtils::Exists             ();
use Cpanel::Hooks                        ();
use Cpanel::AcctUtils::DomainOwner::Tiny ();

local $SIG{'PIPE'} = 'IGNORE';
local $SIG{'HUP'}  = 'IGNORE';
local $SIG{'ALRM'} = 'IGNORE';

if ( $ENV{'KILLDNS_ENTRY'} && $ENV{'KILLDNS_ENTRY'} eq '1' ) {
    print "Loop prevented.  Are your /usr/local/cpanel/scripts out of sync with your cPanel version in /usr/local/cpanel? Try running /usr/local/cpanel/scripts/upcp --sync\n";
    exit 1;
}
$ENV{'KILLDNS_ENTRY'} = 1;    #prevent looping

my $domain = @ARGV ? $ARGV[0] : '';

if ( $domain eq '' && -t STDIN ) {
    print 'Enter the zone to delete? ';
    chomp( $domain = <STDIN> );
}

if ( $domain eq '' ) {
    print "Error: No zone specified\n";
    exit;
}

$domain =~ s/\///g;

local $0 = "killdns - $domain";

my $owner = Cpanel::AcctUtils::DomainOwner::Tiny::getdomainowner( $domain, { 'default' => '' } );

my $zone_existed = Cpanel::DnsUtils::Exists::domainexists($domain);

if ($zone_existed) {
    my $hook_data = { 'domain' => $domain, 'owner' => $owner };

    my $pre_hook_info = {
        'category' => 'Whostmgr',
        'event'    => 'Domain::zone_remove',
        'stage'    => 'pre',
        'blocking' => 1,
    };

    my ( $pre_hook_result, $hook_msgs ) = Cpanel::Hooks::hook( $pre_hook_info, $hook_data );
    if ( !$pre_hook_result ) {
        print Cpanel::Hooks::hook_halted_msg( $pre_hook_info, $hook_msgs ) . "\n";
        exit 1;
    }
}

my $output = Cpanel::DnsUtils::AskDnsAdmin::askdnsadmin( 'REMOVEZONE', 0, $domain );
print $output;

if ( $zone_existed && !Cpanel::DnsUtils::Exists::domainexists($domain) ) {
    Cpanel::Hooks::hook(
        {
            'category' => 'Whostmgr',
            'event'    => 'Domain::zone_remove',
            'stage'    => 'post',
        },
        { 'domain' => $domain, 'owner' => $owner },
    );
}
