eat: Automated install of fresh builds

Needs:
1 - a build
2 - a connected device

Change-Id: I24820b984e79430b7af7ccedc171fc69269c0a32

envsetup: eat safely

Change-Id: I2d8b52206213d8f4d8dcce3518cadb2be59e78d8

eat more safely, if there is something to eat

Check for zip file presence before trying to eat

Change-Id: I7913be619d189bbd723263edad878d6d05b8a5a7

Fixed eat not waiting for device if adb server was not started

$(adb get-state) returned unexpected starting server message
causing the if statement to fail. Calling "adb start-server"
beforehand remedies that.

Patch Set 2:
$(adb get-state) returns 'unknown' if Clockwork recovery is running on the device
look for /sbin/recovery and 'pass' the state test
also
adb wait-for-device
will result in endless loop, replace with until loop using with get-state OR 'recovery' tests

Patch Set 3:
fix the initial adb state test, use AND instead of OR

Patch Set 4:
Rebased and prevent "device not found" message from spamming the screen by
redirecting error output to null

Change-Id: I2d41b8853567cde80bf7fc08b5e4f0ad5ba1fdf5
Signed-off-by: Firerat <firer4t@googlemail.com>

Updated for CM9 variables

eat: Adapt to new file naming scheme

Change-Id: I101e333918bc215bebcc594c97bdd68307c90aaa

eat: Adapt to new storage paths

Change-Id: Ic060304275c2d19858636c84740b3f60dfdc6193

envsetup: Make eat restart adbd as root before pushing the recovery command

Without adbd running as root we can't write to /cache/recovery and
eat fails with a permission denied.

Change-Id: I015b50f29c001605cdc1a0088f68296b62809b56

Eat: Option to specify directory of zip in recovery mode

It pushes to /mnt/sdcard0, which may be /emmc/ in recovery. Now the user
can specify another path other than /sdcard/ where the zip is

Change-Id: Ie39615132ae39d9f6b304a3049b2395b2a2af182

fix "eat"

Assume user 0 is the active one when flashing...

Change-Id: Ibdc28ef49f8023e786ee82c9e42c221409dfd6bb

eat: Use /cache if possible

 * Check the size of /cache and use it if possible.

Change-Id: I72fecf3f8d2311c04e63fe81a7ca9a8a6a40419d
diff --git a/envsetup.sh b/envsetup.sh
index cfc02c7..d20d874 100644
--- a/envsetup.sh
+++ b/envsetup.sh
@@ -701,6 +701,58 @@
     printconfig
 }
 
+function eat()
+{
+    if [ "$OUT" ] ; then
+        MODVERSION=`sed -n -e'/ro\.cm\.version/s/.*=//p' $OUT/system/build.prop`
+        ZIPFILE=cm-$MODVERSION.zip
+        ZIPPATH=$OUT/$ZIPFILE
+        if [ ! -f $ZIPPATH ] ; then
+            echo "Nothing to eat"
+            return 1
+        fi
+        adb start-server # Prevent unexpected starting server message from adb get-state in the next line
+        if [ $(adb get-state) != device -a $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) != 0 ] ; then
+            echo "No device is online. Waiting for one..."
+            echo "Please connect USB and/or enable USB debugging"
+            until [ $(adb get-state) = device -o $(adb shell busybox test -e /sbin/recovery 2> /dev/null; echo $?) = 0 ];do
+                sleep 1
+            done
+            echo "Device Found.."
+        fi
+        # if adbd isn't root we can't write to /cache/recovery/
+        adb root
+        sleep 1
+        adb wait-for-device
+        SZ=`stat -c %s $ZIPPATH`
+        CACHESIZE=`adb shell busybox df -PB1 /cache | grep /cache | tr -s ' ' | cut -d ' ' -f 4`
+        if [ $CACHESIZE -gt $SZ ];
+        then
+            PUSHDIR=/cache/
+            DIR=cache
+        else
+            PUSHDIR=/storage/sdcard0/
+             # Optional path for sdcard0 in recovery
+             [ -z "$1" ] && DIR=sdcard/0 || DIR=$1
+        fi
+        echo "Pushing $ZIPFILE to $PUSHDIR"
+        if adb push $ZIPPATH $PUSHDIR ; then
+            cat << EOF > /tmp/command
+--update_package=/$DIR/$ZIPFILE
+EOF
+            if adb push /tmp/command /cache/recovery/ ; then
+                echo "Rebooting into recovery for installation"
+                adb reboot recovery
+            fi
+            rm /tmp/command
+        fi
+    else
+        echo "Nothing to eat"
+        return 1
+    fi
+    return $?
+}
+
 function gettop
 {
     local TOPFILE=build/core/envsetup.mk