overdue commit lots of changes
[buildscripts] / go
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23
24 # usage: no args, run to install latest go.
25
26 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
27
28 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
29 source /usr/local/lib/bash-bear
30
31 force=false
32 case $1 in
33 -f) force=true ;;
34 esac
35
36 m() { printf "$pre %s\n" "$*"; "$@"; }
37 e() { printf "$pre %s\n" "$*"; }
38 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $0: $*" >&2; }
39
40
41 if [[ ! -d /a/opt/go.git ]]; then
42 mkdir -p /a/opt
43 cd /a/opt
44 git clone --bare https://go.googlesource.com/go
45 fi
46
47 m cd /a/opt/go.git
48
49 up=true
50 install=false
51
52 if ! $force; then
53 yesterday=$(( $(date +%s) - 60*60*24 ))
54 # no need to check for updates on go more than once a day
55 if (( $(stat -c %Y FETCH_HEAD) > yesterday )); then
56 up=false
57 fi
58
59 # update will get overwritten if we dont do it on central host.
60 if [[ -e /a/bin/bash_unpublished/source-state ]]; then
61 source /a/bin/bash_unpublished/source-state
62 if [[ $HOSTNAME != "$HOST2" ]]; then
63 up=false
64 fi
65 fi
66 fi
67
68 shopt -s nullglob
69
70 f=$(
71 for f in go*.tar.gz; do
72 echo $f
73 done | sort -Vr | head -n1
74 )
75 if [[ ! $f ]]; then
76 up=true
77 fi
78
79 if $up; then
80 m git fetch --tags
81 mkdir -p ~/.local
82 touch ~/.local/gofetch
83
84 vers=($(git tag | sort -Vr | grep -E '^go[0-9.]+$' | head))
85
86 ver=${vers[0]}
87 dl() {
88 arch=$(dpkg --print-architecture)
89 f=${ver}.linux-${arch}.tar.gz
90 if [[ -e $f ]]; then
91 timestamp=$(stat -c %Y $f)
92 else
93 timestamp=0
94 fi
95 m wget -nv -N https://dl.google.com/go/$f || ret=$?
96 }
97 dl
98 # 8 is return code for 404. for a new release, they might not have a binary out yet
99 if [[ $ret == 8 ]]; then
100 ver=${vers[1]}
101 dl
102 fi
103
104 new_timestamp=$(stat -c %Y $f)
105 if [[ $timestamp != $new_timestamp ]]; then
106 install=true
107 fi
108 fi
109
110 if $install || [[ ! -e /usr/local/go/bin/go ]]; then
111 m sudo rm -rf /usr/local/go
112 m sudo tar -C /usr/local -xzf $f
113 fi
114
115 for x in go*.tar.gz; do
116 if [[ $x == $f ]]; then continue; fi
117 rm -fv $x
118 done