#!/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
}

APP_DATA=12
ATTACH="attach ${APP_DATA}"

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

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

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

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

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

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

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

#设置准慢盘一个月内允许诊断次数 并持久化
BDM_SETDISKSLOWDIAGPERMITCNT()
{
    if [ $1 -gt '5' ] || [ $1 -le '0' ]; then
        echo -e "The input permit count is not correct(1-5)."
        exit 0
    fi
    
	L_EXEC_FLAG="drm_slow_disk setdiskdiagdetectpermitcnt -c"
    L_EXEC_FLAG1="econf monmodifycfg BDM SlowDiskDiagAllowCount"
	echo -e "${ATTACH}\n${L_EXEC_FLAG} $1\nexit\n" | diagnose_usr --auto
    echo -e "exit"
    echo -e "${L_EXEC_FLAG1} $1\nexit\n" | diagnose --auto
}

#设置准慢盘404诊断中读写测试的慢周期阈值 并持久化
BDM_SETSLOWDISKTIMETHREDINDIAG()
{
    if [ $1 -ne '1' ] && [ $1 -ne '2' ] && [ $1 -ne '3' ]; then
        echo -e "The disk type is not correct, please check: 1:SAS_HDD, 2:NL_SAS_HDD, 3:SSD."
        exit 0
    fi

    if [ $2 -gt '60000' ] || [ $2 -lt '100' ]; then
        echo -e "Delay time is not in range 100us-60000us!."
        exit 0
    fi
    
    if [ $1 == "1" ]; then
        ITEM_NAME="SlowDiskDiagAvgDelayTypeSASHDD"
    elif [ $1 == "2" ]; then
        ITEM_NAME="SlowDiskDiagAvgDelayTypeNLSASHDD"
    elif [ $1 == "3" ]; then
        ITEM_NAME="SlowDiskDiagAvgDelayTypeSSD"
    fi
	L_EXEC_FLAG="drm_slow_disk setslowdisktimethresholdindiag -d $1 -t $2"
    L_EXEC_FLAG1="econf monmodifycfg BDM ${ITEM_NAME} $2"
	echo -e "${ATTACH}\n${L_EXEC_FLAG}\nexit\n" | diagnose_usr --auto
    echo -e "exit"
    echo -e "${L_EXEC_FLAG1}\nexit\n" | diagnose --auto
}

#查询准慢盘一个月内允许诊断次数
BDM_SHOWDISKSLOWDIAGPERMITCNT()
{
	L_EXEC_FLAG="drm_slow_disk showdiskdiagdetectpermitcnt"
	echo -e "${ATTACH}\n${L_EXEC_FLAG}\nexit\n" | diagnose_usr --auto
}

#查询准慢盘404诊断中读写测试的慢周期阈值
BDM_SHOWSLOWDISKTIMETHREDINDIAG()
{
	L_EXEC_FLAG="drm_slow_disk showslowdisktimethresholdindiag"
	echo -e "${ATTACH}\n${L_EXEC_FLAG}\nexit\n" | diagnose_usr --auto
}

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

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

#查看系统盘上报故障开关
BDM_SHOWREPORTSWICH()
{
    L_EXEC_FLAG="hdm rootdisk showreportswitch"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
}
 
#设置系统盘上报故障开关             
BDM_SETREPORTSWICH()
{
    L_EXEC_FLAG="hdm rootdisk setreportswitch $1"
    L_EXEC_FLAG1="econf monmodifycfg BDM DpRtReportFaultSwitch $1"
    echo -e "${L_EXEC_FLAG}\nexit\n" | diagnose --auto
    echo -e "${L_EXEC_FLAG1}\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 setdiskdiagdetectpermitcnt 'count'                         : set disk diagnose detect permit count(1-5)."
            echo -e "bdm_diskbomlist.sh setslowdisktimethresholdindiag 'disktype' 'time'           : set time threshold in slow disk diag,diskType(1:SAS 2:NL_SAS 3:SSD),time(100us-60000us)."
            echo -e "bdm_diskbomlist.sh showdiskdiagdetectpermitcnt                                : show disk diagnose detect permit count."
            echo -e "bdm_diskbomlist.sh showslowdisktimethresholdindiag                            : show time threshold in slow disk diag."
            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 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
	;;
    setdiskdiagdetectpermitcnt)
        #第二个参数是空或者第三个参数不为空，则参数错误
		if [ ! -n "$2" ] || [ -n "$3" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_SETDISKSLOWDIAGPERMITCNT $2
		exit 0
	;;
	setslowdisktimethresholdindiag)
        #第二个参数或第三个参数是空，则参数错误
		if [ ! -n "$2" ] || [ ! -n "$3" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_SETSLOWDISKTIMETHREDINDIAG $2 $3
		exit 0
	;;
	showdiskdiagdetectpermitcnt)
        #第二个参数不为空，则参数错误
		if [ -n "$2" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_SHOWDISKSLOWDIAGPERMITCNT
		exit 0
	;;
	showslowdisktimethresholdindiag)
        #第二个参数不为空，则参数错误
		if [ -n "$2" ]
		then
			echo "usage: bdm_diskbomlist.sh -a"
			exit 1
		fi
		BDM_SHOWSLOWDISKTIMETHREDINDIAG
		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
	;;
    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
