#!/bin/bash

OP_SOURCE="$BASEDIR/optools"

_install() {
    if [[ $ARCH = 'aarch64' ]];then
        local pkgs='storcli smartmontools lldpd nvme-cli jq sg3_utils ipmitool'
    else
        local pkgs='perccli Arcconf MegaCli hpssacli hpssaducli ssacli ssaducli storcli smartmontools mcelog nvme-cli lldpd jq sg3_utils ipmitool'
    fi
    yum -y -q install $pkgs &> /dev/null
    yum -y -q update $pkgs &> /dev/null

    # start ipmi for after
    if [[ ! `ls /dev/ipmi* &> /dev/null` ]];then
        modprobe ipmi_devintf &> /dev/null
        modprobe ipmi_si &> /dev/null
    fi

    # add Mellanpx tools
    if [[ $ARCH = 'x86_64' ]];then
        cp -f $BASEDIR/optools/mlxup /bin/
    else
        cp -f $BASEDIR/optools/mlxup.aarch64 /bin/mlxup
    fi

    if [[ $OS == "CentOS7" ]];then
        if [[ $(lspci | grep -ci "mellanox") -ne 0 ]];then
		    yum -y -q install mft >/dev/null 2>&1
        fi
    fi

    if [[ $ARCH = 'aarch64' ]];then
        return 0
    fi

    if [[ $Usage = udisk && $SecUsage = ubs ]] \
    || [[ $Usage = ufile && $SecUsage = s3-storage ]] \
    || [[ $Usage = umem ]] \
    || [[ $Usage = uhadoop ]];then
        yum -y -q install gperftools-devel leveldb* > /dev/null 2>&1
    fi

    # add raid tools to path
    ## arcconf
    rm -f /usr/sbin/arcconf
    ln -s /usr/Arcconf/arcconf /usr/sbin/arcconf
    ## MegaCli storcli64
    rm -f /usr/sbin/MegaCli64
    ln -s  /opt/MegaRAID/MegaCli/MegaCli64  /usr/sbin/MegaCli64
    rm -f /usr/sbin/storcli64
    ln -s /opt/MegaRAID/storcli/storcli64  /usr/sbin/storcli64

    # Some OP Tools
    cp -f $OP_SOURCE/sas3ircu /opt/op
    cp -f $OP_SOURCE/sas2ircu /opt/op

    # add hba raid tools to path
    rm -f /usr/sbin/sas3ircu
    rm -f /usr/sbin/sas2ircu
    ln -s /opt/op/sas3ircu /usr/sbin/sas3ircu
    ln -s /opt/op/sas2ircu /usr/sbin/sas2ircu

    if [[ $OS == "CentOS6" && $(uname -r) =~ "2.6.32" && $Usage = uhost ]];then
        yum -y -q install microcode_ctl-1.17-25.3.el6.ucloud >/dev/null 2>&1
    fi

    # scl, devtoolset for hardware update
    if [[ $OS == "CentOS6" ]];then
	    local scl_rpms="scl-utils devtoolset-3-runtime devtoolset-3-binutils devtoolset-3-gcc devtoolset-3-libstdc++-devel devtoolset-3-gcc-c++"
	    yum -y -q install $scl_rpms >/dev/null 2>&1
    fi

    # 为 3008 卡安装额外硬盘检测工具
    if lspci -nn -d 1000:0097:0107 |grep -q 3008 ;then
        wget -q softlib.service.ucloud.cn/downloads/umonitor/ismd.tar.gz -P $BASEDIR && tar xf $BASEDIR/ismd.tar.gz -C /opt/
    fi
}


_check() {
    _check="PASS"

    # start ipmi for after
    # service ipmi start
    if [[ ! `ls /dev/ipmi* > /dev/null 2>&1` ]];then
        modprobe ipmi_devintf > /dev/null 2>&1
        modprobe ipmi_si > /dev/null 2>&1
    fi
    
    if [[ $ARCH = 'aarch64' ]] ;then
        local pkgs='storcli smartmontools lldpd nvme-cli jq sg3_utils'
    else
        local pkgs='perccli Arcconf MegaCli hpssacli hpssaducli ssacli ssaducli storcli smartmontools mcelog nvme-cli lldpd jq sg3_utils'
    fi
    for pkg in $pkgs
    do
        if ! rpm -q $pkg &> /dev/null;then
            _check="FAIL"
            PRINT_ERR "[ERROR] $pkg Not Installed."
        fi
    done

    if [[ $ARCH = 'aarch64' ]];then
        return 0
    fi

    if [[ $Usage = udisk && $SecUsage = ubs ]] \
     || [[ $Usage = ufile && $SecUsage = s3-storage ]] \
     || [[ $Usage = umem ]] \
     || [[ $Usage = uhadoop ]];then
        perf_rpms="gperftools-devel leveldb*"
        for perf_rpm in $perf_rpms
        do
            if [[ $(rpm -qa|grep -c $perf_rpm) -eq 0 ]];then
                _check="FAIL"
                PRINT_ERR "[ERROR] $perf_rpm Not Installed."
            fi
        done
    fi

    if [[ $OS == "CentOS6" && $(uname -r) =~ "2.6.32" && $Usage = uhost ]];then
        # microcode_ctl check
        rpm -q microcode_ctl-1.17-25.3.el6.ucloud >/dev/null 2>&1
        if [[ $? -ne 0 ]];then
            _check="FAIL"
            PRINT_ERR "[ERROR] microcode_ctl-1.17-25.3.el6.ucloud Not Installed."
        fi
    fi
 
    # scl, devtoolset for hardware update
    if [[ $OS == "CentOS6" ]];then
 	    local scl_rpms='scl-utils devtoolset-3-runtime devtoolset-3-binutils devtoolset-3-gcc devtoolset-3-libstdc++-devel devtoolset-3-gcc-c++'
    	for scl_rpm in $scl_rpms
    	do
        	if ! rpm -q $scl_rpm &> /dev/null ;then
                _check="FAIL"
                PRINT_ERR "[ERROR] $scl_rpm Not Installed."
        	fi
    	done
    fi

    if [[ $OS == "CentOS7" ]];then
    	if lspci -d 15b3: | grep -qi "mellanox";then
        	if ! rpm -q mft &> /dev/null ;then
                _check="FAIL"
                PRINT_ERR "[ERROR] mft Not Installed."
        	fi
    	fi
    fi

    # 为 3008 卡安装额外硬盘检测工具
    if lspci -d 1000:0097:0107 |grep -q 3008 && [[ ! -d /opt/ismd ]] ;then
        _check="FAIL"
        PRINT_ERR "[ERROR] ismd Not Installed."
    fi
}

