#!/bin/bash

VERSION=1.0.0
function printHelp()
{
    echo "$VERSION"
    echo "*************************************************************************
	Usage:./updateLnx.sh  <BIOS.bin> [param]|[...]
    command:
    ------------------------------------------------------------
	|    Afu_Tools    |    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

	
  Note: -h|--help for this help. 
*************************************************************************"

}

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


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

		if [ $# -eq 1 ] ; then 
			res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
			echo "preserve $res"
			
			CMD="/b /p /n /k /l /x /ME "

			echo $CMD
			chmod 777 afulnx_64
			./afulnx_64 $1 $CMD
			echo "$?"
			
		elif [ $# -eq 2 ] ; then
			if [ $2 == "/ME" ]  || [ $2 == "/me" ]  ; then 
				CMD="/b /p /n /k /l /x /ME "
				res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			elif [ $2 == "/NOME" ] || [ $2 == "/nome"  ] ; then
				CMD="/b /p /n /k /l /x "
				res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			elif [ $2 == "/NP" ] || [ $2 == "/np" ]  ; then 
				CMD="/b /p /n /k /l /x /ME "
				echo "NONE preserve "
			elif [ $2 == "/P" ] || [ $2 == "/p" ] ; then
				CMD="/b /p /n /k /l /x /ME "
				res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			else
				CMD="/b /p /n /k /l /x /ME "
				res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			fi

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

		elif [ $# -eq 3 ] ; then
			if [ $2 == "/P" ] || [ $2 == "/p" ] || [ $3 == "/P" ] || [ $3 == "/p" ] ; then 
				res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			elif [ $2 == "/NP" ] || [ $2 == "/np" ] || [ $3 == "/NP" ] || [ $3 == "/np" ] ; then 
				echo "NONE Preserve "
			else
			    res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			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 "
			else
				CMD="/b /p /n /k /l /x /ME "
			fi

			echo $CMD
			chmod 777 afulnx_64
			./afulnx_64 $1 $CMD
			echo "$?"
			
		else
			printHelp
			exit
		fi

    else
	
		echo "32 $@"
		
		if [ $# -eq 1 ] ; then 
			res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
			echo "preserve $res"
			
			CMD="/b /p /n /k /l /x /ME "

			echo $CMD
			chmod 777 afulnx_32
			./afulnx_32 $1 $CMD
			echo "$?"
			
		elif [ $# -eq 2 ] ; then
			if [ $2 == "/ME" ]  || [ $2 == "/me" ]  ; then 
				CMD="/b /p /n /k /l /x /ME "
				res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			elif [ $2 == "/NOME" ] || [ $2 == "/nome"  ] ; then
				CMD="/b /p /n /k /l /x "
				res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			elif [ $2 == "/NP" ] || [ $2 == "/np" ]  ; then 
				CMD="/b /p /n /k /l /x /ME "
				echo "NONE preserve "
			elif [ $2 == "/P" ] || [ $2 == "/p" ] ; then
				CMD="/b /p /n /k /l /x /ME "
				res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			else
				CMD="/b /p /n /k /l /x /ME "
				res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			fi

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

		elif [ $# -eq 3 ] ; then
			if [ $2 == "/P" ] || [ $2 == "/p" ] || [ $3 == "/P" ] || [ $3 == "/p" ] ; then 
				res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			elif [ $2 == "/NP" ] || [ $2 == "/np" ] || [ $3 == "/NP" ] || [ $3 == "/np" ] ; then 
				echo "NONE Preserve "
			else
			    res=`ipmitool raw 0x3c 0x4a 0x0b 0x01 0x20 0x00`
				echo "preserve $res"
			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 "
			else
				CMD="/b /p /n /k /l /x /ME "
			fi

			echo $CMD
			chmod 777 afulnx_32
			./afulnx_32 $1 $CMD
			echo "$?"
			
		else
			printHelp
			exit
		fi

    fi
}

function DMIEDIT()
{
    echo "DMIEDIT"
    if [ `getconf WORD_BIT` -eq 32 ] && [ `getconf LONG_BIT` -eq 64 ] ; then
        echo "64 $@"
	
	#chmod 777 DMIEDIT/DmiEditLnx64_V5.18/DmiEditLnx64/amidelnx_26_64
	#./DMIEDIT/DmiEditLnx64_V5.18/DmiEditLnx64/amidelnx_26_64 $@
    else
	echo "32 $@"
	#chmod 777 DMIEDIT/DmiEditLnx32_V5.18/DmiEditLnx32/amidelnx_26_32
	#./DMIEDIT/DmiEditLnx32_V5.18/DmiEditLnx32/amidelnx_26_32 $@

    fi
}

function SCE_Tools()
{
    echo "SCE_Tools"
    if [ `getconf WORD_BIT` -eq 32 ] && [ `getconf LONG_BIT` -eq 64 ] ; then
        echo "64 $@"
	#chmod 777 SCE_Tools/SceLnx64/SCELNX_64
	#./SCE_Tools/SceLnx64/SCELNX_64 $@
    else
	echo "Error: 32 $@"
	
    fi
}

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

