cros_sdk: optimize gconftool interaction

It doesn't make much sense to run gconftool when the default automount
value is what we already want: false.  So in this common case, skip the
save/restore logic altogether since we aren't actually changing things.

Further, we can merge the `which` check in by checking the exit status
of the `gconftool` run.  If it fails, the tool doesn't exist.

BUG=None
TEST=enable `set -x`; run `cros_sdk true`; see gconftool run once in common case

Change-Id: I1c6aee945218a8a9df533d9ef207c750f909c252
Reviewed-on: http://gerrit.chromium.org/gerrit/7823
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh
index de7dfc6..6dd210a 100755
--- a/sdk_lib/enter_chroot.sh
+++ b/sdk_lib/enter_chroot.sh
@@ -282,16 +282,18 @@
     # Turn off automounting of external media when we enter the
     # chroot; thus we don't have to worry about being able to unmount
     # from inside.
-    if [ $(which gconftool-2 2>/dev/null) ]; then
-      gconftool-2 -g ${AUTOMOUNT_PREF} > \
-        "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
-      if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
-        warn "-- Note: USB sticks may be automounted by your host OS."
-        warn "-- Note: If you plan to burn bootable media, you may need to"
-        warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
-        warn "-- Note: outside the chroot."
+    if SAVED_PREF=$(gconftool-2 -g ${AUTOMOUNT_PREF} 2>/dev/null); then
+      if [ "${SAVED_PREF}" != "false" ]; then
+        if [ $(gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} false) ]; then
+          warn "-- Note: USB sticks may be automounted by your host OS."
+          warn "-- Note: If you plan to burn bootable media, you may need to"
+          warn "-- Note: unmount these devices manually, or run image_to_usb.sh"
+          warn "-- Note: outside the chroot."
+        fi
       fi
     fi
+    # Always write the temp file so we can read it when exiting
+    echo "${SAVED_PREF:-false}" > "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}"
 
     if [ -d "$HOME/.subversion" ]; then
       TARGET="/home/${USER}/.subversion"
@@ -377,8 +379,8 @@
     # Remove any dups from lock file while installing new one
     sort -n "$TMP_LOCKFILE" | uniq > "$LOCKFILE"
 
-    if [ $(which gconftool-2 2>/dev/null) ]; then
-      SAVED_PREF=$(cat "${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
+    SAVED_PREF=$(<"${FLAGS_chroot}${SAVED_AUTOMOUNT_PREF_FILE}")
+    if [ "${SAVED_PREF}" != "false" ]; then
       gconftool-2 -s --type=boolean ${AUTOMOUNT_PREF} ${SAVED_PREF} || \
         warn "could not re-set your automount preference."
     fi