#!/bin/bash
#bdm在minisystem下的命令脚本

#检查是否已经加载BDM的sd的KO
CHECK_KO_INSMOD()
{
	L_INSMOD_SD=`lsmod |awk '{print $1}' | grep 'sd$'`
	if [ -z "$L_INSMOD_SD" ]; then
		echo 'The sd module is not insmod, the command do not execute.'
		exit 1
	fi
}

#显示硬盘bom编码
BDM_SHOWDISKBOM()
{
	L_EXEC_FLAG="sddebug showdisk"
	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#显示黑白名单中的硬盘编码
BDM_SHOWBOMCODE()
{
	L_EXEC_FLAG="sddebug showbomcode"
	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}
#在白名单中增加bom编码
BDM_ADDBOMTOWL()
{
	L_EXEC_FLAG="sddebug opbomlist add white"
	echo -e "${L_EXEC_FLAG} $1\nexit\n" | diagnose --auto
}

#在白名单中删除bom编码
BDM_DELBOMTOWL()
{
	L_EXEC_FLAG="sddebug opbomlist delete white"
	echo -e "${L_EXEC_FLAG} $1\nexit\n" | diagnose --auto
}

#在黑名单中增加bom编码
BDM_ADDBOMFROMBL()
{
	L_EXEC_FLAG="sddebug opbomlist add black"
	echo -e "${L_EXEC_FLAG} $1\nexit\n" | diagnose --auto
}

#在黑名单中删除bom编码
BDM_DELBOMFROMBL()
{
	L_EXEC_FLAG="sddebug opbomlist delete black"
	echo -e "${L_EXEC_FLAG} $1\nexit\n" | diagnose --auto
}

#在黑名单中清除所有bom编码
BDM_CLEARBOMFROMWL()
{
	L_EXEC_FLAG="sddebug clearbomlist white"
	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#在白名单中清除所有bom编码
BDM_CLEARBOMFROMBL()
{
	L_EXEC_FLAG="sddebug clearbomlist black"
	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#查询当前SSD盘超时调整开关状态
BDM_SHOWSWITCH()
{
	L_EXEC_FLAG="sddebug showio 0"
	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#设置SSD盘超时调整开关状态
BDM_SETSWITCH()
{
	L_EXEC_FLAG="sddebug showio 1"
	echo -e "${L_EXEC_FLAG} $1\nexit\n" | diagnose --auto
}

#显示慢盘诊断单盘每月诊断限制次数和读写测试判定门限
BDM_SHOWSLOWDISKDIAGPARAM()
{
	L_EXEC_FLAG="drm_slow_disk showdiskdiagdetectpermitcnt"
	echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#设置慢盘诊断单盘每月诊断限制次数
BDM_SETSLOWDISKDIAGALLOWCNT()
{
	L_EXEC_FLAG="drm_slow_disk setdiskdiagdetectpermitcnt -c "
	echo -e "${L_EXEC_FLAG} $1\nexit\n" | diagnose --auto
}

#设置慢盘诊断读写测试门限值
BDM_SETSLOWDISKDIAGTHRESHOLD()
{
	L_EXEC_FLAG="drm_slow_disk setdiskdiagdetectpermitcnt -d "
    L_EXEC_FLAG2=" -t "
	echo -e "${L_EXEC_FLAG} $1 ${L_EXEC_FLAG2} $2\nexit\n" | diagnose --auto
}

#查看系统盘上报故障开关
BDM_SHOWREPORTSWICH()
{
    L_EXEC_FLAG="hdm physwitch showreportswitch"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#设置系统盘上报故障开关	 	 
BDM_SETREPORTSWICH()
{
    L_EXEC_FLAG="hdm physwitch setreportswitch $1"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}

#检查函数是否加载
CHECK_KO_INSMOD

while getopts ":ha" options
do
    case "$options" in
        h)	
            echo -e "bdm_diskbomlist.sh [-h] | [-a] : It is just a BDM command. Enter "bdm_diskbomlist.sh -a" to obtain more information."
            exit 0
            ;;
        a) 
            echo -e "bdm_diskbomlist.sh showdisk                                      : Shows system disk information."
            echo -e "bdm_diskbomlist.sh showbomcode                                   : Shows information about the BOM blacklist and BOM whitelist."
            echo -e "bdm_diskbomlist.sh resetwhitebom                                 : Restore the initial configuration of a BOM whitelist."
            echo -e "bdm_diskbomlist.sh resetblackbom                                 : Restore the initial configuration of a BOM blacklist."
            echo -e "bdm_diskbomlist.sh addwhitebom 'bomcode'                         : Adds a disk item in the BOM whitelist."
            echo -e "bdm_diskbomlist.sh delwhitebom 'bomcode'                         : Deletes a disk item from the BOM whitelist."
            echo -e "bdm_diskbomlist.sh addblackbom 'bomcode'                         : Adds a disk item to the BOM blacklist."
            echo -e "bdm_diskbomlist.sh delblackbom 'bomcode'                         : Deletes a disk item from the BOM blacklist."
            echo -e "bdm_diskbomlist.sh showswitch                                    : Get current timeout adjust switch."
            echo -e "bdm_diskbomlist.sh setswitch 'switch'                            : Set timeout adjust switch 0:close|1:open"
	        echo -e "bdm_diskbomlist.sh setslowdiskdiagallowcnt  'times'              : Set slow disk diagnose permit count times(1-5)."
            echo -e "bdm_diskbomlist.sh showslowdiskdiagparam                         : Show slow disk diagnose parameters."
            echo -e "bdm_diskbomlist.sh setslowdiskdiagthreshold 'diskType'  'time'   : Set slow disk rw test threshold,param1:diskType(1:SAS HDD,2:NL_SAS HDD,3:SSD),param2:time(100us-60000us)."
            echo -e "bdm_diskbomlist.sh showreportswitch                              : show the root disk report falut switch."
            echo -e "bdm_diskbomlist.sh setreportswitch 'value'                       : set the root disk report falut switch."
            exit 0
            ;;		    
        \?)
            echo "usage: bdm_diskbomlist.sh -h"
            exit 1
            ;;
    esac
done

case "$1" in
	showdisk)
		if [ -n "$2" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi

		BDM_SHOWDISKBOM 
        exit 0
	;;
	showbomcode)
		if [ -n "$2" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_SHOWBOMCODE 
		exit 0
	;;
	addwhitebom)
		#第三个参数不是空，或第二个参数是空，则参数错误
		if [ -n "$3" ] || [ ! -n "$2" ] 
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_ADDBOMTOWL $2
		exit 0
	;;
	delwhitebom)
		#第三个参数不是空，或第二个参数是空，则参数错误
		if [ -n "$3" ] || [ ! -n "$2" ] 
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_DELBOMTOWL $2
		exit 0
	;;
	addblackbom)
		#第三个参数不是空，或第二个参数是空，则参数错误
		if [ -n "$3" ] || [ ! -n "$2" ] 
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_ADDBOMFROMBL $2 
		exit 0
	;;
	delblackbom)
		#第三个参数不是空，或第二个参数是空，则参数错误
		if [ -n "$3" ] || [ ! -n "$2" ] 
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_DELBOMFROMBL $2 
		exit 0
	;;
	resetwhitebom)
		if [ -n "$2" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_CLEARBOMFROMWL
		exit 0
	;;
	resetblackbom)
		if [ -n "$2" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_CLEARBOMFROMBL
		exit 0
	;;
	showswitch)
		if [ -n "$2" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_SHOWSWITCH
		exit 0
	;;
	setswitch)
		#第三个参数不是空，或第二个参数是空，则参数错误
		if [ -n "$3" ] || [ ! -n "$2" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_SETSWITCH $2
		exit 0
	;;
	showslowdiskdiagparam)
		if [ -n "$2" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_SHOWSLOWDISKDIAGPARAM
		exit 0
	;;
        setslowdiskdiagallowcnt)
		#第三个参数不是空，或第二个参数是空，则参数错误
		if [ -n "$3" ] || [ ! -n "$2" ] 
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_SETSLOWDISKDIAGALLOWCNT $2 
		exit 0
	;;
        setslowdiskdiagthreshold)
		#第三个参数不是空，或第二个参数是空，则参数错误
		if [ -n "$4" ] || [ ! -n "$3" ] || [ ! -n "$2" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_SETSLOWDISKDIAGTHRESHOLD $2 $3 
		exit 0
	;;
        showreportswitch)
        if [ -n "$2" ]
        then
                echo "usage: bdm_diskbomlist.sh -a"
                exit 1
        fi
        BDM_SHOWREPORTSWICH
        exit 0
    ;;
        setreportswitch)
        if [ ! -n "$2" ] #第2个参数为空则参数错误
        then
                echo "usage: bdm_diskbomlist.sh -a"
                exit 1
        fi
        BDM_SETREPORTSWICH $2
        exit 0
    ;;    
    *)
        echo "usage: bdm_diskbomlist.sh -a"
esac

exit 1
