#!/bin/bash

VERSION=1.2.6
function printHelp()
{
    echo "updateLnx.sh $VERSION"
    echo "*************************************************************************
    Usage:./updateLnx.sh  <BIOS.bin> [param]|[...]
    command:
    ------------------------------------------------------------
	|    updateLnx.sh    |    BIOS.bin    |    param1  |    param2  |
	------------------------------------------------------------

	
	param1:/P:Preserve BIOS Setup; /NP: Not Preserve BIOS Setup
	param2:/ME:refresh ME; /NOME:None refresh ME

	eg:
	1.AFU Preserve BIOS Setup, refresh ME
	./updateLnx.sh  BIOS.bin /P /ME
	2.AFU Preserve BIOS Setup, None refresh ME
	./updateLnx.sh  BIOS.bin /P /NOME
	3.AFU Not Preserve BIOS Setup, refresh ME
	./updateLnx.sh  BIOS.bin /NP /ME
	4.AFU Not Preserve BIOS Setup, None refresh ME
	./updateLnx.sh  BIOS.bin /NP /NOME
	5.The same instruct as 1. :
	./updateLnx.sh  BIOS.bin
	6.The same instruct as 2. :
	./updateLnx.sh  BIOS.bin /NOME
	7.The same instruct as 3. :
	./updateLnx.sh  BIOS.bin /NP
	
*************************************************************************"

}

if [ $# -eq 0 ] ; then 
    printHelp
	exit
fi

if ! [ -x "$(command -v ipmitool)" ]; then
	echo 'ERROR: ipmitool is not installed!!!'
	echo 'ERROR: Preserve BIOS Setup Failed!!!'
fi

REFRESH_ME="YES"

function Afu_Tools()
{
	
    if [ `getconf WORD_BIT` -eq 32 ] && [ `getconf LONG_BIT` -eq 64 ] ; then
        echo "Afu_Tools 64 $@"
	#var=$@
	#param1=${var:0:3}
	#echo $param1 

		if [ $# -eq 1 ] ; then 
			ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
			if [ $? -eq 0 ] ; then
				echo "Preserve BIOS Setup $res"
			else
				echo "Preserve BIOS Setup Failed $res!!!"
			fi
			CMD="/b /p /n /k /l /x /me "
			echo $CMD

			chmod 777 afulnx_64
			./afulnx_64 $1 $CMD
			echo "$?"

			echo 'WARNING : The changes of PHY take effect, System must AC power-off!!!'

			
		elif [ $# -eq 2 ] ; then
			if [ $2 == "/ME" ]  || [ $2 == "/me" ]  ; then 
				CMD="/b /p /n /k /l /x /ME "
				ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
			
	
			elif [ $2 == "/NOME" ] || [ $2 == "/nome"  ] ; then
				CMD="/b /p /n /k /l /x "
				ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then 
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
				REFRESH_ME="NONE"
				
			elif [ $2 == "/NP" ] || [ $2 == "/np" ]  ; then 
				CMD="/b /p /n /k /l /x /ME "
				echo "NONE Preserve BIOS Setup."
				
			elif [ $2 == "/P" ] || [ $2 == "/p" ] ; then
				CMD="/b /p /n /k /l /x /ME "
				ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
			else
				CMD="/b /p /n /k /l /x /ME "
				ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
			fi

			echo $CMD
			chmod 777 afulnx_64
			./afulnx_64 $1 $CMD
			echo "$?"

			if [ $REFRESH_ME == "YES" ] ; then
				echo 'WARNING : The changes of PHY take effect, System must AC power-off!!!'
			elif [ $REFRESH_ME == "NONE" ] ; then
				echo '.'
			fi


		elif [ $# -eq 3 ] ; then
			if [ $2 == "/P" ] || [ $2 == "/p" ] || [ $3 == "/P" ] || [ $3 == "/p" ] ; then 
				ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
			elif [ $2 == "/NP" ] || [ $2 == "/np" ] || [ $3 == "/NP" ] || [ $3 == "/np" ] ; then 
				echo "NONE Preserve BIOS Setup."
				
			else
			    ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
			fi

			if [ $2 == "/ME" ] || [ $2 == "/me" ] || [ $3 == "/ME" ] || [ $3 == "/me" ] ;  then 
				CMD="/b /p /n /k /l /x /ME "
				
				
			elif [ $2 == "/NOME" ] || [ $2 == "/nome" ] || [ $3 == "/NOME" ] || [ $3 == "/nome" ] ;  then 	
				CMD="/b /p /n /k /l /x "

				REFRESH_ME="NONE"
			else
				CMD="/b /p /n /k /l /x /ME "
				
				
			fi

			echo $CMD
			chmod 777 afulnx_64
			./afulnx_64 $1 $CMD
			echo " $?"

			if [ $REFRESH_ME == "YES" ] ; then
				echo 'WARNING : The changes of PHY take effect, System must AC power-off!!!'
			elif [ $REFRESH_ME == "NONE" ] ; then
				echo '.'
			fi

			
		else
			printHelp
			exit
		fi

    else
	
		echo "32 $@"
		
		if [ $# -eq 1 ] ; then 
			ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
			if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
			
			CMD="/b /p /n /k /l /x /ME "

			echo $CMD
			chmod 777 afulnx_32
			./afulnx_32 $1 $CMD
			echo "$?"

			echo 'WARNING : The changes of PHY take effect, System must AC power-off!!!'
			
		elif [ $# -eq 2 ] ; then
			if [ $2 == "/ME" ]  || [ $2 == "/me" ]  ; then 
				CMD="/b /p /n /k /l /x /ME "
				ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
				
			elif [ $2 == "/NOME" ] || [ $2 == "/nome"  ] ; then
				CMD="/b /p /n /k /l /x "
				ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
				REFRESH_ME="NONE"
				
			elif [ $2 == "/NP" ] || [ $2 == "/np" ]  ; then 
				CMD="/b /p /n /k /l /x /ME "
				echo "NONE Preserve BIOS Setup."
				
				
			elif [ $2 == "/P" ] || [ $2 == "/p" ] ; then
				CMD="/b /p /n /k /l /x /ME "
				ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
				
			else
				CMD="/b /p /n /k /l /x /ME "
				ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
			fi

			echo $CMD
			chmod 777 afulnx_32
			./afulnx_32 $1 $CMD
			echo "$?"

			if [ $REFRESH_ME == "YES" ] ; then
				echo 'WARNING : The changes of PHY take effect, System must AC power-off!!!'
			elif [ $REFRESH_ME == "NONE" ] ; then
				echo '.'
			fi


		elif [ $# -eq 3 ] ; then
			if [ $2 == "/P" ] || [ $2 == "/p" ] || [ $3 == "/P" ] || [ $3 == "/p" ] ; then 
				ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
			elif [ $2 == "/NP" ] || [ $2 == "/np" ] || [ $3 == "/NP" ] || [ $3 == "/np" ] ; then 
				echo "NONE Preserve BIOS Setup."
				
			else
			    ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00
				if [ $? -eq 0 ] ; then
					echo "Preserve BIOS Setup $res"
				else
					echo "Preserve BIOS Setup Failed $res!!!"
				fi
				
			fi

			if [ $2 == "/ME" ] || [ $2 == "/me" ] || [ $3 == "/ME" ] || [ $3 == "/me" ] ;  then 
				CMD="/b /p /n /k /l /x /ME "
				
				
			elif [ $2 == "/NOME" ] || [ $2 == "/nome" ] || [ $3 == "/NOME" ] || [ $3 == "/nome" ] ;  then 	
				CMD="/b /p /n /k /l /x "

				REFRESH_ME="NONE"
				
			else
				CMD="/b /p /n /k /l /x /ME "
				
			fi

			echo $CMD
			chmod 777 afulnx_32
			./afulnx_32 $1 $CMD
			echo "$?"

			if [ $REFRESH_ME == "YES" ] ; then
				echo 'WARNING : The changes of PHY take effect, System must AC power-off!!!'
			elif [ $REFRESH_ME == "NONE" ] ; then
				echo '.'
			fi
			
		else
			printHelp
			exit
		fi

    fi
	
}

function DMIEDIT()
{
    echo "DMIEDIT"
}

function SCE_Tools()
{
    echo "SCE_Tools"
}

Afu_Tools $@

case $1 in
     -h|--help)
	printHelp
	exit
        ;;
    0)
	var=$@
        CMD=${var:1}
	#Afu_Tools $CMD
      exit
    ;;
    1)
	var=$@
        CMD=${var:1}
	DMIEDIT $CMD
      exit
    ;;
    2)
	var=$@
        CMD=${var:1}
	SCE_Tools $CMD
     exit
    ;;
    3)
    
     exit
    ;;
esac

