#!/bin/bash
which nvme &>/dev/null
if [ $? -ne 0 ];then
	yum install nvme-cli -y
fi
for DEV in `nvme list |egrep "\bP5920|\bP5926" |awk '{print $1}' |awk -F"/" '{print $3}' |sed 's/n1$//g'`;do
	LBA_Size=`smartctl -a /dev/$DEV |awk '/Namespace 1 Formatted LBA Size/{print $NF}' |sed 's/\s*//g'`
	FW_Ver=`smartctl -a /dev/$DEV |awk '/Firmware Version/{print $NF}' |sed 's/\s*//g'`
	if [[ "$LBA_Size" != "512" ]];then
		nvme format /dev/$DEV -l 0
		modprobe -r nvme
		sleep 5s
		modprobe nvme
		sleep 5s
	else 
		echo "$DEV LBA size is already 512B"
	fi
	if [[ "$FW_Ver" != "283001S0" ]];then
		nvme fw-download /dev/$DEV -f allBinary_1S0.tar
		nvme fw-commit /dev/$DEV -a 3 -s 0
		sleep 5
	else 
		echo "$DEV FW is already 283001S0"
        fi
	smartctl -a /dev/$DEV | grep -w 'Temperature Sensor 4' | grep "159"
        if [[ $? == 0 ]];then
        	unzip nvmemgr_for_ucloud_3sensor_0629.zip 
		cd nvmemgr_for_ucloud_3sensor/centos-7.0
                chmod +x *
		sh install.sh
		sleep 5s
		sh oobfw.sh
		sleep 5s 	
	else
		echo "OOB is ok"
	fi
		

done
