#!/usr/bin/perl
## @file    fqdnreassign
# @brief    This script updates /etc/hosts file to re-assign FQDN
#           to TLAN or ELAN interface depends on user's selection.
# @author  Kate Bereznyakova
# @date    May 2009
# @version 1.00 - Initial version

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

use strict;
use warnings;

use common_variables;
use common_functions;
use CommonInstallUtils;
use base_system;
use baselog;
use baseParams;

## @skipline
sub fqdnreassign();

exit(fqdnreassign());

###############################################################################
## @fn fqdnreassign()
# @brief   Main function. It provides user's interaction part.
# @details None.
# @return  $OK     -  operation successfully completed
# @return  $ERROR  -  some error happened.
sub fqdnreassign()
{
   my $retval = $OK;
   my %data   = ();

   signals_ignore();

   if (!defined(getParams(\%data)))
   {
      baselog($LOG_ERROR, "Cannot get current data.\n", $TRUE);
      return $ERROR;
   }

   if ((!defined $data{'fqdnTlan'}) or ($data{'fqdnTlan'} eq ''))
   {
      baselog($LOG_ERROR, "Incorrect FQDN. Check your network settings.\n", $TRUE);
      return $ERROR;
   }

   print("   FQDN (Fully Qualified Domain Name) = $data{'fqdnTlan'} \n\n");

   #Assign FQDN to selected interface
   my $fqdn_interface = '2';

   #wait for valid input.
   while (($fqdn_interface ne '1') and ($fqdn_interface ne '0'))
   {
      $fqdn_interface = getTextResponse(" Do you wish to assign FQDN to TLAN IP or ELAN  (0: ELAN; 1:TLAN)?", '1');
      if (($fqdn_interface ne '1') and ($fqdn_interface ne '0'))
      {
         print "You provided incorrect value. Only '0' and '1' are supported.\n";
      }
   } ## end while (($fqdn_interface ne...

   baselog($LOG_INFO, "User selected '$fqdn_interface' as FQDN interface.");

   # 0 is ELAN
   if ($fqdn_interface eq '0')
   {
      if (do_system([$SUDO, $SUDO_NORTEL, $UPDATE_HOSTS, $data{'machineIP'}, $data{'fqdnTlan'}]))
      {
         print "Now FQDN is associated with ELAN IP.\n\n";
         baselog($LOG_INFO, 'Changes successfully applied.');
      }
      else
      {
         baselog($LOG_ERROR, "Cannot update 'hosts' file.\n", $TRUE);
         $retval = $ERROR;
      }
   } ## end if ($fqdn_interface eq...
     #if not '0' then use TLAN by default
   else
   {
      if (do_system([$SUDO, $SUDO_NORTEL, $UPDATE_HOSTS, $data{'machineIPTlan'}, $data{'fqdnTlan'}]))
      {
         print "Now FQDN is associated with TLAN IP.\n\n";
         baselog($LOG_INFO, 'Changes successfully applied.');
      }
      else
      {
         baselog($LOG_ERROR, "Cannot update 'hosts' file.\n", $TRUE);
         $retval = $ERROR;
      }

   } ## end else [ if ($fqdn_interface eq...

   return $retval;
} ## end sub fqdnreassign()
