#!/system/bin/sh

if [ -f /g_file_storage.ko ] ; then
    if [ -f /busybox/sbin/losetup ] ; then
        echo "Find /busybox/sbin/losetup" > usbgadget.log
        mkdir /UserSpace
        mount -t yaffs2 /dev/block/mtdblock11 /UserSpace
        if [ $? -ne 0 ] ; then
            echo "Fail to mount /dev/block/mtdblock11 to /UserSpace" > usbgadget.log
            exit 0
        fi
        if [ -f /UserSpace/UserFile ] ; then
            /busybox/sbin/losetup /dev/block/loop0 /UserSpace/UserFile
            if [ $? -eq 0 ] ; then
                echo "losetup successful" > usbgadget.log
                insmod g_file_storage.ko file=/dev/block/loop0,/dev/block/mmcblk0 removable=1 stall=0
                if [ $? -eq 0 ] ; then
                    echo "insmod g_file_storage.ko file=/dev/block/loop0,/dev/block/mmcblk0 successful" > usbgadget.log
                    mkdir /LocalDisk
                    /system/bin/mount -o dirsync,nosuid,nodev,noexec,utf8,uid=1000,gid=1015,fmask=702,dmask=702,shortname=mixed -t vfat /dev/block/loop0  /LocalDisk
                    if [ $? -eq 0 ] ; then
                        echo "mount -t vfat /dev/block/loop0 to /LocalDisk successful" > usbgadget.log
                    else
                        echo "fail to mount /dev/block/loop0 to /LocalDisk" > usbgadget.log
                    fi
                else
                    echo "Fail to insmod g_file_storage.ko file=/dev/block/loop0,/dev/block/mmcblk0" > usbgadget.log
                fi
            else
                echo "Fail to losetup /dev/block/loop0 /UserSpace/UserFile" > usbgadget.log
            fi
        else
            echo "Not find /UserSpace/UserFile" > usbgadget.log
            insmod g_file_storage.ko  file=/dev/block/mmcblk0 removable=1 stall=0
            if [ $? -eq 0 ] ; then                                                
                echo "insmod g_file_storage.ko file=/dev/block/mmcblk0 successful" > usbgadget.log        
            else                                                        
                echo "Fail to insmod g_file_storage.ko file=/dev/block/mmcblk0" > usbgadget.log       
            fi
            umount /UserSpace
            mkdir /LocalDisk
            mount -t yaffs2  /dev/block/mtdblock11 /LocalDisk
            if [ $? -eq 0 ] ; then
                echo "mount -t yaffs2 /dev/block/mtdblock11 to /LocalDisk successful" > usbgadget.log
            else
                echo "fail to mount /dev/block/mtdblock11 to /LocalDisk" > usbgadget.log
            fi
        fi
    else
        echo "Not find /busybox/sbin/losetup" > usbgadget.log
        insmod g_file_storage.ko  file=/dev/block/mmcblk0 removable=1 stall=0
        if [ $? -eq 0 ] ; then                                                
            echo "insmod g_file_storage.ko file=/dev/block/mmcblk0 successful" > usbgadget.log        
        else                                                        
            echo "Fail to insmod g_file_storage.ko file=/dev/block/mmcblk0" > usbgadget.log       
        fi
        mkdir /LocalDisk
        mount -t yaffs2  /dev/block/mtdblock11 /LocalDisk
        if [ $? -eq 0 ] ; then
            echo "mount -t yaffs2 /dev/block/mtdblock11 to /LocalDisk successful" > usbgadget.log
        else
            echo "fail to mount /dev/block/mtdblock11 to /LocalDisk" > usbgadget.log
        fi
    fi
else
    mkdir /LocalDisk
    mount -t yaffs2  /dev/block/mtdblock11 /LocalDisk
    if [ $? -eq 0 ] ; then
        echo "mount -t yaffs2 /dev/block/mtdblock11 to /LocalDisk successful" > usbgadget.log
    else
        echo "fail to mount /dev/block/mtdblock11 to /LocalDisk" > usbgadget.log
    fi
fi
