#!/usr/bin/perl 

use v5.10;
use warnings;
use strict;
use File::Path;
use DNStools::Config qw(get_config);
use Net::LibIDN qw(:all);
use Pod::Usage;
use Getopt::Long;

my $CHARSET = "UTF-8";

my %cf = get_config();

my $opt_interactive = 0;

MAIN: {

    GetOptions(
        "i|interactive!" => \$opt_interactive,
        "h|help"         => sub { pod2usage(-exit => 0, -verbose => 1) },
        "m|man"          => sub {
            pod2usage(
                -exit      => 0,
                -verbose   => 2,
                # "system('perldoc -V &>/dev/null')" appears shorter, but may not
                # do what you expect ( it still returns 0 on debian squeeze with
                # dash as system shell even if cannot find the command in $PATH)
                -noperldoc => system('perldoc -V >/dev/null 2>&1')
            );
        },
      )
      and @ARGV
      or pod2usage;

    for my $utf8zone (@ARGV) {
        my $zone = idn_to_ascii($utf8zone, $CHARSET);

        if (not(-d "$cf{master_dir}/$zone" or -f "$cf{zone_conf_dir}/$zone")) {
            say "$utf8zone ($zone): nothing found";
            next;
        }

        if ($opt_interactive) {
            print "Remove $utf8zone ($zone)? [y/n]: ";
            chomp($_ = <STDIN>);
            next if not /^y(?:es)?$/i;
        }

        rmtree("$cf{master_dir}/$zone", "$cf{zone_conf_dir}/$zone",
            { verbose => 1 });

    }
}

__END__

=head1 NAME

    zone-rm - remove a zone

=head1 SYNOPSIS

    zone-rm [-i|--interactive] zone...

    zone-rm [-h|--help]
    zone-rm [-m|--man]

=head1 DESCRIPTION

This removes the specified zone(s) from the configured directories.

=head1 OPTIONS

The standard B<-h>, B<--help>, B<-m>, and B<--man> options are
understood.

=over

=item B<-i>|B<--interactive>

Ask for confirmation. (default: off)

=cut
