#!/system/bin/sh
#

# Enable Apps2SD?

A2SD_ENABLE=`getprop lk.apps2sd.enable`

if [ "$A2SD_ENABLE" != 1 ];
then
    echo "Apps2SD DISABLE!";
    exit;
fi;


A2SD_MOVE_DC=`getprop lk.movedc.enable`


if [ -e /dev/block/mmcblk0p2 ];
then
    echo "--- Checking filesystems";

    # fsck the sdcard filesystem first
    e2fsck -y /dev/block/mmcblk0p2;

    # set property with exit code in case an error occurs
    setprop lk.e2fsck.errors $?;

    # mount and set perms
    busybox mount -o noatime,nodiratime -t auto /dev/block/mmcblk0p2 /system/sd;
    if [ "$?" = 0 ];
    then
        busybox chown 1000:1000 /system/sd;
        busybox chmod 771 /system/sd;

        # clean up any old symlinks, create data directories
        for i in data;
        do
            if [ -h /data/$i ];
            then
                rm /data/$i;
            fi;
            if [ ! -d /data/$i ];
            then
                mkdir /data/$i;
                busybox chown 1000:1000 /data/$i;
                busybox chmod 771 /data/$i;
            fi;
        done;

        # don't allow /data/data on sd because of upgrade issues - move it if possible
        if [ -d /system/sd/data ];
        then
            busybox cp -a /system/sd/data/* /data/data/;
            busybox rm -rf /system/sd/data;
        fi;

        # move apps from internal memory to sdcard
        for i in app app-private;
        do
            if [ ! -d /system/sd/$i ];
            then
                mkdir /system/sd/$i;
            fi

            busybox chown 1000:1000 /system/sd/$i;
            busybox chmod 771 /system/sd/$i

            if [ -d /data/$i ] && [ ! -h /data/$i ];
            then
                busybox cp -a /data/$i/* /system/sd/$i/;
                busybox rm -f /data/$i/*;
            fi;
        done;

        # symlink app dirs - they must be on the same filesystem
        for i in app app-private;
        do
            if [ -d /data/$i ] && [ ! -h /data/$i ];
            then
                busybox rm -rf /data/$i;
                busybox ln -s /system/sd/$i /data/$i;
            fi;
        done;

        # clean up old whiteouts
        for i in local misc property system tombstones data;
        do
            if [ -h /system/sd/$i ]; then rm -f /system/sd/$i; fi
        done;


	# move dalvik-cache
	if [ "$A2SD_MOVE_DC" == 1 ];
	then
            if [ ! -h /data/dalvik-cache ];
            then
	        echo "delete dalvik-cache on sd card...";
                rm -r /system/sd/dalvik-cache;
            fi

	    echo "Apps2SD: Moving dalvik-cache";
            if [ ! -d /system/sd/dalvik-cache ];
            then
                mkdir /system/sd/dalvik-cache;
            fi
            busybox chown 1000:1000 /system/sd/dalvik-cache;
            busybox chmod 771 /system/sd/dalvik-cache;

	    if [ -d /data/dalvik-cache ];
	    then
 		echo "apps2sd: delete /data/dalvik-cache...";
	    	rm -r /data/dalvik-cache;
	    fi
            busybox ln -s /system/sd/dalvik-cache /data/dalvik-cache;

	    setprop lk.a2sd.move_dc_active 1;
	fi;

        # please don't put odex files in the app directory people!
        # it causes dexopt to crash when switching builds!
        busybox rm -f /system/sd/app/*.odex

        setprop lk.a2sd.active 1;

        echo "+++ Apps-to-SD successfully enabled";
    else
        echo "*** Unable to mount filesystem for a2sd!";
    fi
fi
