Rob Mohr | 1908715 | 2020-05-08 10:06:39 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Copyright 2020 The Pigweed Authors |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | # use this file except in compliance with the License. You may obtain a copy of |
| 6 | # the License at |
| 7 | # |
| 8 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations under |
| 14 | # the License. |
| 15 | |
Rob Mohr | ce87bc0 | 2020-07-15 11:16:17 -0700 | [diff] [blame] | 16 | if [ -z "$PW_ENVIRONMENT_ROOT" ]; then |
| 17 | PW_ENVIRONMENT_ROOT="$PW_ROOT/.environment" |
| 18 | fi |
| 19 | PREFIX="$PW_ENVIRONMENT_ROOT/bootstrap" |
Rob Mohr | 1908715 | 2020-05-08 10:06:39 -0700 | [diff] [blame] | 20 | mkdir -p "$PREFIX" |
| 21 | |
Rob Mohr | 788da9e | 2020-07-22 12:52:46 -0700 | [diff] [blame] | 22 | set -o errexit |
| 23 | |
Rob Mohr | 1908715 | 2020-05-08 10:06:39 -0700 | [diff] [blame] | 24 | # Update the mtimes on the most recent pw_env_setup executables. |
| 25 | for HASH in $(git --git-dir="$PW_ROOT/.git" --no-pager log --max-count=5 --format=format:%H); do |
| 26 | if [ -f "$PREFIX/$HASH" ]; then |
| 27 | touch "$PREFIX/$HASH" &> /dev/null |
| 28 | fi |
| 29 | done |
| 30 | |
| 31 | # Delete any files with an (apparent) age greater than 5 days. This will never |
| 32 | # include the 5 most recent pw_env_setup executables, but if there's been no |
Rob Mohr | 788da9e | 2020-07-22 12:52:46 -0700 | [diff] [blame] | 33 | # bootstrap call in less than 5 days this could delete all versions of |
Rob Mohr | 1908715 | 2020-05-08 10:06:39 -0700 | [diff] [blame] | 34 | # pw_env_setup. This is acceptable because it's very unlikely there have been |
| 35 | # no commits in a 5 day period, and if there really have been no commits this |
| 36 | # will just re-download that executable in a few lines. |
Rob Mohr | 788da9e | 2020-07-22 12:52:46 -0700 | [diff] [blame] | 37 | find "$PREFIX" -mtime +5 -exec rm {} \; |
Rob Mohr | 1908715 | 2020-05-08 10:06:39 -0700 | [diff] [blame] | 38 | |
| 39 | OS=$(uname -s | tr '[:upper:]' '[:lower:]') |
| 40 | if [ "$OS" = "darwin" ]; then |
| 41 | OS=mac |
| 42 | fi |
| 43 | |
| 44 | ARCH=$(uname -m | tr '[:upper:]' '[:lower:]') |
| 45 | if [ "$ARCH" = "x86_64" ]; then |
| 46 | ARCH="amd64" |
| 47 | fi |
| 48 | |
Chad Norvell | f1511cd | 2022-03-15 20:46:58 -0700 | [diff] [blame] | 49 | # Support `mac-arm64` through Rosetta until `mac-arm64` binaries are ready |
| 50 | if [[ "$OS" = "mac" ] && [ "$ARCH" = "arm64" ]]; then |
| 51 | ARCH="amd64" |
| 52 | fi |
| 53 | |
Rob Mohr | 1908715 | 2020-05-08 10:06:39 -0700 | [diff] [blame] | 54 | for HASH in $(git --git-dir="$PW_ROOT/.git" --no-pager log --max-count=10 --format=format:%H); do |
| 55 | URL="https://storage.googleapis.com/pigweed-envsetup/$OS-$ARCH" |
| 56 | URL="$URL/$HASH/pw_env_setup" |
| 57 | FILEPATH="$PREFIX/$HASH" |
| 58 | |
| 59 | # First try curl. |
| 60 | if [ ! -f "$FILEPATH" ]; then |
| 61 | curl -o "$FILEPATH" "$URL" &> /dev/null |
| 62 | fi |
| 63 | |
| 64 | # If curl fails try wget. |
| 65 | if [ ! -f "$FILEPATH" ]; then |
| 66 | wget -O "$FILEPATH" "$URL" &> /dev/null |
| 67 | fi |
| 68 | |
| 69 | # If either curl or wget succeeded mark the file executable, print it, and |
| 70 | # exit. If the file appears to be a text file then it doesn't exist in GCS |
| 71 | # and we'll try the next one. |
| 72 | TEXT=$(file --mime "$FILEPATH" | grep text) |
| 73 | if [ -n "$TEXT" ]; then |
| 74 | rm "$FILEPATH" |
| 75 | continue |
| 76 | fi |
| 77 | |
| 78 | if [ -f "$FILEPATH" ]; then |
| 79 | chmod a+x "$FILEPATH" |
| 80 | echo "$FILEPATH" |
| 81 | exit 0 |
| 82 | fi |
| 83 | done |
| 84 | |
| 85 | exit -1 |