#!/system/bin/bash
# Darktremor A2SD 2.7 Repair Setup Program
# By: Robert Travis Kirton
# Date:  June 21, 2010
# Based on code from Darktremor A2SD 2.6.1-R2"

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 a2sdcachefix=0;
export a2sdrunfix=`/system/bin/toolbox getprop cm.a2sd.runfix`;

if [ !$a2sdrunfix ];
  then
    export a2sdrunfix=0;
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 [ !$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 [ $a2sdfix != 1 ];
  then
    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.";
            /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 "[ ] Remounting /system as 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."; 
        /system/bin/busybox.a2sd echo "[ ] Remounting /system as read-only.";
        /system/bin/busybox.a2sd mount -o remount,ro /system;
        exit;
    fi;
fi;
/system/bin/busybox.a2sd echo "[ ] Checking A2SD links.";
/system/bin/busybox.a2sd echo "";
/system/bin/busybox.a2sd echo "A2SD Status Report";
        /system/bin/busybox.a2sd echo "===================================================";

if [ -h /data/app ];
  then
    if [ -e /system/sd/.noa2sd ];
      then
        /system/bin/busybox.a2sd echo "Apps          - [-] Linked to SD Card.";
        /system/bin/busybox.a2sd echo "                [-] File .noa2sd found.";
        /system/bin/busybox.a2sd echo "                [X] Programs run from SD card";
        /system/bin/busybox.a2sd rm -rf /data/app;
        /system/bin/busybox.a2sd mkdir /data/app;
        /system/bin/busybox.a2sd chmod 777 /data/app;
        /system/bin/busybox.a2sd cp /system/sd/app/* /data/app;
        /system/bin/busybox.a2sd echo "                [*] Issue repaired.";
      else
        /system/bin/busybox.a2sd echo "Apps          - [-] Linked to SD Card.";
        /system/bin/busybox.a2sd echo "                [-] File .noa2sd not found.";
        /system/bin/busybox.a2sd echo "                [*] Programs run from SD card";
        /system/bin/busybox.a2sd echo "";
    fi;
  else
    if [ -e /system/sd/.noa2sd ];
      then
        /system/bin/busybox.a2sd echo "Apps          - [-] Regular directory.";
        /system/bin/busybox.a2sd echo "                [-] File .noa2sd found.";
        /system/bin/busybox.a2sd echo "                [!] Programs run from Int. Storage.";
        /system/bin/busybox.a2sd echo "";
      else
        /system/bin/busybox.a2sd echo "Apps          - [-] Regular directory.";
        /system/bin/busybox.a2sd echo "                [-] File .noa2sd not found.";
        /system/bin/busybox.a2sd echo "                [X] Programs runs from Int. Storage.";
        if [ ! -d /system/sd/app ];
          then
            /system/bin/busybox.a2sd mkdir /system/sd/app; 
            /system/bin/busybox.a2sd chmod 777 /system/sd/app;
         fi;
         /system/bin/busybox.a2sd cp /data/app/* /system/sd/app;
         /system/bin/busybox.a2sd rm -rf /data/app;
         /system/bin/busybox.a2sd ln -s /system/sd/app /data/app;
         /system/bin/busybox.a2sd echo "               [*] Issue repaired.";
    fi;
fi;

if [ -h /data/app-private ];
  then
    if [ -e /system/sd/.noa2sd ];
      then
        /system/bin/busybox.a2sd echo "Private Apps  - [-] Linked to SD Card.";
        /system/bin/busybox.a2sd echo "                [-] File .noa2sd found.";
        /system/bin/busybox.a2sd echo "                [X] Private Programs run from SD card";
        /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;
        /system/bin/busybox.a2sd cp /system/sd/app-private/* /data/app-private;
        /system/bin/busybox.a2sd echo "                [*] Issue repaired.";
      else
        /system/bin/busybox.a2sd echo "Private Apps  - [-] Linked to SD Card.";
        /system/bin/busybox.a2sd echo "                [-] File .noa2sd not found.";
        /system/bin/busybox.a2sd echo "                [*] Private Programs run from SD card";
        /system/bin/busybox.a2sd echo "";
    fi;
  else
    if [ -e /system/sd/.noa2sd ];
      then
        /system/bin/busybox.a2sd echo "Private Apps  - [-] Regular directory.";
        /system/bin/busybox.a2sd echo "                [-] File .noa2sd found.";
        /system/bin/busybox.a2sd echo "                [!] Private Programs run from Int. Storage.";
        /system/bin/busybox.a2sd echo "";
      else
        /system/bin/busybox.a2sd echo "Private Apps  - [-] Regular directory.";
        /system/bin/busybox.a2sd echo "                [-] File .noa2sd not found.";
        /system/bin/busybox.a2sd echo "                [X] Private Programs runs from Int. Storage.";
        if [ ! -d /system/sd/app-private ];
          then
            /system/bin/busybox.a2sd mkdir /system/sd/app-private; 
            /system/bin/busybox.a2sd chmod 777 /system/sd/app-private;
         fi;
         /system/bin/busybox.a2sd cp /data/app-private/* /system/sd/app-private;
         /system/bin/busybox.a2sd rm -rf /data/app-private;
         /system/bin/busybox.a2sd ln -s /system/sd/app-private /data/app-private;
        /system/bin/busybox.a2sd echo "                [*] Issue repaired.";
    fi;
fi;

cd /data/dalvik-cache;
export a2sddcloc=`/system/bin/busybox.a2sd pwd;`;

if [ -h /data/dalvik-cache ];
  then
    if [ ! -e /system/sd/.dalvikcache ] && [ ! -e /system/sd/.dcpartition ];
      then
        if [ $a2sddcloc == "/cache/dalvik-cache" ];
          then
        /system/bin/busybox.a2sd echo "Dalvik Cache  - [-] Symbolically Linked.";
        /system/bin/busybox.a2sd echo "                [-] File .dalvikcache not found.";
        /system/bin/busybox.a2sd echo "                [-] File .dalvikcachepart not found.";
        /system/bin/busybox.a2sd echo "                [-] Dalvik-cache found on /cache.";
        /system/bin/busybox.a2sd echo "                [X] Dalvik runs from /cache partition.";
        /system/bin/busybox.a2sd echo "                [-] Creating .dalvikcachepart flag.";
        /system/bin/busybox.a2sd echo "x" > /system/sd/.dcpartition;
        /system/bin/busybox.a2sd echo "                [*] Issue resolved.";
        /system/bin/busybox.a2sd echo "";
        export a2sdcachefix=1;
           else
        /system/bin/busybox.a2sd echo "Dalvik Cache  - [-] Symbolically Linked.";
        /system/bin/busybox.a2sd echo "                [-] File .dalvikcache not found.";
        /system/bin/busybox.a2sd echo "                [-] File .dalvikcachepart not found.";
        /system/bin/busybox.a2sd echo "                [X] Dalvik runs from SD card.";
        /system/bin/busybox.a2sd echo "                [-] Moving data 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;
        /system/bin/busybox.a2sd echo "                [*] Issue resolved.";
        if [ $a2sdfix == 1 ];
          then
            /system/bin/busybox.a2sd echo "[ ] Rebooting Phone.";
            /system/bin/reboot.a2sd;
        fi;
        /system/bin/busybox.a2sd echo "";
        export a2sdcachefix=1;
        fi;
      else
        if [ -e /system/sd.dcpartition ];
          then
        /system/bin/busybox.a2sd echo "Dalvik Cache  - [-] Symbolically Linked.";
        /system/bin/busybox.a2sd echo "                [-] File .dalvikcachepart found.";
        /system/bin/busybox.a2sd echo "                [*] Dalvik runs from /cache partition.";
        /system/bin/busybox.a2sd echo "";
           else
        /system/bin/busybox.a2sd echo "Dalvik Cache  - [-] Symbolically Linked.";
        /system/bin/busybox.a2sd echo "                [-] File .dalvikcache found.";
        /system/bin/busybox.a2sd echo "                [*] Dalvik runs from SD card";
        /system/bin/busybox.a2sd echo "";
        fi;
    fi;
  else
    if [ ! -e /system/sd/.dalvikcache ] && [ ! -e /system/sd/.dcpartition ];
      then
        /system/bin/busybox.a2sd echo "Dalvik Cache  - [-] Regular directory.";
        /system/bin/busybox.a2sd echo "                [-] File .dalvikcache not found.";
        /system/bin/busybox.a2sd echo "                [-] File .dalvikcachepart not found.";
        /system/bin/busybox.a2sd echo "                [!] Dalvik runs from Int. Storage.";
        /system/bin/busybox.a2sd echo "";
      else
        if [ -e /system/sd/.dcpartition ];
          then
        /system/bin/busybox.a2sd echo "Dalvik Cache   - [-] Regular directory.";
        /system/bin/busybox.a2sd echo "                 [-] File .dalvikcachepart found.";
        /system/bin/busybox.a2sd echo "                 [X] Dalvik runs from Int. Storage.";
             if [ ! -d /cache/dalvik-cache ];
               then
                 /system/bin/busybox.a2sd mkdir /cache/dalvik-cache;
                 /system/bin/busybox.a2sd chmod 777 /cache/dalvik-cache;
              fi;
              /system/bin/busybox.a2sd rm -rf /data/dalvik-cache;
              /system/bin/busybox.a2sd ln -s /cache/dalvik-cache /data/dalvik-cache;
        /system/bin/busybox.a2sd echo "                 [*] Issue repaired.";
             if [ $a2sdfix == 1 ];
               then
        /system/bin/busybox.a2sd echo "                 [-] Rebooting Phone.";
                 /system/bin/reboot.a2sd;
             fi;         
             export a2sdcachefix=1;
          else
        /system/bin/busybox.a2sd echo "Dalvik Cache   - [*] Regular directory.";
        /system/bin/busybox.a2sd echo "                 [*] File .dalvikcache found.";
        /system/bin/busybox.a2sd echo "                 [X] Dalvik runs from Int. Storage.";
            if [ ! -d /system/sd/dalvik-cache ];
              then
                /system/bin/busybox.a2sd mkdir /system/sd/dalvik-cache; 
                /system/bin/busybox.a2sd chmod 777 /system/sd/dalvik-cache;
            fi;
            /system/bin/busybox.a2sd rm -rf /data/dalvik-cache;
            /system/bin/busybox.a2sd ln -s /system/sd/dalvik-cache /data/dalvik-cache;
        /system/bin/busybox.a2sd echo "                 [*] Issue repaired.";
             if [ $a2sdfix == 1 ];
               then
        /system/bin/busybox.a2sd echo "                 [-] Rebooting Phone.";
                 /system/bin/reboot.a2sd;
             fi;         
             export a2sdcachefix=1;
        fi;
    fi;
fi;

if [ $a2sdfix == 1 ];
  then
    /system/bin/toolbox setprop cm.a2sd.runfix 0;
fi;

/system/bin/busybox.a2sd echo "[ ] Remounting /system as read-only.";
/system/bin/busybox.a2sd mount -o remount,ro /system;

