#!/usr/bin/perl

#######################################################################
#
# Ldap2Zone - write DNS Zone file from LDAP informations
#
# Copyright (C) 2011-2016 FusionDirectory project
#
# Author: Côme BERNIGAUD
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
#######################################################################

use strict;
use warnings;

use 5.008;

use Argonaut::Libraries::Ldap2zone qw(argonaut_ldap2zone);

my $zone_arg = undef;
my $ldap2view = 0;
my $verbose_arg = 0;
my $norefresh = 0;
my $noreverse;
my $dumpdir = '';

sub print_usage
{
  print "Usage : argonaut-ldap2zone [--verbose] [--noreverse] [--norestart] [--dumpdir dir] [--ldap2acls] [--ldap2view view] ZONE\n";
  exit(0);
}

my $i_arg = 0;

foreach my $arg ( @ARGV ) {
  if (not defined $dumpdir) {
    $dumpdir = $arg;
  } elsif (lc($arg) eq "--verbose") {
    $verbose_arg = 1;
  } elsif (lc($arg) eq "--noreverse") {
    $noreverse = 1;
  } elsif (lc($arg) eq "--norestart") {
    $norefresh = 1;
  } elsif (lc($arg) eq "--dumpdir") {
    undef $dumpdir;
  } elsif (lc($arg) eq "--ldap2acls") {
    $ldap2view = 'acls';
  } elsif (lc($arg) eq "--ldap2view") {
    $ldap2view = 'view';
  } elsif ($i_arg==0) {
    $zone_arg = $arg;
    $i_arg++;
  } else {
    print_usage();
  }
}
if (not defined $dumpdir) {
  print_usage();
} elsif ($dumpdir eq '') {
  undef $dumpdir;
}

if (!$zone_arg) {
  if ($ldap2view eq 'acls') {
    $zone_arg = '';
  } else {
    print_usage();
  }
}

argonaut_ldap2zone($zone_arg,$verbose_arg,$norefresh,$dumpdir,$noreverse,$ldap2view);

__END__

=head1 NAME

argonaut-ldap2zone - creating bind zone files and refreshing the server

=head1 SYNOPSIS

argonaut-ldap2zone [--verbose] [--noreverse] [--norestart] [--dumpdir dir] [--ldap2acls] [--ldap2view view] ZONE

=head1 DESCRIPTION

argonaut-ldap2zone is a program used to create bind zone files and refresh the bind service

=head1 OPTIONS

=over 3

=item B<--verbose>

be verbose

=item B<--noreverse>

Do not write reverse zone

=item B<--norestart>

Do not refresh bind zone with rndc

=item B<--dumpdir>

Dump the zones and bind configuration to another dir for testing purposes

=item B<--ldap2acls>

Write the the acls needed for bind in named.conf.acls

=item B<--ldap2view>

Write the zones in view format when you want to do split horizon dns

=back

=head1 EXAMPLE

B<argonaut-ldap2zone --verbose fusiondirectory.org.>

 Write the updated zones from ldap and refresh bind

B<argonaut-ldap2zone --verbose --norestart --dumpdir dnszone/ fusiondirectory.org>

 Write the updated zones from ldap to another directory and don't refresh bind

B<argonaut-ldap2zone --verbose --ldap2view internal fusiondirectory.org>

 Write the updated zones from ldap in a bind view named internal and refresh bind

B<argonaut-ldap2zone --verbose --ldap2acls>

 Write the acls from ldap into named.conf.acls
 
=head1 BUGS

Please report any bugs, or post any suggestions, to the fusiondirectory mailing list fusiondirectory-users or to
<https://forge.fusiondirectory.org/projects/argonaut-agents/issues/new>

=head1 AUTHORS

Come Bernigaud

=head1 LICENCE AND COPYRIGHT

This code is part of Argonaut Project <https://www.argonaut-project.org/>

=over 1

=item Copyright (C) 2011-2016 FusionDirectory project

=back

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

=cut
