#!/bin/bash

FUNNAME=$(basename $BASH_SOURCE)

# default var value
IP=""                                              # -I, HDM IP
USER=""                                            # -U, user name
PWD=""                                             # -P
DEALMODE="UNKNOW"                                  # dealmode
label=""                                           # cmd label
frulabel=""                                        # fru label
labvalue=""                                        # label value
curlabvalue=""                                     # current label value
ipmicmd=""                                         # ipmicmd
cmd=""                                             # cmd
cmd_fru=""
cmd_hex=""
cmd_asc=""

# Usage
function Usage()
{
    echo -e "Usage : ./$1 [MEDIUM] [FEILD] [VALUE]"
    echo -e " MEDIUM :"
    echo -e "\t <-K>: KCS,Option to use KCS"
    echo -e "\t <-N -I -U -P>: Option to use Network"
    echo -e "\t      <-I>: Option to enter IP, when using Network"
    echo -e "\t      <-U>: Option to enter Username, when using Network"
    echo -e "\t      <-P>: Option to enter Password, when using Network"
    echo -e " FEILD :"
    echo -e "\t <CT> : Modify Chassis Type"
    echo -e "\t <CPN>: Modify Chassis Part Number"
    echo -e "\t <CS> : Modify Chassis Serial"
    echo -e "\t <CE> : Modify Chassis Extra"
    echo -e "\t <BM> : Modify Board Mfg"
    echo -e "\t <BP> : Modify Board Product"
    echo -e "\t <BS> : Modify Board Serial"
    echo -e "\t <BN> : Modify Board Part Number"
    echo -e "\t <PM> : Modify Product Manufacturer"
    echo -e "\t <PN> : Modify Product Name"
    echo -e "\t <PPN>: Modify Product Part Number"
    echo -e "\t <PV> : Modify Product Version"
    echo -e "\t <PS> : Modify Product Serial"
    echo -e "\t <PAT>: Modify Product Asset Tag"
    echo -e " VALUE :"
    echo -e "\t The value of the modify"
}



# IN arg
while getopts "KNI:U:P:" arg
do
    case $arg in
        K)
            DEALMODE="KCS"
            ;;
        N)
            DEALMODE="NETWORK"
            ;;
        I)
            IP="$OPTARG"
            ;;
        U)
            USER="$OPTARG"
            ;;
        P)
            PWD="$OPTARG"
            ;;
        ?)
            echo "Unknow argment"
            Usage $FUNNAME
            exit 1
            ;;
    esac
done

#Judging KCS or Network
if [ "KCS" = "$DEALMODE" ]; then
    ipmicmd="ipmitool"
elif [ "NETWORK" = "$DEALMODE" ] && [ "" != "$IP" ] && [ "" != "$USER" ] && [ "" != "$PWD" ]; then
	ipmicmd="ipmitool -I lanplus -H $IP -U $USER -P $PWD"
else
    Usage $FUNNAME
    exit 1
fi

cmd_fru="$ipmicmd raw 0x36 0x02 0x20 0x14 0x00 0x80 0x54 0x01 0x02"

for arg in "$@"
do
    i=$i+1
    temp=$arg
    if (($#-1 == $i)); then
        if [ "CT" = $temp ]; then
            label="Chassis Type"
            frulabel="Chassis Type"
        elif [ "CPN" =  $temp ]; then
            label="Chassis Part Number"
            frulabel="Chassis Part Number"
        elif [ "CS" =  $temp ]; then
            label="Chassis Serial Number"
            frulabel="Chassis Serial"
        elif [ "CE" =  $temp ]; then
            label="Chassis Customer Serial Number"
            frulabel="Chassis Extra"
        elif [ "BM" =  $temp ]; then
            label="Board Manufacturer"
            frulabel="Board Mfg"
        elif [ "BP" =  $temp ]; then
            label="Board Product Name"
            frulabel="Board Product"
        elif [ "BS" =  $temp ]; then
            label="Board Serial Number"
            frulabel="Board Serial"
        elif [ "BN" =  $temp ]; then
            label="Board Part Number"
            frulabel="Board Part Number"
        elif [ "PM" =  $temp ]; then
            label="Product Manufacturer"
            frulabel="Product Manufacturer"
        elif [ "PN" =  $temp ]; then
            label="Product Product Name"
            frulabel="Product Name"
        elif [ "PPN" =  $temp ]; then
            label="Product Part Number"
            frulabel="Product Part Number"
        elif [ "PV" =  $temp ]; then
            label="Product Version"
            frulabel="Product Version"
        elif [ "PS" =  $temp ]; then
            label="Product Serial Number"
            frulabel="Product Serial"
        elif [ "PAT" =  $temp ]; then
            label="Product Asset Tag"
            frulabel="Product Asset Tag"
        else
            Usage $FUNNAME
            exit 1
        fi
        cmd_asc="$label:"
    fi

    if (($# == $i)); then
        labvalue=$temp
        cmd_asc+=$labvalue
    fi
done

#from ASCII to HEX
temp=$cmd_asc
for ((i=0;$i<${#temp};i++));
do 
    cmd_hex+=`printf "0x%x " "'${temp:$i:1}"`
done

cmd="$cmd_fru $cmd_hex 0x00"
#echo $cmd
$cmd > /dev/null 2>&1

#check execution result
curlabvalue=`$ipmicmd fru list 0 |grep "$frulabel" | awk -F ': ' '{print $2}' |grep "$labvalue"`
echo "-----------------------------------------------------------"
if [ "$labvalue" == "$curlabvalue" ]; then
    echo "Set $IP \"$frulabel\" to \"$labvalue\" successed!" 
else
    echo "Set $IP \"$frulabel\" failed!"
fi
echo "-----------------------------------------------------------"




