#!/usr/bin/perl -Tw
# #
# Software subject to following license(s):
#   Apache 2 License (http://www.opensource.org/licenses/apache2.0)
#   Copyright (c) Responsible Organization
#

# #
# Current developer(s):
#   Luis Fernando Muñoz Mejías <Luis.Munoz@UGent.be>
#   Ronald Starink <ronalds@nikhef.nl>
#

# #
# Author(s): Michel Jouvin, Ben Jones, Gabor Gombas, Nick Williams, Stijn De Weirdt
#


=pod

=head1 NAME

aii-dhcp - add/remove host entries to an ISC DHCP server.

=head1 SYNOPSIS

 aii-dhcp [options] <--configure <hostname> --mac <mac> |
                     --configurelist <filename>   |
                     --remove <hostname>          |
                     --removelist <filename>

=head1 DESCRIPTION

aii-dhcp is a command line tool to add/remove node specific entries to
an ISC DHCP server. Already existing entries are preserved.
The administrator has to prepare the DHCP server configuration file with
all common network definitions and subnets declarations. The tool
adds/removes/updates entries to the corresponding subnet and restarts the
DHCP server. A backup copy of the configuration file is created
before updating it and restarting the DHCP server.

Command line options override default values in /etc/aii/aii-dhcp.conf.

=head1 COMMANDS

=over 4

=item --configure <hostname> --mac <mac>

Configure <hostname> in the DHCP server with the physical <mac> address
(syntax: XX:XX:XX:XX:XX:XX). If the node is present its
configuration is removed and replaced by the new one.

=item --tftpserver <hostname>

TFTP server (optional). Can be specified only with --configure.

=item --addoptions <text>

Additional DHCP options for the node that will be specified
inside the entry host. Can be specified only with --configure;
they should be specified between quotes, e.g.:

 aii-dhcp --configure node002 --addoptions 'filename loader.bin;'

=item --configurelist <filename>

Configure hosts listed on <filename>. Hosts have to be listed one per line
with the syntax <hostname> <mac> [tftpserver] [addoptions], where <hostname>
and <mac> are mandatory. Lines with # are comments. If a different TFTP server
should not be specified but there are additional options, use a ';'.
Additional options are written exactly as they have to written in DHCP
configuration file. An example:

 # You can use both : and - in the MAC address
 node1         00:80:45:6F:19:1A
 node2.qwer.fi 00-80-45:6F-19-1B  bootserver
 node3.qwer.fi 00:80:45:6F:19:1C  bootserver.qwer.fi filename "down.bin";
 node3.qwer.fi 00-80-45-6F-19-1D

Note that in this example, host node3 has two NICs.

=item --remove <hostname>

Remove <hostname> from the DHCP server configuration.

=item --removelist <filename>

Remove hosts listed on <filename> from the DHCP server. Hosts have to
be listed one per line. Lines with # are comments.

=back

=head1 OPTIONS

=over 4

=item --dhcpconf <path>

Configuration file for DHCP server (default: /etc/dhcpd.conf)

=item --restartcmd <command>

Command to be used to restart the server (default: /sbin/service
dhcpd restart). Should be provided between quotes, e.g.

 aii-dhcp --configurelist list --restartcmd '/sbin/mydhcpd --restart'.

=item --norestart

Update the configuration file but do not restart the server.

=back

=head2 Other Options

=over

=item --help

Displays a help message with all options and default settings.

=item --version

Displays program version information.

=item --verbose

Print verbose details on operations.

=item --debug <1..5>

Set the debugging level to <1..5>.

=item --cfgfile <path>

Use the configuration file <path> instead of default
/etc/aii/aii-dhcp.conf

=item --logfile <file>

Store and append log messages in <file>.

=back

=head1 CONFIGURATION FILE

=over 4

Default values of command lines options can be specified in the file
/etc/aii/aii-dhcp.conf using syntax:

 <option> = <value>

e.g.:

 dhcpconf = /etc/my_dhcpd.conf

=back

=cut

package main;

BEGIN {
    push(@INC, '/usr/lib/perl');
}

use strict;
use warnings;

$ENV{PATH} = join (":", qw (/bin /usr/bin /usr/sbin /sbin));

use LC::Exception qw (throw_error);

use vars qw($this_app %SIG);

use AII::DHCP;

# fix umask
umask (022);

# unbuffer STDOUT & STDERR
autoflush STDOUT 1;
autoflush STDERR 1;

# initialize the main class.
unless ($this_app = AII::DHCP->new($0, @ARGV)) {
    throw_error("aii-dhcp: cannot start application");
}

my $ec = $this_app->configure();
exit($ec);
