#!/system/bin/sh
#
# mount ext partition from sd card
# Revised by Andrew Sutherland for The Evervolv Project (20120223)

if [ "$SD_EXT_DIRECTORY" = "" ];
then
    SD_EXT_DIRECTORY=/sd-ext;
fi;
BB="/system/xbin/busybox";
FDISK="$BB fdisk -l";
E2FSCK="/system/bin/e2fsck";
logI="log -p i -t mountext"
logE="log -p e -t mountext"

# find SD Card
for MMC_NUM in `seq 0 9`;
do
    MMC_TYPE=`cat /sys/block/mmcblk${MMC_NUM}/device/type`
    if [ "$MMC_TYPE" = "SD" ];
    then
        # 2nd partition of sdcard should be the sd-ext if it exists
        SD_EXT_PART=/dev/block/mmcblk${MMC_NUM}p2
        # Find the linux and swap partitions (if they exist)
        #SD_EXT_PART=`$FDISK /dev/block/mmcblk${MMC_NUM} | awk '/^\// && $5 == 83 {print $1;exit;}'`;
        SWAP_PART=`$FDISK /dev/block/mmcblk${MMC_NUM} | awk '/^\// && $5 == 82 {print $1;exit;}'`;
        setprop ev.sdextpart $SD_EXT_PART
        break;
    fi;
done
# mount sdcard if a valid partition was found
if [ -b "$SD_EXT_PART" ];
then
    $logI "Checking $SD_EXT_PART for errors...";
    # fsck the sd-ext filesystem first
    if [ -x "$E2FSCK" ];
    then
        # using p instead of y to prevent 10min+ boot times when the
        # filesystem has many unrecoverable errors
        $E2FSCK -p $SD_EXT_PART;
        e2fsk_exitcode=$?;
    else
        $logE "executable e2fsck not found, assuming no filesystem errors";
        e2fsk_exitcode=0;
    fi;
    # set property with exit code in case an error occurs
    setprop ev.e2fsck.errors $e2fsk_exitcode;
    if [ $e2fsk_exitcode -le 2 ];
    then
        # mount and set perms
        # it is ok to mount ext[2/3/4] partitions as ext4
        $BB mount -t ext4 -o noauto_da_alloc,data=ordered,commit=15,barrier=1,nouser_xattr,errors=continue,noatime,nodiratime,nosuid,nodev $SD_EXT_PART $SD_EXT_DIRECTORY;
        if [ $? -eq 0 ];
        then
            $BB chown 1000:1000 $SD_EXT_DIRECTORY;
            $BB chmod 771 $SD_EXT_DIRECTORY;
            $logI "$SD_EXT_DIRECTORY successfully mounted";
        else
            $logE "Unable to mount filesystem for $SD_EXT_DIRECTORY!";
        fi;
    else
        $logE "e2fsck returned error $e2fsk_exitcode";
        $logE "Unable to repair ext partition...not mounting";
    fi;
else
    $logE "linux partition not found on sdcard";
fi;
# swap: do zram first so it gets first priority since it should be faster
# handle zram
zramprop=`getprop persist.sys.zram.enable`
zramblkdev="zram0"
# 10% is smallest reasonable size
if [[ $zramprop -ge 10 ]]; then
    $logI "zRam percentage = $zramprop%"
    if [ -b /dev/block/${zramblkdev} ]; then
        memtotal=`$BB awk '{ if ($1 eq "MemTotal:") print $2 ;exit }' </proc/meminfo`
        zramsize=$((($(($memtotal * $zramprop)) / 100) * 1024))
        $logI "Initializing $zramsize bytes of zRam"
        echo $zramsize > /sys/block/${zramblkdev}/disksize
        mkswap /dev/block/${zramblkdev} >/dev/null
        $BB swapon /dev/block/${zramblkdev}
    else
        $logE "no zRam device found"
    fi;
fi;
# enable swap
if [ -n "$SWAP_PART" ]; then
    if [ -b $SWAP_PART ]; then
        $BB swapon $SWAP_PART;
        $logI "Swap enabled on $SWAP_PART";
    else
        $logE "Swap block device doesnt exist.";
    fi;
fi;
exit;
