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 fetch_mtime=$(stat -c %Y FETCH_HEAD)
55 if [[ ! $fetch_mtime ]]; then
56 err failed to get output from stat -c %Y FETCH_HEAD
57 exit 1
58 fi
59 # no need to check for updates on go more than once a day
60 if (( fetch_mtime > yesterday )); then
61 up=false
62 fi
63
64 # update will get overwritten if we dont do it on central host.
65 if [[ -e /a/bin/bash_unpublished/source-state ]]; then
66 source /a/bin/bash_unpublished/source-state
67 if [[ $HOSTNAME != "$HOST2" ]]; then
68 up=false
69 fi
70 fi
71 fi
72
73 shopt -s nullglob
74
75 f=$(
76 for f in go*.tar.gz; do
77 echo $f
78 done | sort -Vr | head -n1
79 )
80 if [[ ! $f ]]; then
81 up=true
82 fi
83
84 if $up; then
85 m git fetch --tags
86 mkdir -p ~/.local
87 touch ~/.local/gofetch
88
89 vers=($(git tag | sort -Vr | grep -E '^go[0-9.]+$' | head))
90
91 ver=${vers[0]}
92 dl() {
93 arch=$(dpkg --print-architecture)
94 f=${ver}.linux-${arch}.tar.gz
95 if [[ -e $f ]]; then
96 timestamp=$(stat -c %Y $f)
97 else
98 timestamp=0
99 fi
100 m wget -nv -N https://dl.google.com/go/$f || ret=$?
101 }
102 dl
103 # 8 is return code for 404. for a new release, they might not have a binary out yet
104 if [[ $ret == 8 ]]; then
105 ver=${vers[1]}
106 dl
107 fi
108
109 new_timestamp=$(stat -c %Y $f)
110 if [[ $timestamp != $new_timestamp ]]; then
111 install=true
112 fi
113 fi
114
115 if $install || [[ ! -e /usr/local/go/bin/go ]]; then
116 m sudo rm -rf /usr/local/go
117 m sudo tar -C /usr/local -xzf $f
118 fi
119
120 for x in go*.tar.gz; do
121 if [[ $x == $f ]]; then continue; fi
122 rm -fv $x
123 done