#!/system/bin/sh
#
# Author: zach.xtr Jan 2011 (modified by MongooseHelix Dec 2011)
# Moves the contents of /data/dalvik-cache to the less used /cache/dalvik-cache and then bind mounts back

DCFILES=$(ls /data/dalvik-cache | wc -l)

log -p i -t cache2cache "running cache2cache to move /data/dalvik-cache to /cache/dalvik-cache";

# remount /system as rw
$BB mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system

# check if /cache/dalvik-cache target directory exists
if [ ! -d /cache/dalvik-cache ]; then
    log -p i -t cache2cache "/cache/dalvik-cache directory not found, creating it and setting permissions...";
    busybox mkdir /cache/dalvik-cache;
    busybox chown 1000:1000 /cache/dalvik-cache;
    busybox chmod 771 /cache/dalvik-cache
fi

# check if orignal /data/dalvik-cache directory exists
if [ ! -d /data/dalvik-cache ]; then
    log -p i -t cache2cache "/data/dalvik-cache directory not found, creating it and setting permissions...";
    busybox mkdir /data/dalvik-cache;
    busybox chown 1000:1000 /data/dalvik-cache;
    busybox chmod 771 /data/dalvik-cache
fi

# check if there are any dalvik-cache files to move from /data/dalvik-cache
if [ $DCFILES -gt 0 ]; then
    log -p i -t cache2cache "moving files from /data/dalvik-cache to /cache/dalvik-cache";
    busybox cp -fp /data/dalvik-cache/* /cache/dalvik-cache;
    busybox rm -r /data/dalvik-cache/*;
fi

# bind mount the cache dalvik-cache directory to the expected data directory
log -p i -t cache2cache "bind mounting /cache/dalvik-cache to /data/dalvik-cache";
busybox mount -o bind /cache/dalvik-cache /data/dalvik-cache;

# remount /system as ro
busybox mount -o remount,ro -t yaffs2 /dev/block/mtdblock3 /system

