#!/system/bin/bash

# Darktremor Apps2SD/A2SD Setup Program version 2.7
# By: Travis Kirton
# Date: June 21, 2010
# Based on the Darktremor A2SD version 2.6R2 code

if [ $# == 0 ];
  then
    export a2sdsetup="none";
  else
    export a2sdsetup=$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 $?;`

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;

/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 "[ ] Remounting /system to read-write";
/system/bin/busybox.a2sd mount -o remount,rw /system;

/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 "[ ] 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 "[ ] Remounting /system as read-only.";
    /system/bin/busybox.a2sd mount -o remount,ro /system;
    exit;
fi;

case $a2sdsetup
  in
    "install")
              /system/bin/busybox.a2sd echo "[ ] Installing A2SD Links...";
              if [ ! -h /data/app ];
                then
                  if [ ! -d /system/sd/app ];
                    then
                      /system/bin/busybox.a2sd echo "[ ] Creating /system/sd/app";
                      /system/bin/busybox.a2sd mkdir /system/sd/app;
                  fi;
                  /system/bin/busybox.a2sd echo "[ ] Copying /data/app to /system/sd/app";
		  /system/bin/busybox.a2sd cp /data/app/* /system/sd/app;
		  /system/bin/busybox.a2sd echo "[ ] Removing /data/app";
		  /system/bin/busybox.a2sd rm -rf /data/app;
		  /system/bin/busybox.a2sd echo "[ ] Creating /data/app link to /system/sd/app";
		  /system/bin/busybox.a2sd ln -s /system/sd/app /data/app
	      fi;
	      if [ ! -h /data/app-private ];
	        then
                  if [ ! -d /system/sd/app-private ];
		    then
		      /system/bin/busybox.a2sd echo "[ ] Creating /system/sd/app-private";
                      /system/bin/busybox.a2sd mkdir /system/sd/app-private;
	          fi;
	          /system/bin/busybox.a2sd echo "[ ] Copying /data/app-private to /system/sd/app-private";
	          /system/bin/busybox.a2sd cp /data/app-private/* /system/sd/app-private;
	          /system/bin/busybox.a2sd echo "[ ] Removing /data/app-private";
	          /system/bin/busybox.a2sd rm -rf /data/app-private;
	          /system/bin/busybox.a2sd echo "[ ] Creating /data/app-private link to /system/sd/app-private";
	          /system/bin/busybox.a2sd ln -s /system/sd/app-private /data/app-private
              fi;
	      if [ -e /system/sd/.dalvikcache ];
	        then
	          if [ ! -h /data/dalvik-cache ];
		    then
		      if [ ! -d /system/sd/dalvik-cache ];
	                then
		          /system/bin/busybox.a2sd echo "[ ] Creating /system/sd/dalvik-cache";
                          /system/bin/busybox.a2sd mkdir /system/sd/dalvik-cache;
		       fi;
		       /system/bin/busybox.a2sd echo "[ ] Removing /data/dalvik-cache";
		       /system/bin/busybox.a2sd rm -rf /data/dalvik-cache;
		       /system/bin/busybox.a2sd echo "[ ] Creating /data/dalvik-cache link to /system/sd/dalvik-cache";
		       /system/bin/busybox.a2sd ln -s /system/sd/app-private /data/app-private
		  fi;
              fi;
              if [ -e /system/sd/.dcpartition ];
	        then
	          if [ ! -h /data/dalvik-cache ];
		    then
		      if [ ! -d /cache/dalvik-cache ];
	                then
		          /system/bin/busybox.a2sd echo "[ ] Creating /cache/dalvik-cache";
                          /system/bin/busybox.a2sd mkdir /cache/dalvik-cache;
		       fi;
		       /system/bin/busybox.a2sd echo "[ ] Removing /data/dalvik-cache";
		       /system/bin/busybox.a2sd rm -rf /data/dalvik-cache;
		       /system/bin/busybox.a2sd echo "[ ] Creating /data/dalvik-cache link to /cache/dalvik-cache";
		       /system/bin/busybox.a2sd ln -s /cache/app-private /data/app-private
		  fi;
              fi;
	      /system/bin/busybox.a2sd echo "[ ] Removing No A2SD flag file.";
	      /system/bin/busybox.a2sd rm /system/sd/.noa2sd;
              if [ $a2sdfix == 1 ];
                then
                  /system/bin/busybox.a2sd echo "[ ] Rebooting phone.";
                  reboot;
              fi;
              ;;    

    "remove")
	      /system/bin/busybox.a2sd echo "[ ] Removing A2SD Links...";
	      if [ -h /data/app ];
	        then
		  /system/bin/busybox.a2sd echo "[ ] Removing /data/app link.";
		  /system/bin/busybox.a2sd rm -f /data/app;
		  /system/bin/busybox.a2sd echo "[ ] Creating /data/app directory.";
		  /system/bin/busybox.a2sd mkdir /data/app;
                  /system/bin/busybox.a2sd echo "[ ] Setting permissions on /data/app";
		  /system/bin/busybox.a2sd chmod 777 /data/app;
		  /system/bin/busybox.a2sd echo "[ ] Copying /data/app to /system/sd/app";
		  /system/bin/busybox.a2sd cp /system/sd/app/* /data/app;
	      fi;
	      if [ -h /data/app-private ];
	        then
 		  /system/bin/busybox.a2sd echo "[ ] Removing /data/app-private link.";
		  /system/bin/busybox.a2sd rm -f /data/app-private;
		  /system/bin/busybox.a2sd echo "[ ] Creating /data/app-private directory.";
		  /system/bin/busybox.a2sd mkdir /data/app-private;
                  /system/bin/busybox.a2sd echo "[ ] Setting permissions on /data/app-private";
		  /system/bin/busybox.a2sd chmod 777 /data/app-private;
		  /system/bin/busybox.a2sd echo "[ ] Copying /data/app-private to /system/sd/app-private";
		  /system/bin/busybox.a2sd cp /system/sd/app-private/* /data/app-private;
	      fi;
	      if [ ! -e /system/sd/.dalvikcache ] && [ ! -e /system/sd/.dcpartition ];
	        then
  		  if [ -h /data/dalvik-cache ];
		    then
	              /system/bin/busybox.a2sd echo "[ ] Removing /data/dalvik-cache link.";
		      /system/bin/busybox.a2sd rm -f /data/dalvik-cache;
		      /system/bin/busybox.a2sd echo "[ ] Creating /data/dalvik-cache directory.";
		      /system/bin/busybox.a2sd mkdir /data/dalvik-cache;
		      /system/bin/busybox.a2sd echo "[ ] Setting permissions on /data/dalvik-cache";
		      /system/bin/busybox.a2sd chmod 777 /data/dalvik-cache;
                  fi;
              fi;
	      /system/bin/busybox.a2sd echo "[ ] Creating No A2SD flag file.";
	      /system/bin/busybox.a2sd echo "x" > /system/sd/.noa2sd;
              if [ $a2sdfix == 1 ];
                then
                  /system/bin/busybox.a2sd echo "[ ] Rebooting Phone.";
                  reboot;
              fi;
              ;;

    "nocache")
	      if [ -h /data/dalvik-cache ];
	        then
		  /system/bin/busybox.a2sd echo "[ ] Removing /data/dalvik-cache link.";
		  /system/bin/busybox.a2sd rm -f /data/dalvik-cache;
		  /system/bin/busybox.a2sd echo "[ ] Creating /data/dalvik-cache directory.";
		  /system/bin/busybox.a2sd mkdir /data/dalvik-cache;
	          /system/bin/busybox.a2sd echo "[ ] Setting permissions on /data/dalvik-cache";
		  /system/bin/busybox.a2sd chmod 777 /data/dalvik-cache;
                  if [ -e /system/sd/.dalvikcache ];
                    then
		      /system/bin/busybox.a2sd echo "[ ] Copying /system/sd/dalvik-cache to /data/dalvik-cache";
		      /system/bin/busybox.a2sd cp /system/sd/dalvik-cache/* /data/dalvik-cache;
                  fi;
                  if [ -e /system/sd/.dcpartition ];
                    then
                      /system/bin/busybox.a2sd echo "[ ] Copying /cache/dalvik-cache to /data/dalvik-cache";
                      /system/bin/busybox.a2sd cp /cache/dalvik-cache/* /data/dalvik-cache;
                  fi;
	      fi;
	      /system/bin/busybox.a2sd echo "[ ] Removing Dalvik Cache flag files.";
	      /system/bin/busybox.a2sd rm -f /system/sd/.dalvikcache;
              /system/bin/busybox.a2sd rm -f /system/sd/.dcpartition;
              ;;

    "cache")
	      if [ ! -h /data/dalvik-cache ];
	        then
		  if [ ! -d /system/sd/dalvik-cache ];
	            then
		      /system/bin/busybox.a2sd echo "[ ] Creating /system/sd/dalvik-cache";
		      /system/bin/busybox.a2sd mkdir /system/sd/dalvik-cache;
		  fi;
		  /system/bin/busybox.a2sd echo "[ ] Copying /data/dalvik-cache to /system/sd/dalvik-cache";
		  /system/bin/busybox.a2sd cp /data/dalvik-cache/* /system/sd/dalvik-cache;
		  /system/bin/busybox.a2sd echo "[ ] Removing /data/dalvik-cache";
		  /system/bin/busybox.a2sd rm -rf /data/dalvik-cache;
		  /system/bin/busybox.a2sd echo "[ ] Creating /data/dalvik-cache link to /system/sd/dalvik-cache";
		  /system/bin/busybox.a2sd ln -s /system/sd/dalvik-cache /data/dalvik-cache;
                else
                  if [ -e /system/sd/.dcpartition ];
                    then
                      if [ ! -d /system/sd/dalvik-cache ];
                        then
                          /system/bin/busybox.a2sd echo "[ ] Creating /system/sd/dalvik-cache";
                          /system/bin/busybox.a2sd mkdir /system/sd/dalvik-cache;
                      fi;
                      /system/bin/busybox.a2sd echo "[ ] Copying /data/dalvik-cache to /system/sd/dalvik-cache";
                      /system/bin/busybox.a2sd cp /data/dalvik-cache/* /system/sd/dalvik-cache;
                      /system/bin/busybox.a2sd echo "[ ] Removing /data/dalvik-cache";
                      /system/bin/busybox.a2sd rm -f /data/dalvik-cache;
                      /system/bin/busybox.a2sd echo "[ ] Creating /data/dalvik-cache link to /system/sd/dalvik-cache";
                      /system/bin/busybox.a2sd ln -s /system/sd/dalvik-cache /data/dalvik-cache;
                      /system/bin/busybox.a2sd echo "[ ] Removing File .dcpartition";
                      /system/bin/busybox.a2sd rm -f /system/sd/.dcpartition;
                  fi;
	      fi;
	      /system/bin/busybox.a2sd echo  "[ ] Creating Dalvik Cache on SD flag file.";
	      /system/bin/busybox.a2sd echo "x" > /system/sd/.dalvikcache;
              ;;

    "cachepart")
	      if [ ! -h /data/dalvik-cache ];
	        then
		  if [ ! -d /cache/dalvik-cache ];
	            then
		      /system/bin/busybox.a2sd echo "[ ] Creating /cache/dalvik-cache";
		      /system/bin/busybox.a2sd mkdir /cache/dalvik-cache;
		  fi;
		  /system/bin/busybox.a2sd echo "[ ] Copying /data/dalvik-cache to /cache/dalvik-cache";
		  /system/bin/busybox.a2sd cp /data/dalvik-cache/* /cache/dalvik-cache;
		  /system/bin/busybox.a2sd echo "[ ] Removing /data/dalvik-cache";
		  /system/bin/busybox.a2sd rm -rf /data/dalvik-cache;
		  /system/bin/busybox.a2sd echo "[ ] Creating /data/dalvik-cache link to /cache/dalvik-cache";
		  /system/bin/busybox.a2sd ln -s /cache/dalvik-cache /data/dalvik-cache;
                else
                  if [ -e /system/sd/.dalvikcache ];
                    then
                      if [ ! -d /cache/dalvik-cache ];
                        then
                          /system/bin/busybox.a2sd echo "[ ] Creating /cache/dalvik-cache";
                          /system/bin/busybox.a2sd mkdir /cache/dalvik-cache;
                      fi;
                      /system/bin/busybox.a2sd echo "[ ] Copying /data/dalvik-cache to /cache/dalvik-cache";
                      /system/bin/busybox.a2sd cp /data/dalvik-cache/* /cache/dalvik-cache;
                      /system/bin/busybox.a2sd echo "[ ] Removing /data/dalvik-cache";
                      /system/bin/busybox.a2sd rm -f /data/dalvik-cache;
                      /system/bin/busybox.a2sd echo "[ ] Creating /data/dalvik-cache link to /cache/dalvik-cache";
                      /system/bin/busybox.a2sd ln -s /cache/dalvik-cache /data/dalvik-cache;
                      /system/bin/busybox.a2sd echo "[ ] Removing File .dalvikcache";
                      /system/bin/busybox.a2sd rm -f /system/sd/.dalvikcache;
                  fi;
	      fi;
	      /system/bin/busybox.a2sd echo  "[ ] Creating Dalvik Cache on Cache flag file.";
	      /system/bin/busybox.a2sd echo "x" > /system/sd/.dcpartition;
              ;;

    "clearcache")
              /system/bin/busybox.a2sd echo "[ ] Resetting Dalvik Cache.";
              /system/bin/busybox rm -f /data/dalvik-cache/*;
              /system/bin/busybox.a2sd echo "[ ] Rebooting Phone.";
              reboot;
              ;;

    "resetcache")
              /system/bin/busybox.a2sd echo "[ ] Resetting Dalvik Cache.";
              /system/bin/busybox rm -f /data/dalvik-cache/*;
              /system/bin/busybox.a2sd echo "[ ] Rebooting Phone.";
              reboot;
              ;;

    "swap")
	      /system/bin/busybox.a2sd echo "[ ] Removing No Swap flag file.";
	      /system/bin/busybox.a2sd rm /system/sd/.noswap;
              ;;

    "noswap")
	      /system/bin/busybox.a2sd echo "[ ] Creating No Swap flag file.";
	      /system/bin/busybox.a2sd echo "x" > /system/sd/.noswap;
              ;;

    "zipalign")
              /system/bin/busybox.a2sd echo "[ ] Creating ZipAlign flag file.";
              /system/bin/busybox.a2sd echo "x" > /system/sd/.zipalign;
              ;;

     "nozipalign")
               /system/bin/busybox.a2sd echo "[ ] Removing ZipAlign flag file.";
               /system/bin/busybox.a2sd rm /system/sd/.zipalign;
               ;;

     "fixmarket")
               export a2sdosver21=`/system/bin/toolbox getprop ro.build.version.release | grep -q -e 2.1;echo $?;`;
               if [ $a2sdosver21 == 0 ];
                 then
                   /system/bin/busybox.a2sd echo "[*] Android version 2.1 detected.";
                   /system/bin/busybox.a2sd echo "[ ] Removing Marketplace";
                   if [ -e /data/app/com.android.vending.apk ];
                     then
                       /system/bin/busybox.a2sd rm -f /data/app/com.android.vending.apk;
                    fi;
                    /system/bin/busybox.a2sd rm -f /system/app/Vending.apk;
                    /system/bin/busybox.a2sd echo "[ ] Copy Marketplace to /system/app.";
                    /system/bin/busybox.a2sd cp -f /data/a2sd/Vending.apk /system/app/Vending.apk;
                    /system/bin/busybox.a2sd echo "[ ] Clearing Marketplace Cache.";
                    /system/bin/busybox.a2sd rm -rf /data/data/com.android.vending/cache/*;
                    /system/bin/busybox.a2sd echo "[*] Marketplace repaired.";
                  else
                    /system/bin/busybox.a2sd echo "[X] Marketplace Fix is only for Android version 2.1";
                    /system/bin/busybox.a2sd echo "    Your version is: `/system/bin/toolbox getprop ro.build.version.release`";
               fi;
               ;;      
               
esac;           
/system/bin/busybox.a2sd echo "[ ] Remounting /system as read-only.";
/system/bin/busybox.a2sd mount -o remount,ro /system;

