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


#### 05mountsd

BB="logwrapper busybox";
LOG="/system/bin/log -p w sd-ext:mountsd: ";

$LOG "Commencing sd-ext mounting"
if [ "$SD_EXT_DIRECTORY" = "" ];
then
    SD_EXT_DIRECTORY=/sd-ext;
fi;

$LOG "ensure SD Card is present"
SD_PRESENT=0
COUNT=10
until [ $SD_PRESENT -gt 0 ] || [ $COUNT -lt 1 ];
do
    $LOG "Try SD..."
    for MMC_NUM in `seq 0 9`;
    do
        MMC_TYPE=`cat /sys/block/mmcblk$MMC_NUM/device/type`
        if [ "$MMC_TYPE" = "SD" ];
        then
          $LOG "SD found"
          SD_PRESENT=1
          break
        else
          $LOG "SD not present, waiting 1s"
          sleep 1
          sync;
          COUNT=$((COUNT-1));
        fi
    done
done

$LOG "find SD Card now or never"
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 exist
    SD_EXT_PART=/dev/block/mmcblk${MMC_NUM}p2

    $LOG "wait for the device to settle"
    if [ "$SD_EXT_PART" = "" ];
    then
        COUNT=6;
        until [ -b "$SD_EXT_PART" ] || [ $COUNT -lt 1 ];
        do
            $LOG "Waiting 1s"
            sleep 1;
            COUNT=$((COUNT-1));
        done;
    fi
fi

$LOG "SD card should be settled if present"

if [ -b "$SD_EXT_PART" ];
then
    log -p i -t mountsd "Checking filesystems..";
    
    # skip scan
    #$LOG "fsck the sdcard filesystem first"
    #if [ -x `which e2fsck` ];
    #then
    #    e2fsck -y $SD_EXT_PART
    #    e2fsk_exitcode=$?
    #    $LOG "Check done"
    #else
        echo "executable e2fsck not found, assuming no filesystem errors"
        e2fsk_exitcode=0
        $LOG "Check skipped"
    #fi

    # set property with exit code in case an error occurs
    setprop cm.e2fsck.errors $e2fsk_exitcode;
    if [ "$e2fsk_exitcode" -lt 2 ];
    then
        
        if [ -e "$SD_EXT_DIRECTORY" ];
				then
					 $LOG "/sd-ext already present"
				else
					 $BB mount -o remount,rw /;
					 mkdir /sd-ext
					 $LOG "/sd-ext first created"
					 sync;
					 $BB mount -o remount,r /;
				fi;
        

        $LOG "mount and set perms"
        $BB mount -o async,noatime,nodiratime,delalloc,noauto_da_alloc,barrier=0,nobh -t ext4 $SD_EXT_PART $SD_EXT_DIRECTORY;
        if [ "$?" = 0 ];
        then
        
            $LOG "transfer apks to sd-ext"
						busybox mv -f /data/app /data/apptrans/;
						busybox mkdir /data/app;
						busybox chown 1000:1000 /data/app;
						busybox chmod 771 /data/app;
						sync;
        
            $BB chown 1000:1000 $SD_EXT_DIRECTORY;
            $BB chmod 771 $SD_EXT_DIRECTORY;
            sync;
            $LOG "Mounted, migrating apks"
            busybox mkdir /sd-ext/app;
            busybox cp -f /data/apptrans/*.* /sd-ext/app;
            busybox rm -fR /data/apptrans;
            busybox chown 1000:1000 /sd-ext/app;
            busybox chmod 771 /sd-ext/app;
            sync;
            log -p i -t mountsd "$SD_EXT_DIRECTORY successfully mounted";
        else
            $LOG "Mounting failed"
            log -p e -t mountsd "Unable to mount filesystem for $SD_EXT_DIRECTORY!";
        fi
    else
        log -p e -t mountsd "Unable to repair filesystem, disabling apps2sd";
        $LOG "Check failed! Abort!"
    fi
fi

#### 06mountdl

CACHESIZE=$(df -k /cache | tail -n1 | tr -s ' ' | cut -d ' ' -f2)
if [ $CACHESIZE -lt 20000 ]
then
  mount -o bind /data/local/download /cache/download
fi

rm /cache/download/downloadfile*.apk >/dev/null 2>&1


LOG="/system/bin/log -p w sd-ext:apps2sd: ";
$LOG "Commencing sd-ext integration"

if [ "$SD_EXT_DIRECTORY" = "" ];
then
    SD_EXT_DIRECTORY=/sd-ext;
fi;

if ! awk -vDIR="$SD_EXT_DIRECTORY" '$2 == DIR { exit 1; }' /proc/mounts ;
then
    $LOG "create directories if necessary."
    #dalvik-cache;
    for i in app app-private;
    do
        if [ ! -d $SD_EXT_DIRECTORY/$i ];
        then

            $LOG "creating directories."
            mkdir $SD_EXT_DIRECTORY/$i
            busybox chown 1000:1000 $SD_EXT_DIRECTORY/$i
            busybox chmod 771 $SD_EXT_DIRECTORY/$i
            log -p i -t a2sd "$SD_EXT_DIRECTORY/$i created"
        fi;
    done
		
    $LOG "Mounting into system"
    # mount and sync app app-private dalvik-cache to sd-ext
    mount -o bind /sd-ext/app /data/app
    $LOG "/data/app bound!"
    mount -o bind /sd-ext/app-private /data/app-private
    $LOG "/data/app-private bound!"
    #mount -o bind /sd-ext/dalvik-cache /data/dalvik-cache
    #$LOG "/data/dalvik-cache bound!"
    setprop cm.a2sd.active 1
    $LOG "Apps2SD successfully activated"
    
    # upon first successful mount, fix permissions
    #if [ -e /data/local/sdext-permfixed ];
		#then
		#	$LOG "this is not a first run"
		#else
		#	echo "fixed" >> /data/local/sdext-permfixed
		#	fix_permissions
		#fi; 
    
fi;




# 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
        # 3rd partition of sdcard should be the linux-swap if exist
        LINUX_SWAP_PART=/dev/block/mmcblk${MMC_NUM}p3

        # wait for the device to settle
        if [ "$LINUX_SWAP_PART" = "" ];
        then
            COUNT=3;
            until [ -b "$LINUX_SWAP_PART" ] || [ $COUNT -lt 1 ];
            do
                sleep 2;
                COUNT=$((COUNT-1));
                break
            done;
        fi
    fi
done

if [ -b "$LINUX_SWAP_PART" ];
then
        $BB freeramdisk $LINUX_SWAP_PART;
        if [ "$?" = 0 ];
        then
            $BB swapon $LINUX_SWAP_PART;
            if [ "$?" = 0 ];
            then
                log -p i -t swap2sd "Swap partition activated";
            else
                log -p e -t swap2sd "Unable to activate swap, exiting";
                exit 1;
            fi;
        else
            log -p e -t swap2sd "Unable to freeramdisk, exiting";
            exit 1;
        fi;
fi;

