#!/system/bin/bash

# Darktremor Apps2SD (A2SD) version 2.7
# Based on the Cyanogen 1.5 Scripts
# Date:  June 21, 2010
# Bourne Again Shell (bash) courtesy of mzet (http://pub.mzet.net/bash)

# Note: This script requires the Bourne Again Shell (bash)

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

/system/bin/busybox.a2sd echo "[ ] Starting A2SD in $a2sdcmd mode";

export a2sdfs=`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=`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=`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 ] && [ $a2sdfix2 == -1 ];
  then
    export a2sdfix=-1;
  else
    if [ $a2sdfix == 1 ] || [ $a2sdfix == 1 ];
      then
        export a2sdfix=1;
      else
        export a2sdfix=0;
     fi;
fi;


case $a2sdcmd
  in
	"check")    /system/bin/busybox.a2sd echo "[ ] Running A2SD Check Program.";
		    /system/bin/bash /system/bin/chka2sd;
		    ;;
        "repair")   /system/bin/busybox.a2sd echo "[ ] Running A2SD Repair Program.";
		    /system/bin/bash /system/bin/fixa2sd;
                    ;;
	"swap")     /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
		    /system/bin/bash /system/bin/setupa2sd swap;
                    ;;
	"noswap")   /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
		    /system/bin/bash /system/bin/setupa2sd noswap;
                    ;;
	"install")  /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
		    /system/bin/bash /system/bin/setupa2sd install;
                    ;;
	"remove")   /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
		    /system/bin/bash /system/bin/setupa2sd remove;
                    ;;
	"cache")    
                    /system/bin/busybox.a2sd echo "[!] Note: a2sd cache will be replaced";
                    /system/bin/busybox.a2sd echo "          in the future by a2sd cachesd";
                    /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
		    /system/bin/bash /system/bin/setupa2sd cache;
                    ;;
        "cachepart")
                    /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
                    /system/bin/bash /system/bin/setupa2sd cachepart;
                    ;;
        "cachesd")
                    /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
                    /system/bin/bash /system/bin/setupa2sd cache;
                    ;;
	"nocache")  /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
		    /system/bin/bash /system/bin/setupa2sd nocache;
                    ;;
        "resetcache")
                    /system/bin/busybox.a2sd echo "[ ] Resetting Dalvik-Cache";
                    cd /data/dalvik-cache;
                    /system/bin/busybox.a2sd rm -f *;
                    reboot;
                    ;;
        "clearcache")
                    /system/bin/busybox.a2sd echo "[ ] Resetting Dalvik-Cache";
                    cd /data/dalvik-cache;
                    /system/bin/busybox.a2sd rm -f *;
                    reboot;
                    ;;
        "fixmarket")
                    /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
		    /system/bin/bash /system/bin/setupa2sd fixmarket;
                    ;;
        "diskspace")
                    /system/bin/busybox.a2sd echo "[ ] Displaying Disk Space statistics on SD card";
                    /system/bin/busybox.a2sd df -h /dev/block/mmcblk0p2;
                    ;;

	"boot")     if [ $a2sdfix == 0 ];
                      then
                        /system/bin/bash /system/bin/boota2sd;
                        exit;
                    fi;
                    if [ $a2sdfix == 1 ];
                      then
                        /system/bin/busybox.a2sd echo "[X] You must execute a command.";
                        /system/bin/busybox.a2sd echo "    Type a2sd help for a list of commands.";
                    fi;
                    ;;
        "align")    /system/bin/busybox.a2sd echo "[ ] Running A2SD ZipAlign Program";
                    /system/bin/bash /system/bin/aligna2sd;
                    ;;
        "nozipalign")
                    /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
                    /system/bin/bash /system/bin/setupa2sd nozipalign;
                    ;;
        "zipalign")
                    /system/bin/busybox.a2sd echo "[ ] Running A2SD Setup Program.";
                    /system/bin/bash /system/bin/setupa2sd zipalign;
                    ;;
        "help")     /system/bin/busybox.a2sd echo "Darktremor A2SD/Apps2SD Help";
		    /system/bin/busybox.a2sd echo "Version $a2sdver";
		    /system/bin/busybox.a2sd echo "=================================================";
		    /system/bin/busybox.a2sd echo "Syntax: /system/bin/a2sd command";
		    /system/bin/busybox.a2sd echo "";
		    /system/bin/busybox.a2sd echo "Command is one of the following";
		    /system/bin/busybox.a2sd echo "";
		    /system/bin/busybox.a2sd echo "install";
		    /system/bin/busybox.a2sd echo "  Moves free and paid apps to the ext2/ext3/ext4";
		    /system/bin/busybox.a2sd echo "  partition on your secure digital card.  Removes";
                    /system/bin/busybox.a2sd echo "  the No A2SD flag file.";
		    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "remove";
                    /system/bin/busybox.a2sd echo "  Moves free and paid apps to the internal phone";
                    /system/bin/busybox.a2sd echo "  storage.  Creates the No A2SD flag file.";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "swap";
                    /system/bin/busybox.a2sd echo "  Removes the No Swap flag.  Allows A2SD to ";
                    /system/bin/busybox.a2sd echo "  activate the swap partition.";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "noswap";
                    /system/bin/busybox.a2sd echo "  Creates the No Swap flag.  Prevents A2SD from";
                    /system/bin/busybox.a2sd echo "  activating the swap partition.";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "cachesd";
                    /system/bin/busybox.a2sd echo "  Moves Dalvik cache files to the ext2/ext3/ext4";
                    /system/bin/busybox.a2sd echo "  partition on your secure digital card. Creates";
                    /system/bin/busybox.a2sd echo "  Dalvik Cache to SD flag.";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "cachepart";
                    /system/bin/busybox.a2sd echo "  Moves Dalvik cache files to the /cache partition";
                    /system/bin/busybox.a2sd echo "  in your phone.  Creates Dalvik Cache to Cache";
                    /system/bin/busybox.a2sd echo "  Partition flag.";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "nocache";
                    /system/bin/busybox.a2sd echo "  Moves Dalvik cache files to the internal phone";
                    /system/bin/busybox.a2sd echo "  storage.  Removes all Dalvik Cache flags.";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "resetcache";
                    /system/bin/busybox.a2sd echo "  Clears the dalvik cache and restarts phone.";
                    /system/bin/busybox.a2sd echo "  Same as clearcache";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "clearcache";
                    /system/bin/busybox.a2sd echo "  Clears the dalvik cache and restarts phone.";
                    /system/bin/busybox.a2sd echo "  Same as resetcache";
                    /system/bin/busybox.a2sd echo ""; 
                    /system/bin/busybox.a2sd echo "check";
                    /system/bin/busybox.a2sd echo "  Performs a symlink and flag file check on your";
                    /system/bin/busybox.a2sd echo "  setup.  Outputs results.";
                    /system/bin/busybox.a2sd echo ""
                    /system/bin/busybox.a2sd echo "repair";
                    /system/bin/busybox.a2sd echo "  Performs a repair of all symlinks based on ";
                    /system/bin/busybox.a2sd echo "  flag file activation.";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "align";
                    /system/bin/busybox.a2sd echo "  Performs a zipalign on all APK files in ";
                    /system/bin/busybox.a2sd echo "  /data/app, /data/app-private and ";
                    /system/bin/busybox.a2sd echo "  /system/app.";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "zipalign";
                    /system/bin/busybox.a2sd echo "  Allows ZipAlign to execute during the boot ";
                    /system/bin/busybox.a2sd echo "  process. Creates the ZipAlign flag file.";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "nozipalign";
                    /system/bin/busybox.a2sd echo "  Prevents ZipAlign from executing during the ";
                    /system/bin/busybox.a2sd echo "  boot process. Removes the ZipAlign flag file.";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "fixmarket";
                    /system/bin/busybox.a2sd echo "  Fixes missing market issue in Android 2.1";
                    /system/bin/busybox.a2sd echo "";
                    /system/bin/busybox.a2sd echo "diskspace";
                    /system/bin/busybox.a2sd echo "   Shows disk space statistics for the ext";
                    /system/bin/busybox.a2sd echo "   partition.";
                    /system/bin/busybox.a2sd echo "";
                    ;;
	*)          /system/bin/busybox.a2sd echo "[X] Invalid command: $a2sdcmd";
                    ;;
    esac;

/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;

