Modify gmerge to allow for any arguments to emerge and have it work with statefuldev

emerge require make.globals in /etc, however re-rooting emerge to install in /usr/local places
make.globals in /usr/local/etc.  This works around this issue and has gmerge work for updating
chromeos (not dev) packages on a system.  I also added some desired functionality to
pass arbitrary emerge options to gmerge making it more like a true wrapper.

Review URL: http://codereview.chromium.org/1707007
diff --git a/gmerge b/gmerge
index 89c94a1..9df98fd 100755
--- a/gmerge
+++ b/gmerge
@@ -8,8 +8,12 @@
 
 build=1
 
-if [ x$1 == x-n ] ; then
-  shift
+# Package name is the last argument.
+package_name=${!#}
+
+# If no package name is provided skip to emerge options or if -n is given.
+if [[ $package_name == -* ]] || [ x$1 == x-n ]
+then
   build=0
 fi
 
@@ -32,15 +36,31 @@
 mkdir -p /etc/make.profile
 
 if [ $build == 1 ] ; then
-  echo "Building $1"
-  ESCAPED_PACKAGE=$(python -c "import urllib; print urllib.quote('''$1''')")
+  echo "Building $package_name"
+  ESCAPED_PACKAGE=$(python -c \
+    "import urllib; print urllib.quote('''$package_name''')")
   ESCAPED_BOARD=$(python -c \
   "import urllib; print urllib.quote('''${BOARD_NAME}''')")
 
-  wget $DEVKIT_URL/build --post-data="pkg=${ESCAPED_PACKAGE}&board=${ESCAPED_BOARD}"
+  wget $DEVKIT_URL/build \
+    --post-data="pkg=${ESCAPED_PACKAGE}&board=${ESCAPED_BOARD}"
 fi
 
-echo "Emerging $1"
+# Installing emerge into /usr/local installs make.globals needed in
+# /usr/local/etc rather than /etc.
+if [ ! -f /etc/make.globals ]
+then
+  if [ -f /usr/local/etc/make.globals ]
+  then
+    echo "Missing /etc/make.globals, copying over from /usr/local/etc"
+    sudo cp /usr/local/etc/make.globals /etc
+   else
+    echo "Missing /etc/make.globals and none in /usr/local/etc.  Aborting."
+    exit 1
+  fi
+fi
+
+echo "Emerging $package_name"
 
 export PORTAGE_BINHOST="${DEVKIT_URL}/static/pkgroot/${BOARD_NAME}/packages"
 export PORTAGE_TMPDIR=/tmp
@@ -49,4 +69,4 @@
 # unstable changes from upstream for packages that you do not want.
 export ACCEPT_KEYWORDS='~x86 x86'
 
-emerge --getbinpkg --usepkgonly $1
+FEATURES="-sandbox" emerge --getbinpkg --usepkgonly "$@"