#!/system/bin/sh
#
# mount ext[234] partition from sd card

BB="logwrapper busybox";

if [ -e /system/bin/log ];
then
    LOGI="log -p i -t mountsd --"
    LOGE="log -p e -t mountsd --"
else
    LOGI=echo
    LOGE=echo
fi

if [ "$SD_EXT_DIRECTORY" = "" ];
then
    SD_EXT_DIRECTORY=/sd-ext;
fi;
if [ ! -d $SD_EXT_DIRECTORY -a -d /system/sd ];
then
    SD_EXT_DIRECTORY=/system/sd;
fi;

# find first linux partition on SD card
MMC=/dev/block/mmcblk0

# wait for the device to settle
COUNT=7;
until [ -b "$MMC" ] || [ $COUNT -lt 1 ];
do
    $LOGI "$MMC not ready, waiting $COUNT seconds..."
    sleep 1;
    COUNT=$((COUNT-1));
done;

if [ ! -b "$MMC" ];
then
    $LOGE "SD not found ($MMC)"
else
    if [ ! -d $SD_EXT_DIRECTORY ];
    then
        $LOGI "$SD_EXT_DIRECTORY not found, trying to create it...";
        $BB mkdir -p $SD_EXT_DIRECTORY
    fi;
    if [ ! -d $SD_EXT_DIRECTORY ];
    then
        $LOGE "failed to create directory for ext partition: $SD_EXT_DIRECTORY ";
    fi
fi

if [ -d $SD_EXT_DIRECTORY -a -b "$MMC" ];
then
    FDISK="busybox fdisk"
    PARTITION=`$FDISK -l $MMC | awk '/^\// && $5 == 83 {print $1;exit;}'`

    if [ -b "$PARTITION" ];
    then
        $LOGI "Checking filesystems..";
   
        # fsck the sdcard filesystem first
        if [ -x `which e2fsck` ];
        then
            e2fsck -y $PARTITION;e2fsk_exitcode=$?
        else
            echo "executable e2fsck not found, assuming no filesystem errors"
            e2fsk_exitcode=0
        fi
        # set property with exit code in case an error occurs
        setprop cm.e2fsck.errors $e2fsk_exitcode;
        if [ "$e2fsk_exitcode" -lt 2 ];
        then
	    FSTYPE=`/system/xbin/parted /dev/block/mmcblk0 print|grep ext |awk {'print $6'}`
	    if [ "$FSTYPE" == "ext4" ];
	    then
	        insmod /system/lib/modules/jbd2.ko
	        insmod /system/lib/modules/ext4.ko
                $BB mount -o noatime,nodiratime -t ext4 $PARTITION $SD_EXT_DIRECTORY;
	        if [ "$?" = 0 ];
                then
                    $BB chown 1000:1000 $SD_EXT_DIRECTORY;
                    $BB chmod 771 $SD_EXT_DIRECTORY;
                    $LOGI "$SD_EXT_DIRECTORY successfully mounted as ext4";
                    else
                        $LOGE "Unable to mount filesystem for $SD_EXT_DIRECTORY!";
                    fi
	    elif [ "$FSTYPE" == "ext3" ];
	    then
       	        insmod /system/lib/modules/jbd.ko
	        insmod /system/lib/modules/ext3.ko
                $BB mount -o noatime,nodiratime -t ext3 $PARTITION $SD_EXT_DIRECTORY;     
	        if [ "$?" = 0 ];
                then
                    $BB chown 1000:1000 $SD_EXT_DIRECTORY;
                    $BB chmod 771 $SD_EXT_DIRECTORY;
                    $LOGI "$SD_EXT_DIRECTORY successfully mounted as ext3";
                    else
                        $LOGE "Unable to mount filesystem for $SD_EXT_DIRECTORY!";
                    fi
	    else
                $BB mount -o noatime,nodiratime -t auto $PARTITION $SD_EXT_DIRECTORY;
                if [ "$?" = 0 ];
                then
                    $BB chown 1000:1000 $SD_EXT_DIRECTORY;
                    $BB chmod 771 $SD_EXT_DIRECTORY;
                    $LOGI "$SD_EXT_DIRECTORY successfully mounted as ext2";
                else
                    $LOGE "Unable to mount filesystem for $SD_EXT_DIRECTORY!";
                fi
       	    fi
        else
            $LOGE "Unable to repair filesystem, disabling apps2sd";
        fi
    fi
fi
