Add code to get pw_env_setup from GCS

Add code to get pw_env_setup from GCS. Leave this disabled for now,
but having .bootstrap in .gitignore should allow local testing
before this is turned on.

Change-Id: Ied2e09181fde34166e8e276ed3f8ccfbc44b5a22
Bug: 67
diff --git a/.gitignore b/.gitignore
index 573d338..2a30618 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,6 +3,7 @@
 out/
 bazel-*
 .presubmit/
+.bootstrap/
 
 # Editors
 .idea/
diff --git a/bootstrap.sh b/bootstrap.sh
index 348c642..0a4e06d 100644
--- a/bootstrap.sh
+++ b/bootstrap.sh
@@ -161,8 +161,12 @@
   PW_CARGO_PACKAGE_FILES="$PW_ROOT/pw_env_setup/py/pw_env_setup/cargo_setup/packages.txt:$PW_CARGO_PACKAGE_FILES"
   export PW_CARGO_PACKAGE_FILES
 
-  "$PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" --shell-file "$SETUP_SH"
-
+  # _PW_ENV_SETUP=$("$PW_ROOT/pw_env_setup/get_pw_env_setup.sh")
+  # if [ -n "$_PW_ENV_SETUP" ]; then
+  #   "$_PW_ENV_SETUP" --shell-file "$SETUP_SH"
+  # else
+    "$PYTHON" "$PW_ROOT/pw_env_setup/py/pw_env_setup/env_setup.py" --shell-file "$SETUP_SH"
+  # fi
 
   PW_CIPD_PACKAGE_FILES="$_PW_OLD_CIPD_PACKAGE_FILES"
   PW_VIRTUALENV_REQUIREMENTS="$_PW_OLD_VIRTUALENV_REQUIREMENTS"
@@ -193,6 +197,7 @@
   _pw_red "Error during $_PW_NAME--see messages above."
 fi
 
+unset _PW_ENV_SETUP
 unset _PW_IS_BOOTSTRAP
 unset _PW_NAME
 unset _PIGWEED_BANNER
diff --git a/pw_env_setup/get_pw_env_setup.sh b/pw_env_setup/get_pw_env_setup.sh
new file mode 100755
index 0000000..d58d4cf
--- /dev/null
+++ b/pw_env_setup/get_pw_env_setup.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+# Copyright 2020 The Pigweed Authors
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may not
+# use this file except in compliance with the License. You may obtain a copy of
+# the License at
+#
+#     https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+PREFIX="$PW_ROOT/.bootstrap"
+mkdir -p "$PREFIX"
+
+# Update the mtimes on the most recent pw_env_setup executables.
+for HASH in $(git --git-dir="$PW_ROOT/.git" --no-pager log --max-count=5 --format=format:%H); do
+  if [ -f "$PREFIX/$HASH" ]; then
+    touch "$PREFIX/$HASH" &> /dev/null
+  fi
+done
+
+# Delete any files with an (apparent) age greater than 5 days. This will never
+# include the 5 most recent pw_env_setup executables, but if there's been no
+# bootstrap call in less than 5 days this could delete all version of
+# pw_env_setup. This is acceptable because it's very unlikely there have been
+# no commits in a 5 day period, and if there really have been no commits this
+# will just re-download that executable in a few lines.
+find "$PREFIX" -mtime +5 -exec echo rm {} \;
+
+OS=$(uname -s | tr '[:upper:]' '[:lower:]')
+if [ "$OS" = "darwin" ]; then
+  OS=mac
+fi
+
+ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
+if [ "$ARCH" = "x86_64" ]; then
+  ARCH="amd64"
+fi
+
+for HASH in $(git --git-dir="$PW_ROOT/.git" --no-pager log --max-count=10 --format=format:%H); do
+  URL="https://storage.googleapis.com/pigweed-envsetup/$OS-$ARCH"
+  URL="$URL/$HASH/pw_env_setup"
+  FILEPATH="$PREFIX/$HASH"
+
+  # First try curl.
+  if [ ! -f "$FILEPATH" ]; then
+    curl -o "$FILEPATH" "$URL" &> /dev/null
+  fi
+
+  # If curl fails try wget.
+  if [ ! -f "$FILEPATH" ]; then
+    wget -O "$FILEPATH" "$URL" &> /dev/null
+  fi
+
+  # If either curl or wget succeeded mark the file executable, print it, and
+  # exit. If the file appears to be a text file then it doesn't exist in GCS
+  # and we'll try the next one.
+  TEXT=$(file --mime "$FILEPATH" | grep text)
+  if [ -n "$TEXT" ]; then
+    rm "$FILEPATH"
+    continue
+  fi
+
+  if [ -f "$FILEPATH" ]; then
+    chmod a+x "$FILEPATH"
+    echo "$FILEPATH"
+    exit 0
+  fi
+done
+
+exit -1