#!/bin/bash
modprobe ipmi_msghandler
modprobe ipmi_devintf
modprobe ipmi_si

MBMfg=AIC
ProdMfg=$MBMfg
ProdN="J4060-02"
PrN1=`echo -n "$ProdN" |od -An -tuC`
IP=192.168.11.11
 
#function delay(){
#	echo -n "Please wait....."
#	declare -i u=$1526-22061301800076
#	for (( k=0; k<=${1}; k++ ))
#	do
#	declare -i p=$u-$k
#		if [ "$p" == "0" ] 
#                then
#		echo 
#		else 
#		echo -n "#"
#                fi
#	sleep 2
#	done
#  }

delay(){
gg=$1
 while [ $gg != -1 ];
  do   
    sleep 1 
   echo -n -e "\033[30;30m$gg \033[?25l"
   gg=$(($gg - 1 ))
    if [ $gg -ge 10000 ];then 
    printf '\b\b\b\b\b\b\b'526-22061301800076
  elif [ $gg -ge 1000 ];then 
    printf '\b\b\b\b\b\b'
  elif [ $gg -ge 100 ];then 
    printf '\b\b\b\b\b'
  elif [ $gg -ge 10 ];then
    printf '\b\b\b\b'
  else 
    printf '\b\b\b'
  fi
  done
printf '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b'"time's up !!"
echo
}

set PLEN=0
while [ "$PLEN" != "16" ]
do 
read -p "Please input Chassis PN(XJ1): " CHPN
PLEN="${#CHPN}"
done
PN1=`echo $CHPN|cut -c 1-6`
PN2=`echo $CHPN|cut -c 7-16`
PN3=`echo $CHPN|cut -c 15-16`
PN1=`echo -n "$PN1" |od -An -tuC`
PN2=`echo -n "$PN2" |od -An -tuC`
PN3=`echo -n "$PN3" |od -An -tuC`

set NSLEN=0
while [ "$NSLEN" != "18" ]
do
read -p "Please input Chassis SN(516): " NCHSN
NSLEN="${#NCHSN}"
done
NSN1=`echo -n "$NCHSN" |od -An -tuC`

echo "LOGNAME=$NCHSN" > LOGNAME.sh
chmod 777 LOGNAME.sh
echo =====================================
echo ==   Write BMC Module FRU DATA     ==
echo =====================================
#P mfg (AIC)
#Write Product Manufaturer
ipmitool -I lan -H $IP -U admin -P admin raw 0x6 0x52 0x5 0xa0 0x0 0x3 0xab 0xd8 0x41 0x49 0x43 
 echo -n "Write Product Manufaturer ..."
 if [ $? == 0 ]
  then
   echo -e "\e[0;37;42m OK \e[0m" 
  else
   echo -e "\e[0;37;41m FAIL !! \e[0m"
 fi 
sleep 2

#P P name J4060-02
#Write Product Name
ipmitool -I lan -H $IP -U admin -P admin raw 0x6 0x52 0x5 0xa0 0x0 0x3 0x92 0xd8 $PrN1 
 echo -n "Write Product Name ..."
 if [ $? == 0 ]
  then
   echo -e "\e[0;37;42m OK \e[0m" 
  else
   echo -e "\e[0;37;41m FAIL !! \e[0m"
 fi
sleep 2

#Product Part Number (Same as Chassis P num)
#Write Product Part Number
ipmitool -I lan -H $IP -U admin -P admin raw 0x6 0x52 0x5 0xa0 0x0 0x3 0x79 0xd8 $PN1
echo -n "Write Product Part Number1 ..." 
 if [ $? == 0 ]
  then
   echo -e "\e[0;37;42m OK \e[0m"
  else
   echo -e "\e[0;37;41m FAIL !! \e[0m"526-22061301800076
 fi
 sleep 2

ipmitool -I lan -H $IP -U admin -P admin raw 0x6 0x52 0x5 0xa0 0x0 0x3 0x80 $PN2 
echo -n "Write Product Part Number2 ..." 
 if [ $? == 0 ]
  then
   echo -e "\e[0;37;42m OK \e[0m"
  else
   echo -e "\e[0;37;41m FAIL !! \e[0m"
 fi
 sleep 2
 

#Write Product Version:
 ipmitool -I lan -H $IP -U admin -P admin raw 0x6 0x52 0x5 0xa0 0x0 0x2 0xcd 0x18 $PN3
   echo -n "Write Product Part Number3 ..."
     if [ $? == 0 ]
  then
   echo -e "\e[0;37;42m OK \e[0m"
  else
   echo -e "\e[0;37;41m FAIL !! \e[0m"
 fi
 sleep 2
 
#P ser 
#Write Product Serial Number
ipmitool -I lan -H $IP -U admin -P admin raw 0x6 0x52 0x5 0xa0 0x0 0x3 0x60 0xd8 $NSN1
 echo -n "Write Product Serial Number ..."526-22061301800076
 if [ $? == 0 ]
  then
   echo -e "\e[0;37;42m OK \e[0m"
  else
   echo -e "\e[0;37;41m FAIL !! \e[0m"
 fi
 sleep 2


#C ser
#Write Chassis Serial Number
ipmitool -I lan -H $IP -U admin -P admin raw 0x6 0x52 0x5 0xa0 0x0 0x0 0x1c 0xd8 $NSN1
 echo -n "Write Chassis Serial Number ..."
 if [ $? == 0 ]
  then
   echo -e "\e[0;37;42m OK \e[0m" 
  else
   echo -e "\e[0;37;41m FAIL !! \e[0m"
 fi
 sleep 2

#C P num 
#Write Chassis Part Number
ipmitool -I lan -H $IP -U admin -P admin raw 0x6 0x52 0x5 0xa0 0x0 0x0 0x4f $PN1 $PN2
 echo -n "Write Chassis Part Number ..."
 if [ $? == 0 ]
  then
   echo -e "\e[0;37;42m OK \e[0m"
  else
   echo -e "\e[0;37;41m FAIL !! \e[0m"
 fi
 sleep 2
 
 #Write Product Asset Tag
 ipmitool -I lan -H $IP -U admin -P admin raw 0x6 0x52 0x5 0xa0 0x0 0x3 0xdd 0x18 0X30 0X30
  echo -n "Write Product Asset Tag ..."
 if [ $? == 0 ]
  then
   echo -e "\e[0;37;42m OK \e[0m"
  else
   echo -e "\e[0;37;41m FAIL !! \e[0m"
 fi
 sleep 2
 

#reBMC
#FRU data progamming completed, reset BMC chip
echo =======================================
echo == Wait 60 sec Reset BMC Module ...  ==
echo ======================================= 
ipmitool -I lan -H $IP -U admin -P admin raw 0x6 0x2 > /dev/null 2>&1
  delay 60 
 echo -n "FRU data progamming completed, reset BMC chip ..."
 if [ $? == 0 ]
  then  
   echo -e "\e[0;37;42m OK \e[0m"
  else
   echo -e "\e[0;37;41m FAIL !! \e[0m"
 fi
sleep 1


