#!/bin/sh

file_emmc_device="/dev/mmcblk0"
file_emmc_partition="/dev/mmcblk0p1"
# file_check_result="/var/log/emmc_check.txt"
# file_log="/var/log/emmc_init.log"
# file_mount_point="/media/mmc1"
file_check_result="/tmp/emmc_check.txt"
file_log="/tmp/emmc_init.log"
file_mount_point="/media"

emmc_result_no_emmc="emmc_no_device"
emmc_result_success="emmc_success"
emmc_result_no_partition="emmc_no_partition"
emmc_result_mount_fail="emmc_mount_fail"

find_format=0
find_mount_point=0

mountCheck() {
	i=1
	find_format=0
	find_mount_point=0
	echo "$1 read $file_emmc_partition property start" >> $file_log
	ext4=`df -T $file_emmc_partition`
	for element in $ext4
	do
		echo "line$i:$element" >> $file_log
		if [ $element == "ext4" ] ; then
			echo "find format: ext4" >> $file_log
			find_format=1
		elif [ $element == "${file_mount_point}" ] ; then
			echo "find mount point: ${file_mount_point}" >> $file_log
			find_mount_point=1
		fi
		i=`expr $i + 1`
	done
	echo "$1 read $file_emmc_partition property end" >> $file_log
}

time=$(date "+%Y-%m-%d %H:%M:%S")
echo "emmc power on checkstart($time)" > $file_log

#检查是否有emmc设备，如果没有
if [ ! -e $file_emmc_device ] ; then
	echo "no emmc device detected"  >> $file_log
	echo "$emmc_result_no_emmc" > $file_check_result
else #如果有
	echo "found $file_emmc_device" >> $file_log

#检查有没有分区设备，如果没有则进行分区
	if [ ! -e $file_emmc_partition ] ; then
			echo "make emmc partition start" >> $file_log
			echo -e "n\np\n1\n\n\nw\n" | fdisk /dev/mmcblk0
			echo "make emmc partition end" >> $file_log
			sleep 1s
	fi

#再次尝试，如果还是找不到分区设备，则认为emmc有问题
	if [ ! -e $file_emmc_partition ] ; then
		echo "can not make partition:$file_emmc_device" >> $file_log
		echo "$emmc_result_no_partition" > $file_check_result
	else
		echo "found $file_emmc_partition" >> $file_log

	#检查是否已经mount，因为可能被系统自动调用fstab而mount上
		mountCheck kkk
		echo "kkk find mount_point:$find_mount_point, find_format:$find_format" >> $file_log
		
	#如果已经mount上,并且格式为ext4,则成功，否则认为有问题
		if [ $find_mount_point -eq 1 -a $find_format -eq 1 ] ; then
			echo "$file_emmc_partition has alreay mount" >> $file_log
			echo "$emmc_result_success" > $file_check_result
		else
		#先卸载挂载点
			echo "umount $file_mount_point" >> $file_log
			umount $file_mount_point
		#先尝试是否可以mount上, 如果已经格式化，只要执行挂载就可以，尽量不格式化emmc
			echo "find $file_emmc_partition, and mount to $file_mount_point" >> $file_log
			mount -t ext4 $file_emmc_partition $file_mount_point
		#检查是否可以挂载上
			mountCheck bbb
			echo "bbb find mount_point:$find_mount_point, find_format:$find_format" >> $file_log
		#如果挂载上，并且是ext4格式，则成功
			if [ $find_mount_point -eq 1 -a $find_format -eq 1 ] ; then
				echo "$file_emmc_partition mount success" >> $file_log
				echo "$emmc_result_success" > $file_check_result
			#如果挂载上，却不是ext4格式，则格式化为ext4
			elif [ $find_mount_point -eq 1 -a $find_format -eq 0 ] ; then
				#先卸载
				echo "$file_emmc_partition mounted, but it's not ext4 format" >> $file_log
				echo "umount $file_mount_point" >> $file_log
				umount $file_mount_point
				#格式化emmc为ext4格式
				echo "fomat $file_emmc_partition to ext4" >> $file_log
				echo "mkfs.ext4 $file_mount_point" >> $file_log
				mkfs.ext4 $file_emmc_partition >> $file_log 2>&1
			#如果挂载不上，也格式化为ext4
			else
				#格式化emmc为ext4格式
				echo "$file_emmc_partition can not mount" >> $file_log
				echo "fomat $file_emmc_partition to ext4" >> $file_log
				echo "mkfs.ext4 $file_mount_point" >> $file_log
				mkfs.ext4 $file_emmc_partition >> $file_log 2>&1
			fi
			
			#重新挂载
			if [ $find_mount_point -eq 0 -o $find_format -eq 0 ] ; then
				echo "try mount $file_emmc_partition after proper process" >> $file_log
				mount -t auto $file_emmc_partition $file_mount_point
				sleep 1s
				#检查是否可以挂载上
				mountCheck zzz
				echo "zzz find mount_point:$find_mount_point, find_format:$find_format" >> $file_log

				if [ $find_mount_point -eq 1 -a $find_format -eq 1 ] ; then
					echo "$file_emmc_partition mount success" >> $file_log
					echo "$emmc_result_success" > $file_check_result
				else
					echo "$file_emmc_partition mount failed" >> $file_log
					echo "$emmc_result_mount_fail" > $file_check_result
				fi
			fi
		fi
	fi
fi

#exit 0

