#!/usr/bin/perl  
## @file dnsconfig
# @brief This script configures DNS servers
# @details If there are no cli arguments provided then
#          user will be prompted to enter them
# @author Alex Arkhipov, Evgeny Lazarev
# @date January 2007
# @version 1.00 - Initial version
# @version 1.01 - POD section was added; the usage is displayed by the
#                 common function - print_pod_usage() now.
# @version 1.02 - Two colons were removed before shared functions and variables.
# @version 1.03 - Support of the '--unconfigure' option has been added.
#                 Secondary DNS Server can not be configured while Primary is
#                 not configured (in the non-interactive mode).

=pod

=head1 NAME

B<dnsconfig> - configures the DNS server settings.

=head1 SYNOPSIS

B<dnsconfig> B<-dns1> I<IP> [B<-dns2> I<IP>]

B<dnsconfig> B<--unconfigure>

B<dnsconfig> B<--show>

B<dnsconfig> B<--help>

=head1 DESCRIPTION

B<dnsconfig> configures DNS servers from command line or in the interactive mode.

=head1 OPTIONS

=over 4

=item B<--show>

Show the current network settings.

=item B<-dns1> I<DNS IP>

Specify the first DNS IP.

=item B<-dns2> I<DNS IP>

Specify the second DNS IP. In order to specify second IP the first IP must be defined.

=item B<--unconfigure>

Unconfigure DNS settings.

=item B<--help>

Print this help message and exit.

=back

=head1 CAVEATS

B<dnsconfig> can be run only under user from the systemadmin group.
The system reboot will be required to apply changes.

=head1 SEE ALSO

B<baseparamconfig>

=cut

use lib "/opt/nortel/base/lib";

use warnings;
use strict;
use Getopt::Long;

use common_variables;
use common_functions;
use baseParams;
use baseParamsUI;
use baselog;

sub main();

my %data = (
            'dns1' => "",
            'dns2' => ""
           );

# Hash with matched names with baseParams module
my %matched_data = (
                    'dns1' => "primaryDNSIP",
                    'dns2' => "secondaryDNSIP"
                   );

# Hash with data to set in baseParams.pm module
my %base_data = (
                 'primaryDNSIP'   => '',
                 'secondaryDNSIP' => '',
                 'tertiaryDNSIP'  => '127.0.0.1'
                );

exit(main());

sub main()
{
   if (defined $ARGV[0] and $ARGV[0] =~ /help$/)
   {
      print_pod_usage();
      return $OK;
   }
   my $interactive = (scalar(@ARGV) == 0) ? $TRUE : $FALSE;
   if (!defined(getParams(\%base_data)))
   {
      baselog($LOG_ERROR, "Cannot get current data\n", $TRUE);
      return $ERROR;
   }

   # If there are any arguments then try to extract data from arguments
   if (!$interactive)
   {
      if ((scalar(@ARGV) == 1) && ($ARGV[0] eq "--show"))
      {
         print_dns_config(\%base_data);
         return $OK;
      }
      if ((scalar(@ARGV) == 1) && ($ARGV[0] eq "--unconfigure"))
      {
         unconfigureDNS(\%base_data);
      }
      else
      {
         my $i = 0;
         while (defined($ARGV[$i]))
         {
            if ($ARGV[$i] !~ /^\-\w+$/)
            {
               print_pod_usage("Incorrect option $ARGV[$i] is provided");
               return $ERROR;
            }
            $i += 2;
         } ## end while (defined($ARGV[$i])...
         if (!GetOptions(\%data, 'dns1=s', 'dns2=s'))
         {
            print_pod_usage();
            return $ERROR;
         }

         if (!$data{'dns1'} && $data{'dns2'})
         {
            baselog($LOG_ERROR, "Can not configure Secondary DNS server until Primary is not defined.", $TRUE);
            return $ERROR;
         }
         if (!$data{'dns1'})
         {
            $data{'dns1'} = $UNCONFIGURED;
         }
         if (!$data{'dns2'})
         {
            $data{'dns2'} = $UNCONFIGURED;
         }

         # Check values
         foreach my $key (keys(%data))
         {
            next if ($data{$key} eq "");  # skip empty arguments
            my $base_key = $matched_data{$key};
            if (!validateParam($base_key, $data{$key}))
            {
               print "Incorrect value for '$key' parameter\n";
               return $ERROR;
            }
            else
            {
               $base_data{$base_key} = $data{$key};
            }
         } ## end foreach my $key (keys(%data...
      } ## end else [ if ((scalar(@ARGV) == ...
   } ## end if (!$interactive)

   signals_ignore();
   if ($interactive)
   {

      # Prompt user for new network settings
      configDNSServer(\%base_data);
   } ## end if ($interactive)

   print_dns_config(\%base_data);

   unless (setupDNS(\%base_data))
   {
      baselog($LOG_ERROR, "ERROR: Cannot set up DNS servers\n", $TRUE);
      return $ERROR;
   }
   signals_default();
   print "DNS servers have been setup successfully. Please reboot the system to apply the parameter changes. \n";
   return $OK;
} ## end sub main()

__END__
