#!/usr/bin/perl 
#############################################################
## @file networkconfig
# @brief   This script configures network parameters
# @details If there are no cli arguments provided then
#          user will be prompted to enter them.
# @author  Alex Arkhipov
# @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 - function main() was changed by Ekaterina Bazhanova:
#                 synchronization 'hostname' and 'fqdn' values was added       
# @note
#############################################################
=pod

=head1 NAME

B<networkconfig> - configures the network settings.

=head1 SYNOPSIS

B<networkconfig> [B<--show>] [B<-eip> I<ELAN IP>] [B<-egw> I<ELAN gateway IP>]
[B<-emsk> I<ELAN netmask>] [B<-tip> I<TLAN IP>] [B<-tgw> I<TLAN gateway IP>] [B<-tmsk>
I<TLAN netmask>] [B<-dgw> I<default gateway IP>] [B<-hn> I<hostname>]
[B<-fqdn> I<fully qualified domain name>]|[B<--help>]

=head1 DESCRIPTION

B<networkconfig> configures the following network settings:

=over 4

=item - ELAN IP

=item - ELAN gateway IP

=item - ELAN netmask

=item - TLAN IP

=item - TLAN gateway IP

=item - TLAN netmask

=item - dafault gateway IP

=item - hostname

=item - fully qualified domain name

=back

B<networkconfig> can be launched in interactive mode in case there are no CLI arguments provided.

=head1 OPTIONS

=over 4

=item B<--show>

Show the current network settings.

=item B<-eip> I<ELAN IP>

Specify an ELAN IP.

=item B<-egw> I<ELAN gateway IP>

Specify an ELAN gateway IP.

=item B<-emsk> I<ELAN netmask>

Specify an ELAN netmask.

=item B<-tip> I<TLAN IP>

Specify a TLAN IP.

=item B<-tgw> I<TLAN gateway IP>

Specify a TLAN gateway IP.

=item B<-tmsk> I<TLAN netmask>

Specify a TLAN netmask.

=item B<-dgw> I<default gateway>

Specify a Default Gateway, requires a numeric value (0 - ELAN, 1 - TLAN)

=item B<-hn> I<hostname>

Specify a hostname

=item B<-fqdn> I<fully qualified domain name>

Specify a Fully Qualified Domain Name.

=item B<--help>

Print this help message and exit

=back

=head1 CAVEATS

B<networkconfig> can be run only under systemadmin role/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 CommonInstallUtils;
use baselog;

sub main();

my %data = (
            'eip'  => "",
            'egw'  => "",
            'emsk' => "",
            'tip'  => "",
            'tgw'  => "",
            'tmsk' => "",
            'dgw'  => "",
            'hm'   => "",
            'fqdn' => ""
           );

# Hash with matched names with baseParams module
my %matched_data = (
                    'eip'  => 'machineIP',
                    'egw'  => 'gateway',
                    'emsk' => 'netmask',
                    'tip'  => 'machineIPTlan',
                    'tgw'  => 'gatewayTlan',
                    'tmsk' => 'netmaskTlan',
                    'dgw'  => 'defaultGateway',
                    'hn'   => 'hostname',
                    'fqdn' => 'fqdnTlan'
                   );

# Hash with data to set in baseParams.pm module
my %base_data = (
                 'machineIP'      => '',
                 'gateway'        => '',
                 'netmask'        => '',
                 'machineIPTlan'  => '',
                 'gatewayTlan'    => '',
                 'netmaskTlan'    => '',
                 'defaultGateway' => '',
                 'hostname'       => '',
                 'fqdnTlan'       => ''
                );

exit(main());

sub main()
{
   my $interactive = (scalar(@ARGV) == 0) ? $TRUE : $FALSE;
   unless (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 "--help")
      {
         print_pod_usage();
         return $OK;
      }
      if ((scalar(@ARGV) == 1) && ($ARGV[0] eq "--show"))
      {
         print_network_config(\%base_data);
         return $OK;
      }
      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])...
      my $res = GetOptions(\%data, 'eip=s', 'egw=s', 'emsk=s', 'tip=s', 'tgw=s', 'tmsk=s', 'dgw=s', 'hn=s', 'fqdn=s');
      if (!$res)
      {
         print_pod_usage();
         return $ERROR;
      }
     
      ##hostname and fqdn should be synchronized
      if (($data{hn}) && ($data{fqdn})) 
      {
          unless ($data{fqdn} =~ /^$data{hn}\./)
          {
             print "Incorrect value for 'hn' or 'fqdn' parameter\n";
             print "FQDN should include new hostname:\n";
             print "Hostname + Domain Name = FQDN (Fully Qualified Domain Name)\n";
             return $ERROR;
          }
      }    
      elsif ($data{hn})
      {
         ## synchronize fqdn with hostname
         $base_data{'fqdnTlan'} =~ s/^[^.]+/$data{hn}/;
      }
      elsif ($data{fqdn})
      {
         ## synchronize hostname with fqdn
         $data{fqdn}  =~ /([^.]+)/;   
         $base_data{'hostname'} = $1;         
      }
             
      if ($data{dgw} ne "")
      {
         if (($data{dgw} eq $ELAN_GW) || ($data{dgw} eq $TLAN_GW))
         {
            # Set the defaultGateway to 'dgw' input
            $base_data{'defaultGateway'} = $data{dgw};
         }
         else
         {
            print "Incorrect value for 'dgw' parameter\n";
            print "Default gateway must be specified as: 0 - ELAN; 1 - TLAN\n";
            return $ERROR;
         } ## end else [ if ($data{dgw} eq "0")
      } ## end if ($data{dgw} ne "")
 
      # 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'\n";
            return $ERROR;
         }
         else
         {
            $base_data{$base_key} = $data{$key};
         }
      } ## end foreach my $key (keys(%data...

      # ELAN IP address should not be the same as TLAN IP
      if ($base_data{'machineIP'} eq $base_data{'machineIPTlan'})
      {
            print "TLAN IP address could not be the same as ELAN IP address\n";
            return $ERROR;
      } ## end unless (andIpAddres($base_data...

      # IP address and its Gateway shoule be within the same network
      unless ((andIpAddrs($base_data{'machineIP'}, $base_data{'netmask'}) eq 
               andIpAddrs($base_data{'gateway'}, $base_data{'netmask'})) and 
              (andIpAddrs($base_data{'machineIPTlan'}, $base_data{'netmaskTlan'}) eq 
               andIpAddrs($base_data{'gatewayTlan'}, $base_data{'netmaskTlan'})))
      {
            print "IP address and its gateway should be within the same subnet\n";
            return $ERROR;
      } ## end unless (andIpAddres($base_data...

   } ## end if (!$interactive)
   
   signals_ignore();
   if ($interactive)
   {

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

   print_network_config(\%base_data);

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



__END__
