#!/bin/sh
USB_MOUNT_PATH=/media
if [ ! -x $USB_MOUNT_PATH ];then
mkdir -p $USB_MOUNT_PATH
fi

export LD_LIBRARY_PATH=/thunder/lib
export PATH=$PATH:/thunder/bin
#ubus call mnt blink
lsmod | grep ufsd

if [ "$?" != "0" ];then
    insmod /thunder/lib/jnl.ko
    insmod /thunder/lib/ufsd.ko
    sleep 1
fi

for i in  `ls /dev/sd*`
do
    mount | grep $i
    if [ "$?" = "0" ];then
		echo "$i has mounted" >>/tmp/mdev.log
        ubus call mnt noblink
    else
    	DEV_NAME=`echo $i | awk -F \/ '{ print $3 }'`
        
        SDEV=`echo $i | awk '{print gensub("[0-9]+$","",$0)}'`
        TYPE=`fdisk -l $SDEV -o device,type | grep $i | grep 'EFI'`
        if [ -n "$TYPE" ]; then
            echo "$i is a EFI partition ignored" >>/tmp/mdev.log
            continue
        fi

		ls /tmp | grep "flag" | grep "$DEV_NAME"
		if [ "$?" = "0" ];then
			echo "detect $DEV_NAME is repairing" >>/tmp/usb_repair.log
			continue
		fi

        if [ ! -d "$USB_MOUNT_PATH/$DEV_NAME" ];then
            mkdir -m 777 -p $USB_MOUNT_PATH/$DEV_NAME && chattr +i $USB_MOUNT_PATH/$DEV_NAME
        fi
        
        mount -t vfat -o umask=000,iocharset=utf8 $i $USB_MOUNT_PATH/$DEV_NAME
        if [ "$?" != "0" ];then
			mount -t ufsd -o nls=utf8,nolazy,force,umask=000 $i $USB_MOUNT_PATH/$DEV_NAME
        else
			/thunder/scripts/usb_repair.sh "vfat" $i $USB_MOUNT_PATH/$DEV_NAME >>/tmp/usb_repair.log &
			continue
        fi
  
        if [ "$?" != "0" ];then
            mount -t ext3 $i $USB_MOUNT_PATH/$DEV_NAME
        else
			/thunder/scripts/usb_repair.sh "ufsd" $i $USB_MOUNT_PATH/$DEV_NAME >>/tmp/usb_repair.log &
            continue
        fi

        if [ "$?" != "0" ];then
            mount -t ext4 $i $USB_MOUNT_PATH/$DEV_NAME
        else
			/thunder/scripts/usb_repair.sh "ext3" $i $USB_MOUNT_PATH/$DEV_NAME >>/tmp/usb_repair.log &
            continue
        fi
        
        if [ "$?" != "0" ];then
            echo "can not mount dev [$i]" >> /tmp/mdev.log
            continue
        else
			/thunder/scripts/usb_repair.sh "ext4" $i $USB_MOUNT_PATH/$DEV_NAME >>/tmp/usb_repair.log &
            continue
        fi

    fi
done

#ubus call mnt noblink
