#!/bin/bash
#
# Name: scr
# Copyright: (c)Copyright 2020-2021 Hewlett Packard Enterprise Company, L.P. 
#
# Description: Configure NFC Host on the target system
#
# Modification History
#
# ARAVINDA	08/10/20 Initial Development

#set -x

#
# Defines
#

ARCH="`uname -m`"
rstatus=0
fn=nfc_configuration_mkv.dat
nfc_configfilefound="false"
nfc_autoregmode="true"
nfc_ipv6mode="false"

string1="[<ENABLEAUTOREGMODE>]"
string2="[<ENABLEIPV6MODE>]"

# 
# Functions
#

# Name: PrintHelp
# Description: Prints a help message
# In: None
# Out: None
# Returns: None
PrintHelp () {
	echo "NAME"
	echo ""
	echo "nfc__host_configuration"
	echo ""
	echo "DESCRIPTION"
	echo ""
	echo " Configure NFC Host on the target system"
	echo ""
	echo "OPTIONS"
	echo ""
	echo "-h, --help - Prints help message"
}

# Name: ReadArgs
# Description: Parse command line arguments
# In: Command line arguments
# Out: None
# Returns: None
ReadArgs () {
	for i in $*
	do
		if [ "$i" = "-h" ] || [ "$i" = "--help" ]
		then
			PrintHelp
			exit 0
		fi
	done
}

ReadConfigFile () {

if [ -f "$fn" ]; then

 nfc_configfilefound="true"
 echo "NFC Host configuration file exists!"

 # The format of the config file is:
 #		parameter
 #		paramter value
 #
 # i.e. [<enableautoregmode>]
 #	Yes
 #
 # value can be Yes,No

 exec<$fn

 while read line 
 do

   #echo "line read = $line"

   # Force the line to uppercase
   uline=$(echo $line | tr '[:lower:]' '[:upper:]')

   #echo "Line converted to upper case = $uline"
   #echo "1st parameter string to compare line to = $string1"

   # If line = enableautoregmode then ENABLEAUTOREGMODE found

   # echo " uline details:"
   # echo "$uline" | od -bc

    if [ "$uline" == "$string1" ]
    then

      echo "Found ENABLEAUTOREGMODE parameter"

      # Extract the EnableAutoReg Modeflag value (YES|NO)? that's on the next line
      echo "Reading next line for EnableAutoRegMode value..."
      read line

      # Force the line to uppercase
      uline=$(echo $line | tr '[:lower:]' '[:upper:]')
      ans=${uline}

      echo "EnableAutoRegMode 2nd line value = $ans"
      if [ $ans == "Y" ] || [ $ans == "YES" ]
      then
        nfc_autoregmode="true"
      else
        nfc_autoregmode="false"
      fi
      echo "ENABLEAUTOREGMODE value set = $nfc_autoregmode"
   fi

   #echo "Checking line against 2nd parameter"
   #echo "2nd parameter string to compare line to = $string2"

   # If line = enableipv6mode then ENABLEIPV6MODE found
   if [ "$uline" == $string2 ]
   then
      echo Found ENABLEIPV6MODE parameter
      echo "Reading next line for enableipv6mode value"

      # Extract the Enable IPV6 Mode flag value (Y|N)? that's on the next line
      read line

      # Force the line to uppercase
      uline=$(echo $line | tr '[:lower:]' '[:upper:]')
      ans=${uline}

      echo "EnableIPV6Mode 2nd line value = $ans"
      if [ $ans == "Y" ] || [ $ans == "YES" ]
      then
        nfc_ipv6mode="true"
      else
         nfc_ipv6mode="false"
      fi
      echo "ENABLEIPV6MODE=$nfc_ipv6mode"
   fi
 done
else
  nfc_configfilefound="false"
  echo "The required configuration file $fn does not exist!"
  echo "Default values will be set..."
  #exit 0 
  rstatus=0
fi

}

#
# Script Main
#

# Read command line arguments
ReadArgs $*

echo "Main() in nfc_configuration"

ReadConfigFile

if [ "$nfc_configfilefound" == "true" ]
then
   if [ "$nfc_autoregmode" == "true" ]
   then
       echo "User wants to enable autoregmode"
       if [ "$nfc_ipv6mode" == "true" ]
       then
          echo "and enable IPV6, so enabling Autoregmode and IPV6"
          ./nfc_param_set auto enable                 
          rstatus=1
       else
          echo "and disable IPV6, so enabling Autoregmode and disabling IPV6"
	  ./nfc_param_set auto disable
          rstatus=1
       fi
    else
       echo "User wants to disable autoregmode."
       if [ "$nfc_ipv6mode" == "true" ]
       then
          echo "and enable IPV6, so enabling Manualregmode and IPV6"
          ./nfc_param_set manual enable 
          rstatus=1
       else
          echo "and disable IPV6 so enabling Manualregmode and disabling IPV6"
          ./nfc_param_set manual disable
          rstatus=1
       fi
   fi
else
   echo "No configuration file found. Default configuration will be configured."
   ./nfc_param_set auto disable
fi
exit $rstatus
