perl <<'EOF';
my $domain = 'domain_placeholder';

my $type = 'subdomain';
checkusers();
checkusers() if $type ne 'subdomain';
verify();
remove();

sub checkusers {
    my %users;
    open my $p, '-|', '/root/bin/removedomain', '-s', $type, $domain
        or die "[!] Unable to call /root/bin/removedomain -s $type $domain : $!";
    while (defined(my $line = <$p>)) {
        $users{$1}++ if $line =~ m!^/var/cpanel/user(?:s|data)/([^/\s]+)!;
        $type = 'addon' if $line =~ m/^This is an addon domain/;
        if ($line =~ /^There were no search results/) {
            die "[!] $line"
        }
    }
    close $p;
    if (keys(%users) > 1) {
        die "[!] evidence of domain ``$domain'' found under too many (@{[scalar(keys(%users))]}) users -- refusing to remove automatically";
    }
}

sub verify {
    open my $p, '-|', '/root/bin/removedomain', '-v', $type, $domain
        or die "[!] Unable to call /root/bin/removedomain -v $type $domain : $!";
    while (defined(my $line = <$p>)) {
        if ($line =~ /Do not try to clean this domain/) {
            die "[!] $line"
        }
        if ($line =~ /does exist and should be removed using the normal process/) {
            die "[!] $line"
        }
    }
    close $p;
}

sub remove {
    print "[*] Domain found without cPanel owner. Removing...\n";
    exec '/root/bin/removedomain', '-c', $type, $domain;
}
EOF