#!/bin/bash
#
# Name: ql_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	09/25/15 Initial Development

#set -x
#set -o xtrace

#
# Defines
#

ARCH="`uname -m`"
rstatus=0
fn=qsmartsan_mkv.dat
ql_configfilefound="true"
ql_fcsmartsanenable="false"
ql_rebuildramdisk="true"
ql_fcsmartsanstatus="false"
ql_fcsmartsansupported="true"

string1="[<ENABLEFCSMARTSAN>]"
string2="[<REBUILDRAMDISK>]"
estring="/sys/module/qla2xxx/parameters/ql2xsmartsan: 1"
dstring="/sys/module/qla2xxx/parameters/ql2xsmartsan: 0"
unstring="ql2xsmartsan not supported by this qla2xxx"

# 
# Functions
#

# Name: PrintHelp
# Description: Prints a help message
# In: None
# Out: None
# Returns: None
PrintHelp () {
	echo "NAME"
	echo ""
	echo "ql_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 () {

echo "ReadConfigFile()"

if [ -f "$fn" ]; then

 echo "QLogic Smart SAN configuration file exists!"

 # The format of the config file is:
 #		parameter
 #		paramter value
 #
 # i.e. [<enablefcsmartsan>]
 #	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
        ql_fcsmartsanenable="true"
      else
         ql_fcsmartsanenable="false"
      fi
      echo "ENABLEFCSMARTSAN value set = $ql_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 (YES|NO)? 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
        ql_rebuildramdisk="true"
      else
         ql_rebuildramdisk="false"
      fi
      echo "REBUILDRAMDISK=$ql_rebuildramdisk"
   fi
 done
else
  ql_configfilefound="false"
  echo "The required configuration file $fn does not exist!"
  echo "No action taken..."
  #exit 0 
  rstatus=0
fi

}

GetStatus() {

outfile=qlsmartsansout.txt

echo "GetStatus()"

./qlssen.sh 0 state > $outfile 
status=$?
echo "Get State command status = $status"

if [ -f "$outfile" ]; then

 #echo "QLogic SmartSAN temporary output file exists!"

 # The format of the file for the get status command is:
 #
 #	/sys/module/qla2xxx/parameters/ql2xsmartsan: x 
 #
 #    0 = not enabled
 #    1 = enabled
 #      = not supported

 exec<$outfile

 read line
 #echo "line read = $line"

 if [ "$line" == "$estring" ]
 then
    echo "QLogic Smart SAN is enabled"
    ql_fcsmartsanstatus="true"
 elif [ "$line" == "$dstring" ]
 then
    echo "QLogic Smart SAN is disabled"
    ql_fcsmartsanstatus="false"
 elif [ "$line" == "$unstring" ]
 then
    echo "QLogic Smart SAN functionality is not supported by the installed QLogic fibre channel driver"
    ql_fcsmartsansupported="false"
 else
    echo "QLogic Smart SAN functionality is not supported by the installed QLogic fibre channel driver"
    ql_fcsmartsansupported="false"
 fi

 # delete the temporary file
 rm $outfile

fi
}

#
# Script Main
#

# Read command line arguments
ReadArgs $*

echo "Main() in ql_ss_enablement"

GetStatus

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

    echo "QLogic 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 [ "$ql_configfilefound" == "true" ]
    then
        echo "Configuration file found"

        if [ "$ql_fcsmartsanenable" == "true" ]
        then
            echo "User wants to enable Smart SAN.  Checking if already enabled."
            if [ "$ql_fcsmartsanstatus" == "true" ]
            then
                echo "Installation success"
                echo "QLogic Smart SAN already enabled.  No action needed."
                # exit no operation
                #exit 0 
                rstatus=0 
            else
                echo "QLogic Smart SAN not enabled.  Sending command to enable."
                # check if user wants to rebuild the ram disk
                if [ "$ql_rebuildramdisk" == "true" ]
                then
                    ./qlssen.sh 1
                    status=$?
                    echo "Command status = $status"
                    echo ""
                    if [ "$status" == 2 ]
                    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
                    ./qlssen.sh 1 nomake       
                    status=$?
                    echo "Command status = $status"
                    echo ""
                    if [ "$status" == 3 ]
                    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 [ "$ql_fcsmartsanstatus" == "false" ]
            then 
                echo "Installation success"
                echo "QLogic Smart SAN already disabled.  No action needed."
                # exit no operation
                #exit 0 
                rstatus=0 
            else
                echo "QLogic Smart SAN enabled.  Sending command to disable."
                # check if user wants to rebuild the ram disk
                if [ "$ql_rebuildramdisk" == "true" ]
                then
                    ./qlssen.sh 0
                    status=$?
                    echo "Command status = $status"
                    echo ""
                    if [ "$status" == 2 ]
                    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
                    ./qlssen.sh 0 nomake
                    status=$?
                    echo "Command status = $status"
                    echo ""
                    if [ "$status" == 3 ]
                    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 [ "$ql_fcsmartsanstatus" == "true" ]
        then
            echo "Current QLogic Smart SAN status: enabled"
        else
            echo "Current QLogic Smart SAN status: disabled"
        fi
        #exit 0
        rstatus=0
    fi
else
     echo "Installation success"
     echo "QLogic Smart SAN functionality is not supported currently, but the enablement kit has been installed for future use."
     echo "May need to install a QLogic Smart SAN enabled driver, and then activate Smart SAN functionality"
     #exit 0
     rstatus=0

fi

echo "Installation complete"

exit $rstatus
