#!/system/bin/sh
############################################
## CronMod Unofficial Data2ext.           ##
## Written by Styrke @xda                 ##
############################################

## Increasing sdcard cache.
mmc='ls -d /sys/block/mmc*';
if [ -e $mmc/queue/read_ahead_kb ];
	then
		echo "2048" >  $mmc/queue/read_ahead_kb;
fi; 

## Make /sd-ext and /internal directories if needed and unmount /sd-ext if it already mounted
if [ ! -e /sd-ext ]
  then
busybox mount -o remount,rw /;
busybox mkdir /sd-ext;
busybox mount -o remount,ro /;
else
busybox umount /sd-ext;
fi;
if [ ! -e /internal ]
then
busybox mount -o remount,rw /;
busybox mkdir /internal;
busybox mount -o remount,ro /;
fi;

## Binds /data to /internal
busybox mount -o bind /data /internal;

## Mounts mmcblk0p2 to /sd-ext
busybox mount -o noatime,nodiratime,nosuid,nodev /dev/block/mmcblk0p2 /sd-ext;
busybox chown 1000:1000 /sd-ext;
busybox chmod 771 /sd-ext;

## Moves directories from internal to /sd-ext and recreate them

for i in app app-private data dalvik-cache system local
do
if [ ! -d /sd-ext/$i ] && [ -d /data/$i ]
then
busybox mv /data/$i /sd-ext/$i ;
fi;
## Removes folders existing in internal or the old symlinks
busybox rm -R /data/$i;

## Creates symlinks
busybox ln -s /sd-ext/$i /data/;
done;