consistent license, various updates
[buildscripts] / go
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4
5 # usage: no args, run to install latest go.
6
7 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
8
9 shopt -s inherit_errexit 2>/dev/null ||: # ignore fail in bash < 4.4
10 source /a/bin/errhandle/err
11
12
13 m() { printf "$pre %s\n" "$*"; "$@"; }
14 e() { printf "$pre %s\n" "$*"; }
15 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $0: $*" >&2; }
16
17 m cd /a/opt/go.git
18 # don't check for updates on go more than once a day
19 yesterday=$(( $(date +%s) - 60*60*24 ))
20 if (( $(stat -c %Y FETCH_HEAD) < yesterday )); then
21 m git fetch
22 touch ~/.local/gofetch
23
24 vers=($(git tag | sort -Vr | grep -E '^go[0-9.]+$' | head))
25
26 ver=${vers[0]}
27 dl() {
28 arch=$(dpkg --print-architecture)
29 f=${ver}.linux-${arch}.tar.gz
30
31 if [[ -e $f ]]; then
32 timestamp=$(stat -c %Y $f)
33 else
34 timestamp=0
35 fi
36 m wget -nv -N https://dl.google.com/go/$f || ret=$?
37 }
38 dl
39 # 8 is return code for 404. for a new release, they might not have a binary out yet
40 if [[ $ret == 8 ]]; then
41 ver=${vers[1]}
42 dl
43 fi
44
45 new_timestamp=$(stat -c %Y $f)
46 if [[ $timestamp != $new_timestamp || ! -e /usr/local/go/bin/go ]]; then
47 m sudo rm -rf /usr/local/go
48 m sudo tar -C /usr/local -xzf $f
49 fi
50
51 shopt -s nullglob
52 for x in go*.tar.gz; do
53 if [[ $x == $f ]]; then continue; fi
54 rm -fv $x
55 done
56
57 fi