blob: ff74220bda7ed22b98c56d9611c1ffd92febf820 [file] [log] [blame]
Rob Mohr19087152020-05-08 10:06:39 -07001#!/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 Mohrce87bc02020-07-15 11:16:17 -070016if [ -z "$PW_ENVIRONMENT_ROOT" ]; then
17 PW_ENVIRONMENT_ROOT="$PW_ROOT/.environment"
18fi
19PREFIX="$PW_ENVIRONMENT_ROOT/bootstrap"
Rob Mohr19087152020-05-08 10:06:39 -070020mkdir -p "$PREFIX"
21
Rob Mohr788da9e2020-07-22 12:52:46 -070022set -o errexit
23
Rob Mohr19087152020-05-08 10:06:39 -070024# Update the mtimes on the most recent pw_env_setup executables.
25for 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
29done
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 Mohr788da9e2020-07-22 12:52:46 -070033# bootstrap call in less than 5 days this could delete all versions of
Rob Mohr19087152020-05-08 10:06:39 -070034# 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 Mohr788da9e2020-07-22 12:52:46 -070037find "$PREFIX" -mtime +5 -exec rm {} \;
Rob Mohr19087152020-05-08 10:06:39 -070038
39OS=$(uname -s | tr '[:upper:]' '[:lower:]')
40if [ "$OS" = "darwin" ]; then
41 OS=mac
42fi
43
44ARCH=$(uname -m | tr '[:upper:]' '[:lower:]')
45if [ "$ARCH" = "x86_64" ]; then
46 ARCH="amd64"
47fi
48
Chad Norvellf1511cd2022-03-15 20:46:58 -070049# Support `mac-arm64` through Rosetta until `mac-arm64` binaries are ready
50if [[ "$OS" = "mac" ] && [ "$ARCH" = "arm64" ]]; then
51 ARCH="amd64"
52fi
53
Rob Mohr19087152020-05-08 10:06:39 -070054for 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
83done
84
85exit -1