#!/bin/bash
#
# Name: elx_ss_enablement
# Copyright: (c)Copyright 2015-2016 Hewlett Packard Enterprise Company, L.P. 
#
# Description: Enable/Disable Smart SAN on the target system
#
# Modification History
#
# CAD	11/2/15 Initial Development

#set -x

#
# Defines
#

ARCH="`uname -m`"
rstatus=0
fn=esmartsan_mkv.dat
elx_configfilefound="true"
elx_fcsmartsanenable="false"
elx_rebuildramdisk="true"
elx_fcsmartsanstatus="false"
elx_fcsmartsansupported="true"

string1="[<ENABLEFCSMARTSAN>]"
string2="[<REBUILDRAMDISK>]"
estring="SmartSAN configured: Enabled"
dstring="SmartSAN configured: Disabled"
unstring="SmartSAN not configured"

# 
# Functions
#

# Name: PrintHelp
# Description: Prints a help message
# In: None
# Out: None
# Returns: None
PrintHelp () {
	echo "NAME"
	echo ""
	echo "elx_ss_enablement"
	echo ""
	echo "DESCRIPTION"
	echo ""
	echo " Enables/disables Smart SAN on the target server"
	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

 echo "Emulex Smart SAN configuration file exists!"

 # The format of the config file is:
 #		parameter
 #		paramter value
 #
 # i.e. [<fcsmartsanenabled>]
 #	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 = enablefcsmartsan then ENABLEFCSMARTSAN found

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

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

      echo "Found ENABLEFCSMARTSAN parameter"

      # Extract the SmartSAN enable flag value (YES|NO)? that's on the next line
      echo "Reading next line for EnableFCSmartSAN  value..."
      read line

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

      echo "EnableFCSmartSAN 2nd line value = $ans"
      if [ $ans == "Y" ] || [ $ans == "YES" ]
      then
        elx_fcsmartsanenable="true"
      else
        elx_fcsmartsanenable="false"
      fi
      echo "ENABLEFCSMARTSAN value set = $elx_fcsmartsanenable"
   fi

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

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

      # Extract the Rebuild RAM disk enable 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 "RebuildRamDisk 2nd line value = $ans"
      if [ $ans == "Y" ] || [ $ans == "YES" ]
      then
        elx_rebuildramdisk="true"
      else
         elx_rebuildramdisk="false"
      fi
      echo "REBUILDRAMDISK=$elx_rebuildramdisk"
   fi
 done
else
  elx_configfilefound="false"
  echo "The required configuration file $fn does not exist!"
  echo "No action taken..."
  #exit 0 
  rstatus=0
fi

}

GetStatus() {

outfile=elxsmartsansout.txt

echo "GetStatus()"

./SmartSAN.sh --state  > $outfile
status=$?

echo "Get State command status = $status"

if [ $status == 2 ] 
then
    echo "Emulex Smart SAN functionality is not supported by the installed Emulex fibre channel driver"
    elx_fcsmartsansupported="false"

    # delete the temporary file
    rm $outfile
else
    ./SmartSAN.sh --status > $outfile
    status=$?
    
    echo "Get Status command status = $status"

    if [ -f "$outfile" ]; then

        #echo "Emulex SmartSAN temporary output file exists!"

        # The format of the file for the get status command is:
        #
        # SmartSAN configured: Enabled
        # SmartSAN configured: Disabled
        # SmartSAN is not configured

        exec<$outfile

        read line
        #echo "line read = $line"

        if [ "$line" == "$estring" ]
        then
            echo "Emulex Smart SAN is enabled"
            elx_fcsmartsanstatus="true"
        elif [ "$line" == "$dstring" ]
        then
            echo "Emulex Smart SAN is disabled"
            elx_fcsmartsanstatus="false"
        elif [ "$line" == "$unstring" ]
        then
            echo "Emulex Smart SAN not configured"
            elx_fcsmartsanstatus="false"
        else
            echo "Emulex Smart SAN is not configured/supported by the Emulex fibre channel driver"
            elx_fcsmartsansupported="false"
        fi

        # delete the temporary file
        rm $outfile
    fi
fi
}

#
# Script Main
#

# Read command line arguments
ReadArgs $*

echo "Main() in elx_ss_enablement"

GetStatus

if [ "$elx_fcsmartsansupported" == "true" ]
then

    echo "Emulex Smart SAN functionality is supported"
    ReadConfigFile

    # If SmartSAN is supported, check to see if the user wants it enabled.  If so, and not already enabled,
    # send command to enable

    if [ "$elx_configfilefound" == "true" ]
    then
        echo "Configuration file found"

        if [ "$elx_fcsmartsanenable" == "true" ]
        then
            echo "User wants to enable Smart SAN.  Checking if already enabled."
            if [ "$elx_fcsmartsanstatus" == "true" ]
            then
                echo "Installation success"
                echo "Emulex Smart SAN already enabled.  No action needed."
                # exit no operation
                #exit 0
                rstatus=0
            else
                echo "Emulex Smart SAN not enabled.  Sending command to enable."
                # check if user wants to rebuild the ram disk
                if [ "$elx_rebuildramdisk" == "true" ]
                then
                    ./SmartSAN.sh --enable --ramdisk
                    status=$?
                    echo "Command status = $status"
                    echo ""
                    if [ "$status" == 0 ]
                    then
                        echo "Installation success"
                        echo "Please restart your server for the changes to take effect"
                        mkdir -p /var/cpq
                        touch /var/cpq/hpe-rpm-reboot-required
                        #exit 0
                        rstatus=0
                    else
                        echo "Installation failed"
                        echo "Enable Smart SAN command with rebuild ram disk failed"
                        #exit 1
                        rstatus=1
                    fi
                else
                    ./SmartSAN.sh --enable 
                    status=$?
                    echo "Command status = $status"
                    echo ""
                    if [ "$status" == 0 ]
                    then
                        echo "Installation success"
                        echo "Please rebuild the ram disk and restart your server for the changes to take effect"
                        #exit 0
                        rstatus=0
                    else
                        echo "Installation failed"
                        echo "Enable Smart SAN command without rebuilding ram disk failed"
                        #exit 1
                        rstatus=1
                    fi
                fi
            fi
         else
            echo "User wants to disable Smart SAN.  Checking if already disabled."
            if [ "$elx_fcsmartsanstatus" == "false" ]
            then
                echo "Installation success"
                echo "Emulex Smart SAN already disabled.  No action needed."
                # exit no operation
                #exit 0
                rstatus=0
            else
                echo "Emulex Smart SAN enabled.  Sending command to disable."
                # check if user wants to rebuild the ram disk
                if [ "$elx_rebuildramdisk" == "true" ]
                then
                    ./SmartSAN.sh --disable --ramdisk
                    status=$?
                    echo "Command status = $status"
                    echo ""
                    if [ "$status" == 0 ]
                    then
                        echo "Installation success"
                        echo "Please restart your server for the changes to take effect"
                        mkdir -p /var/cpq
                        touch /var/cpq/hpe-rpm-reboot-required
                        #exit 0
                        rstatus=0
                    else
                        echo "Installation failed"
                        echo "Disable Smart SAN command with rebuild ram disk failed"
                        #exit 1
                        rstatus=1
                    fi
                else
                    ./SmartSAN.sh --disable 
                    status=$?
                    echo "Command status = $status"
                    echo ""
                    if [ "$status" == 0 ]
                    then
                        echo "Installation success"
                        echo "Please rebuild the ram disk and restart your server for the changes to take effect"
                        #exit 0
                        rstatus=0
                    else
                        echo "Installation failed"
                        echo "Disable Smart SAN command without rebuilding ram disk failed"
                        #exit 1
                        rstatus=1
                    fi
                fi
            fi
        fi
    else
        echo "Installation success"
        echo "No configuration file found.  Nothing to do."
        if [ "$elx_fcsmartsanstatus" == "true" ]
        then
            echo "Current Emulex Smart SAN status: enabled"
        else
            echo "Current Emulex Smart SAN status: disabled"
        fi
        #exit 0
        rstatus=0
    fi
else 
    echo "Installation success"
    echo "Emulex Smart SAN functionality is not supported currently, but the enablement kit has been installed for future use."
    echo "May need to install an Emulex Smart SAN enabled driver, and then activate Smart SAN functionality"
    #exit 0
    rstatus=0
fi

echo "Installation complete"

exit $rstatus



