#!/system/bin/sh

# Darktremor A2SD Starter Routine version 2.7
# By: Travis Kirton
# Date: June 18, 2010
# Portions based on the Darktremor A2SD 2.0 Code
# All A2SD code originally based on Apps2SD written by Cyanogen

export a2sdver="2.7 Update 3r1"

/system/bin/busybox.a2sd echo "Starting Darktremor A2SD version $a2sdver";

/system/bin/busybox.a2sd echo "[ ] Mounting /system as read-write";
/system/bin/busybox.a2sd mount -o remount,rw /system;

if [ $# == 0 ];
  then
    export a2sdparm="boot";
  else
    export a2sdparm=$1;
fi;

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 [ !$a2sdfs2 ];
  then
    if [ $a2sdfix2 == 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 ];
  then
    /system/bin/bash /system/bin/starta2sd $a2sdparm;
  else
    if [ $a2sdparm == "boot" ];
      then
        /system/bin/busybox.a2sd echo "[*] Starting A2SD (Lite Version) in $a2sdcmd mode.";
        /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;
            /system/bin/busybox.a2sd 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
            /system/bin/busybox.a2sd echo "[ ] Ext partition found.";
            /system/bin/busybox.a2sd echo "[ ] Checking Ext partition...";
            /system/bin/e2fsck -y /dev/block/mmcblk0p2;
            /system/bin/toolbox setprop cm.e2fsck.errors $?;
            /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.";
                /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;
          else
            /system/bin/busybox.a2sd echo "[X] Ext partition not found.";
            /system/bin/busybox.a2sd echo "    Run recovery to create an ext partition.";
            /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 [ -h /data/data ]
          then
            /system/bin/busybox.a2sd echo "[ ] Deleting old symlinks.";
            /system/bin/busybox.a2sd rm /data/data;
        fi;
        if [ ! -d data/data ];
          then
            /system/bin/busybox.a2sd echo "[ ] Creating /data/data directory"
            /system/bin/busybox.a2sd mkdir /data/data
            /system/bin/busybox.a2sd chmod 777 /data/data
        fi;
        /system/bin/busybox.a2sd echo "[ ] Searching for Data directory on ext partition.";
        if [ -d /system/sd/data ];
          then
            /system/bin/busybox.a2sd echo "[ ] Transferring /system/sd/data to /data/data";
            /system/bin/busybox.a2sd cp -a /system/sd/data/* /data/data;
            /system/bin/busybox.a2sd echo "[ ] Removing /system/sd/data";
            /system/bin/busybox.a2sd rm -rf /system/sd/data;
        fi;
        /system/bin/busybox.a2sd echo "[ ] Searching for App directory on ext partition";
        if [ ! -d /system/sd/app ];
          then
            /system/bin/busybox.a2sd echo "[!] App directory not found, creating...";
            /system/bin/busybox.a2sd mkdir /system/sd/app;
            /system/bin/busybox.a2sd echo "[!] Changing permissions of /system/sd/app";
            /system/bin/busybox.a2sd chmod 777 /system/sd/app;
            /system/bin/busybox.a2sd echo "[!] Moving files from /data/app to /system/sd/app";
            /system/bin/busybox.a2sd cp -f /data/app/* /system/sd/app;
            /system/bin/busybox.a2sd echo "[!] Deleting /data/app";
            /system/bin/busybox.a2sd rm -rf /data/app;
            /system/bin/busybox.a2sd echo "[!] Creating Symbolic Link of /system/sd/app as /data/app";
            /system/bin/busybox.a2sd ln -s /system/sd/app /data/app;
            /system/bin/busybox.a2sd echo "[!] Changing permissions of /data/app";
            /system/bin/busybox.a2sd chmod 777 /data/app;
          else
            if [ ! -h /data/app ];
              then
                /system/bin/busybox.a2sd echo "[!] App directory exists, but symbolic link do not.";
                /system/bin/busybox.a2sd echo "[!] Copying any files in /data/app";
                /system/bin/busybox.a2sd cp -f /data/app/* /system/sd/app;
                /system/bin/busybox.a2sd echo "[!] Remove /data/app";
                /system/bin/busybox.a2sd rm -rf /data/app;
                /system/bin/busybox.a2sd echo "[!] Creating Symbolic link...";
                /system/bin/busybox.a2sd ln -s /system/sd/app /data/app;
                /system/bin/busybox.a2sd echo "[!] Changing permission...";
                /system/bin/busybox.a2sd chmod 777 /data/app;
            fi;
        fi;
        /system/bin/busybox.a2sd echo "[ ] Searching for App-private directory on ext partition";
        if [ ! -d /system/sd/app-private ];
          then
            /system/bin/busybox.a2sd echo "[!] App-private directory not found, creating...";
            /system/bin/busybox.a2sd mkdir /system/sd/app-private;
            /system/bin/busybox.a2sd echo "[!] Changing permissions of /system/sd/app-private";
            /system/bin/busybox.a2sd chmod 777 /system/sd/app-private;
            /system/bin/busybox.a2sd echo "[!] Moving files from /data/app-private to /system/sd/app-private";
            /system/bin/busybox.a2sd cp -f /data/app-private/* /system/sd/app-private;
            /system/bin/busybox.a2sd echo "[!] Deleting /data/app-private";
            /system/bin/busybox.a2sd rm -rf /data/app-private;
            /system/bin/busybox.a2sd echo "[!] Creating Symbolic Link of /system/sd/app-private as /data/app-private";
            /system/bin/busybox.a2sd ln -s /system/sd/app-private /data/app-private;
            /system/bin/busybox.a2sd echo "[!] Changing permissions of /data/app-private";
            /system/bin/busybox.a2sd chmod 777 /data/app-private;
          else
            if [ ! -h /data/app-private ];
              then
                /system/bin/busybox.a2sd echo "[!] App directory exists, but symbolic link do not.";
                /system/bin/busybox.a2sd echo "[!] Copying any files in /data/app-private";
                /system/bin/busybox.a2sd cp -f /data/app-private/* /system/sd/app-private;
                /system/bin/busybox.a2sd echo "[!] Remove /data/app-private";
                /system/bin/busybox.a2sd rm -rf /data/app-private;
                /system/bin/busybox.a2sd echo "[!] Creating Symbolic link...";
                /system/bin/busybox.a2sd ln -s /system/sd/app-private /data/app-private;
                /system/bin/busybox.a2sd echo "[!] Changing permission...";
                /system/bin/busybox.a2sd chmod 777 /data/app-private;
            fi;
        fi;
        /system/bin/busybox.a2sd echo "[ ] Removing ODEX files from /system/sd/app";
        /system/bin/busybox.a2sd rm -rf /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 active.";
        /system/bin/busybox.a2sd echo "[ ] Remounting /system to read-only.";            
        /system/bin/busybox.a2sd mount -o remount,ro /system;
        /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 "[ ] 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;
      else
        /system/bin/bash /system/bin/starta2sd $a2sdparm;
    fi;
fi;        

