#!/bin/bash

# Could not open device at /dev/ipmi0 or /dev/ipmi/0 or /dev/ipmidev/0: No such file or directory
# 如果没有这几个模块会导致ipmitool user list 1命令产生上面的报错
if [[ ! `ls /dev/ipmi* > /dev/null 2>&1` ]];then
    modprobe ipmi_devintf > /dev/null 2>&1
    modprobe ipmi_si > /dev/null 2>&1
fi

_install() {
    local root_id isChannelOne json1 json2 json3 IPMI_pw json4 req

    # BMC FW 升级有可能导致Lan channel变化
    if [[ "${ManufactureType}" =~ "R4900 G2" ]];then
        isChannelOne=`ipmitool raw 0x36 0x05 0xa2 0x63 0x00 0x0c | awk '{print $4}'`
        if [ "${isChannelOne}" == "00" ]; then
            ipmitool raw 0x36 0x09 0xa2 0x63 0x00 0x26 0x01
        fi
    elif [[ "${ManufactureType}" =~ "R4900 G3" ]];then
        isChannelOne=`ipmitool raw 0x36 0x05 0xA2 0x63 0x00 0x0C | awk '{print $4}'`
        if [ "${isChannelOne}" == "00" ]; then
            ipmitool raw 0x36 0x09 0xa2 0x63 0x00 0x2d 0x01
        fi
    fi

    # ipmi user and password
    json1=$(curl -s -H "Content-Type:application/json" -X POST --data  '{"data":["'"$IPMI"'"],"type":"IPMI"}' http://udcm.ucloudadmin.com/api/ipmi/v1/getpwd/)
    IPMI_user=$(echo "$json1" | jq -r .[].IPMIUser)
    IPMI_pw=$(echo "$json1" | jq -r .[].IPMIPassword)
    if [[ -z "$IPMI_pw" ]];then
        IPMI_pw="Ucloud.cn1"
    fi
    if [[ -z "$IPMI_user" ]];then
        IPMI_user="root"
    fi

    root_id=$(ipmitool user list 1 | grep "$IPMI_user" | awk '{print $1}')
    if [ -z $root_id ];then
        root_id=$(ipmitool user list 2 | grep "$IPMI_user" | awk '{print $1}')
    fi
    if [ -z $root_id ];then
        PRINT_ERR "[ERROR] can not found ipmi user 'root'"
        return 1
    fi

    # get the lan channel
    local UCLOUD_IPMI_CHANNEL=''
    for channel in 1 2 0
    do
        if [[ $(ipmitool -I open channel info $channel |grep -i "Channel Medium Type" |grep -c 802.3) -ne 0 ]]; then
            UCLOUD_IPMI_CHANNEL=$channel
            break
        fi
    done

    [[ "${ManufactureType}" =~ "NF5270M3" ]] && UCLOUD_IPMI_CHANNEL=8

    if [[ $UCLOUD_IPMI_CHANNEL = '' ]]; then
        PRINT_ERR "[ERROR] IPMI Lan Channel is NULL."
        return 1
    fi

    local local_static result ipmi_gateway ipmi_netmask
    local_static=$(ipmitool lan print $UCLOUD_IPMI_CHANNEL|grep "IP Address Source[ ]*:"|awk -F ': ' '{print $NF}')
    if [[ $local_static = "DHCP Address" ]];then
        result=`get_gateway_and_mask $IPMI $UCLOUD_IPMI_RANGE`
        ipmi_gateway=`echo $result|cut -d',' -f1`
        ipmi_netmask=`echo $result|cut -d',' -f2`

        if [[ -z $ipmi_gateway ]] || [[ -z $ipmi_netmask ]];then
            PRINT_ERR "[ERROR] ipmi gateway/netmask get failed"
            return 1
        fi

        ipmitool -I open lan set $UCLOUD_IPMI_CHANNEL ipsrc static 2>/dev/null >&2
        ipmitool -I open lan set $UCLOUD_IPMI_CHANNEL ipaddr $IPMI 2>/dev/null >&2
        ipmitool -I open lan set $UCLOUD_IPMI_CHANNEL netmask $ipmi_netmask 2>/dev/null >&2
        ipmitool -I open lan set $UCLOUD_IPMI_CHANNEL defgw ipaddr $ipmi_gateway 2>/dev/null >&2
        sleep 90
    fi

    local local_ipmi
    local_ipmi=$(ipmitool lan print $UCLOUD_IPMI_CHANNEL|grep "IP Address[ ]*:"|awk '{print $NF}')
    if [[ $local_ipmi != $IPMI ]];then
        PRINT_ERR "[ERROR] Local IPMI Not Match With CMDB IPMI."
        return 1
    fi

    json2=$(curl -s -H "Content-Type:application/json" -X POST --data  '{"data":["'"$IPMI"'"],"type":"IPMI"}' http://udcm.ucloudadmin.com/api/ipmi/v1/fruinfo/)
    if [[ $(echo "$json2" | jq -r .\"$IPMI\".RetCode) -eq 0 ]];then
        _check="PASS"
    else
        IPMI_pw=Ucloud.cn1
        if ipmitool user test "$root_id" 16 "$IPMI_pw" ;then
            :
        elif ipmitool user set password "$root_id" "$IPMI_pw";then
            sleep 5
        else
            PRINT_ERR "[ERROR] Ipmitool Local Command Not Work."
            return 1
        fi
        req4=$(cat <<EOF
{"data":[{"ipmi":"$IPMI","sn":"$SN","ipmi_user":"$IPMI_user","ipmi_password":"$IPMI_pw"}]}
EOF
)
        json4=$(curl -s -H "Content-Type:application/json" http://udcm.ucloudadmin.com/api/ipmi/v1/updatepwd/ -d "$req4")
        if [[ -n $json4 && $(jq -r '.[].status' <<< "$json4") != "success" ]];then
            PRINT_ERR "[ERROR] 默认密码错误请重置数据库密码"
            return 1
        fi
    fi

    req3=$(cat <<EOF
{"data":["$IPMI"],"type":"IPMI"}
EOF
)
    json3=$(curl -s -H "Content-Type:application/json" -X POST --data "$req3" http://udcm.ucloudadmin.com/api/ipmi/v1/modifypwd/)
    if [[ -n $json3 && $(jq -r .Ret <<< "$json3") != "success" ]];then
        PRINT_ERR "[ERROR] Ipmitool Remote Command Not Work, Maybe Passwd Not Match or Unable to establish"
        return 1
    fi
}

_check() {
    _check="PASS"
    local json_fru
    json_fru=$(curl -s -H "Content-Type:application/json" -X POST --data  '{"data":["'"$IPMI"'"],"type":"IPMI"}' http://udcm.ucloudadmin.com/api/ipmi/v1/fruinfo/)
    if [[ -n $json_fru ]];then
        if [[ $(echo "$json_fru"|jq -r ".\"$IPMI\".RetCode") -eq 0 ]];then
            _check="PASS"
        else
            _check="FAIL"
            PRINT_ERR "[ERROR] Ipmitool Remote Command Not Work, Maybe Passwd Not Match."
        fi
    else
        _check="FAIL"
        PRINT_ERR "[ERROR] IPMI Api Not Work."
    fi
}
