#!/system/bin/bash
#
# Darktremor A2SDBoot version 2.7
# By: Robert Travis Kirton
# Based on the Darktremor A2SD 2.5 code
# Date:  June 21, 2010
# Based on Apps2SD written by Cyanogen
# ZipAlign code based on the ZipAlign script by Wes Garner


/system/bin/busybox.a2sd echo "[*] Starting A2SD Boot";

export a2sdfs=`/system/bin/toolbox getprop cm.filesystem.ready`;
export a2sdfix=`/system/bin/busybox.a2sd grep -q -e cm.filesystem.ready /init.rc;/system/bin/busybox.a2sd echo $?;`;

if [ !$a2sdfs ];
  then
    if [ $a2sdfix == 1 ];
      then
        /system/bin/toolbox setprop cm.filesystem.ready -1;
        export a2sdfix=-1;
      else
        /system/bin/toolbox setprop cm.filesystem.ready 0;
        export a2sdfix=0;
    fi;
  else
    export a2sdfix=$a2sdfs;
fi;

export a2sdfs2=`/system/bin/toolbox getprop dc.filesystem.ready`;
export a2sdfix2=`/system/bin/busybox.a2sd grep -q -e dc.filesystem.ready /init.rc;/system/bin/busybox.a2sd echo $?;`;

if [ !$a2sdfs ];
  then
    if [ $a2sdfix == 1 ];
      then
        /system/bin/toolbox setprop dc.filesystem.ready -1;
        export a2sdfix2=-1;
      else
        /system/bin/toolbox setprop dc.filesystem.ready 0;
        export a2sdfix2=0;
    fi;
  else
    export a2sdfix2=$a2sdfs2;
fi;

if [ $a2sdfix == -1 ] && [ $a2sdfix2 == -1 ];
  then
    export a2sdfix=-1;
  else
    if [ $a2sdfix == 1 ] || [ $a2sdfix == 1 ];
      then
        export a2sdfix=1;
      else
        export a2sdfix=0;
     fi;
fi;

if [ $a2sdfix == -1 ] && [ $a2sdfix2 == -1 ];
  then
    export a2sdfix=-1;
  else
    if [ $a2sdfix == 1 ] || [ $a2sdfix == 1 ];
      then
        export a2sdfix=1;
      else
        export a2sdfix=0;
     fi;
fi;

/system/bin/busybox.a2sd echo "[ ] Searching for Mount Point...";
if [ -d /system/sd ];
  then
    /system/bin/busybox.a2sd echo "[!] Mount point found..no further action required.";
  else
    /system/bin/busybox.a2sd echo "[ ] Creating Mount point.";
    /system/bin/busybox.a2sd mkdir /system/sd;
    chmod 777 /system/sd;
    /system/bin/busybox.a2sd echo "[ ] Mount point created.";
fi;

/system/bin/busybox.a2sd echo "[ ] Checking for ext partition";
if [ -e /dev/block/mmcblk0p2 ];
  then
    export a2sdmount=`/system/bin/busybox.a2sd grep -q -e /dev/block/mmcblk0p2 /proc/mounts;/system/bin/busybox.a2sd echo $?;`;
    if [ $a2sdmount == 1 ];    
      then
        /system/bin/busybox.a2sd echo "[!] Ext partition found, but not mounted.";
        if [ $a2sdfs != -1 ];
          then
            /system/bin/busybox.a2sd echo "[ ] Checking Ext partition...";
            /system/bin/e2fsck -y /dev/block/mmcblk0p2;
            /system/bin/toolbox setprop cm.e2fsck.errors $?;
        fi;
        /system/bin/busybox.a2sd echo "[ ] Mounting ext partitions";
        /system/bin/busybox.a2sd mount -o noatime,nodiratime -t auto /dev/block/mmcblk0p2 /system/sd;
        if [ $? != 0 ];
          then
            /system/bin/busybox.a2sd echo "[X] Mounting ext partition failed.";
            /system/bin/busybox.a2sd echo "[X] Can't continue script...aborting.";
            if [ -h /data/dalvik-cache ];
              then
                /system/bin/busybox.a2sd echo "[!] Resetting Dalvik Cache back to Internal Storage.";
                /system/bin/busybox.a2sd rm -rf /data/dalvik-cache;
                /system/bin/busybox.a2sd mkdir /data/dalvik-cache;
                /system/bin/busybox.a2sd chmod 777 /data/dalvik-cache;
            fi;
            if [ -h /data/app ];
              then
                /system/bin/busybox.a2sd echo "[!] Resetting Applications back to Internal Storage.";
                /system/bin/busybox.a2sd rm -rf /data/app;
                /system/bin/busybox.a2sd mkdir /data/app;
                /system/bin/busybox.a2sd chmod 777 /data/app;
            fi;
            if [ -h /data/app-private ];
              then
                /system/bin/busybox.a2sd echo "[!] Resetting Private Applications back to Internal Storage.";
                /system/bin/busybox.a2sd rm -rf /data/app-private;
                /system/bin/busybox.a2sd mkdir /data/app-private;
                /system/bin/busybox.a2sd chmod 777 /data/app-private;
            fi;
            /system/bin/busybox.a2sd echo "[ ] Setting File System Ready property to 1.";
            /system/bin/toolbox setprop cm.filesystem.ready 1;
            /system/bin/toolbox setprop dc.filesystem.ready 1;
            /system/bin/busybox.a2sd echo "[ ] Remounting /system to read-only.";
            /system/bin/busybox.a2sd mount -o remount,ro /system;
            exit;
        fi;
	    /system/bin/busybox.a2sd echo "[*] Ext partition mounted.";
      else
        /system/bin/busybox.a2sd echo "[*] Ext partition found and already mounted.";
    fi;
  else
    /system/bin/busybox.a2sd echo "[X] No ext partition or partition not at";
    /system/bin/busybox.a2sd echo "    /dev/block/mmcblk0p2.";
    /system/bin/busybox.a2sd echo "";
    /system/bin/busybox.a2sd echo "    An ext2, ext3, or ext4 partition must";
    /system/bin/busybox.a2sd echo "    exists on /dev/block/mmcblk0p2.";
    /system/bin/busybox.a2sd echo "";
    /system/bin/busybox.a2sd echo "    Reboot phone into recovery and create";
    /system/bin/busybox.a2sd echo "    an ext2, ext3, or ext4 partition."; 
    if [ -h /data/dalvik-cache ];
      then
        /system/bin/busybox.a2sd echo "[!] Resetting Dalvik Cache back to Internal Storage.";
		/system/bin/busybox.a2sd rm -rf /data/dalvik-cache;
		/system/bin/busybox.a2sd mkdir /data/dalvik-cache;
		/system/bin/busybox.a2sd chmod 777 /data/dalvik-cache;
	fi;
	if [ -h /data/app ];
	  then
		/system/bin/busybox.a2sd echo "[!] Resetting Applications back to Internal Storage.";
		/system/bin/busybox.a2sd rm -rf /data/app;
		/system/bin/busybox.a2sd mkdir /data/app;
		/system/bin/busybox.a2sd chmod 777 /data/app;
	fi;
	if [ -h /data/app-private ];
	  then
		/system/bin/busybox.a2sd echo "[!] Resetting Private Applications back to Internal Storage.";
		/system/bin/busybox.a2sd rm -rf /data/app-private;
		/system/bin/busybox.a2sd mkdir /data/app-private;
		/system/bin/busybox.a2sd chmod 777 /data/app-private;
	fi;
    /system/bin/busybox.a2sd echo "[ ] Setting File System Ready property to 1.";
    /system/bin/toolbox setprop cm.filesystem.ready 1;
    /system/bin/toolbox setprop dc.filesystem.ready 1;	
    /system/bin/busybox.a2sd echo "[ ] Remounting /system to read-only.";
    /system/bin/busybox.a2sd mount -o remount,ro /system;
    exit;
fi;

if [ -e /system/sd/.nocache ];
  then
    /system/bin/busybox.a2sd echo "[!] No Cache flag detected.  Removing for compatibility.";
    rm -f /system/sd/.nocache;
fi;

cd /data/dalvik-cache;
export a2sddcloc=`/system/bin/busybox.a2sd pwd;`;
/system/bin/busybox.a2sd echo "[ ] Dalvik-Cache is located in $a2sddcloc";

if [ $a2sddcloc == "/cache/dalvik-cache" ];
  then
    if [ ! -e /system/sd/.dcpartition ];
      then
        /system/bin/busybox.a2sd echo "[!] Dalvik-cache is symlinked.";
        /system/bin/busybox.a2sd echo "    /cache/dalvik-cache detected.";
        /system/bin/busybox.a2sd echo "    Dalvik Cache on /cache flag not set.";
        /system/bin/busybox.a2sd echo "    Adding Dalvik Cache on /cache flag ";
        /system/bin/busybox.a2sd echo "    for compatibility.";
        /system/bin/busybox.a2sd echo "x" > /system/sd/.dcpartition;
        /system/bin/busybox.a2sd rm -f /system/sd/.dalvikcache;
        /system/bin/busybox.a2sd echo "[ ] Erasing contents of dalvik-cache.";
        /system/bin/busybox.a2sd rm -f /cache/dalvik-cache/*;
    fi;
fi;

if [ $a2sddcloc == "/system/sd/dalvik-cache" ];
  then
    if [ ! -e /system/sd/.dalvikcache ];
      then
        /system/bin/busybox.a2sd echo "[!] Dalvik-cache is symlinked.";
        /system/bin/busybox.a2sd echo "    /system/sd/dalvik-cache detected.";
        /system/bin/busybox.a2sd echo "    Dalvik Cache on SD Card flag not set.";
        /system/bin/busybox.a2sd echo "    Adding Dalvik Cache on SD Card flag";
        /system/bin/busybox.a2sd echo "    for compatibility.";
        /system/bin/busybox.a2sd echo "x" > /system/sd/.dalvikcache;
        /system/bin/busybox.a2sd rm -f /system/sd/.dcpartition;
        /system/bin/busybox.a2sd echo "[ ] Erasing contents of dalvik-cache.";
        /system/bin/busybox.a2sd rm -f /system/sd/dalvik-cache/*;
    fi;
fi;


if [ -e /system/sd/.noa2sd ];
  then
    /system/bin/busybox.a2sd echo "[X] No Start flag detected.";
    /system/bin/busybox.a2sd echo "[X] A2SD will not start.";
    /system/bin/toolbox setprop cm.filesystem.ready 1;
    /system/bin/toolbox setprop dc.filesystem.ready 1;
    /system/bin/busybox.a2sd -o remount,ro /system;
    exit;
fi;

#if [ -h /data/data ];
#  then
#    /system/bin/busybox.a2sd echo "[ ] Deleting old data symlinks.";
#    /system/bin/busybox.a2sd rm -rf /data/data;
#    /system/bin/busybox.a2sd echo "[ ] Creating /data/data directory.";
#    /system/bin/busybox.a2sd mkdir /data/data;
#    /system/bin/busybox.a2sd chmod 777 /data/data;
#    if [ -d /system/sd/data ];
#      then
#        /system/bin/busybox.a2sd echo "[ ] Copying data directory from SD Card.";
#        /system/bin/busybox.a2sd cp /system/sd/data/* /data/data;
#        /system/bin/busybox.a2sd echo "[ ] Remove data directory from SD Card.";
#        /system/bin/busybox.a2sd rm -rf /system/sd/data;
#    fi;
#fi;

if [ ! -h /data/dalvik-cache ];
  then
    if [ -e /system/sd/.dalvikcache ] || [ -e /system/sd/.dcpartition ];
      then
        /system/bin/busybox.a2sd echo "[X] Dalvik-cache is set to internal storage.";
        /system/bin/busybox.a2sd echo "    Dalvik Cache flag set.";
        /system/bin/busybox.a2sd echo "[!] Executing A2SD Repair program.";
        /system/bin/toolbox setprop cm.a2sd.runfix 1;
        /system/bin/bash /system/bin/fixa2sd;
    fi;
fi;

if [ ! -h /data/app ];
  then
	/system/bin/busybox.a2sd echo "[X] Applications are set for internal storage.";
	/system/bin/busybox.a2sd echo "[!] Executing A2SD Repair program.";
	/system/bin/toolbox setprop cm.a2sd.runfix 1;
	/system/bin/bash /system/bin/fixa2sd;
fi;
if [ ! -h /data/app-private ];
  then
	/system/bin/busybox.a2sd echo "[X] Private Applications are set for internal storage.";
	/system/bin/busybox.a2sd echo "[!] Executing A2SD Repair program.";
	/system/bin/toolbox setprop cm.a2sd.runfix 1;
	/system/bin/bash /system/bin/fixa2sd;
fi;

/system/bin/busybox.a2sd echo "[ ] Removing ODEX files from /system/sd/app";
/system/bin/busybox.a2sd rm -r /system/sd/app/*.odex;
/system/bin/busybox.a2sd echo "[ ] Removing ODEX fles from /system/sd/app-private";
/system/bin/busybox.a2sd rm -r /system/sd/app-private/*.odex;
/system/bin/busybox.a2sd echo "[*] A2SD installed and active.";

if [ -e /system/sd/.zipalign ];
  then
    /system/bin/busybox.a2sd echo "[ ] ZipAligning programs";
    for apk in /data/app/*.apk; 
      do
        /system/bin/zipalign -c 4 $apk;
        if [ $? == 1 ];
          then
            /system/bin/busybox.a2sd echo "[ ] ZipAligning $apk";
            /system/bin/zipalign -f 4 $apk /cache/$(basename $apk);
            if [ -e /cache/$(basename $apk) ];
              then
                /system/bin/busybox.a2sd cp -fp /cache/$(basename $apk) $apk;
                /system/bin/busybox.a2sd rm /cache/$(basename $apk);
                /system/bin/busybox.a2sd echo "[*] ZipAligning $apk complete.";
              else
                /system/bin/busybox.a2sd echo "[X] ZipAligning $apk failed.";
            fi;
        fi;
    done;
    for apk in /data/app-private/*.apk; 
      do
        /system/bin/zipalign -c 4 $apk;
        if [ $? == 1 ];
          then
            /system/bin/busybox.a2sd echo "[ ] ZipAligning $apk";
            /system/bin/zipalign -f 4 $apk /cache/$(basename $apk);
            if [ -e /cache/$(basename $apk) ];
              then
                /system/bin/busybox.a2sd cp -fp /cache/$(basename $apk) $apk;
                /system/bin/busybox.a2sd rm /cache/$(basename $apk);
                /system/bin/busybox.a2sd echo "[*] ZipAligning $apk complete.";
              else
                /system/bin/busybox.a2sd echo "[X] ZipAligning $apk failed.";
            fi;
        fi;
    done;
    /system/bin/busybox.a2sd echo "[*] ZipAlign complete.";
  else
    /system/bin/busybox.a2sd echo "[!] ZipAlign flag file not detected.";
    /system/bin/busybox.a2sd echo "    Will not perform ZipAlign";
fi;

/system/bin/busybox.a2sd echo "[ ] Setting File System Ready property to 1.";
/system/bin/toolbox setprop cm.filesystem.ready 1;
/system/bin/toolbox setprop dc.filesystem.ready 1;
/system/bin/busybox.a2sd echo "[ ] Remounting /system as read-only.";
/system/bin/busybox.a2sd mount -o remount,ro /system;

if [ ! -e /system/sd/.noswap ];
  then
    /system/bin/busybox.a2sd echo "[ ] Searching for Swap Partition";
    if [ -e /dev/block/mmcblk0p3 ];
      then
        /system/bin/busybox.a2sd echo "[ ] Swap Partition Found...Turning on swap.";
	/system/bin/busybox.a2sd mkswap /dev/block/mmcblk0p3;
        /system/bin/busybox.a2sd swapon /dev/block/mmcblk0p3;
       else
        /system/bin/busybox.a2sd echo "[X] Swap Partition not found.";
     fi;
fi;

