d948b9d0063c95c53c1cad4128a5f64abc8e8c5a
[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 force=false
13 case $1 in
14 -f) force=true ;;
15 esac
16
17 m() { printf "$pre %s\n" "$*"; "$@"; }
18 e() { printf "$pre %s\n" "$*"; }
19 err() { echo "[$(date +'%Y-%m-%d %H:%M:%S%z')]: $0: $*" >&2; }
20
21
22
23 m cd /a/opt/go.git
24
25 up=true
26 install=false
27
28 if ! $force; then
29 yesterday=$(( $(date +%s) - 60*60*24 ))
30 # no need to check for updates on go more than once a day
31 if (( $(stat -c %Y FETCH_HEAD) > yesterday )); then
32 up=false
33 fi
34
35 # update will get overwritten if we dont do it on central host.
36 if [[ -e /a/bin/bash_unpublished/source-state ]]; then
37 source /a/bin/bash_unpublished/source-state
38 if [[ $HOSTNAME != "$HOST2" ]]; then
39 up=false
40 fi
41 fi
42 fi
43
44 shopt -s nullglob
45
46 f=$(
47 for f in go*.tar.gz; do
48 echo $f
49 done | sort -Vr | head -n1
50 )
51 if [[ ! $f ]]; then
52 up=true
53 fi
54
55 if $up; then
56 m git fetch --tags
57 touch ~/.local/gofetch
58
59 vers=($(git tag | sort -Vr | grep -E '^go[0-9.]+$' | head))
60
61 ver=${vers[0]}
62 dl() {
63 arch=$(dpkg --print-architecture)
64 f=${ver}.linux-${arch}.tar.gz
65 if [[ -e $f ]]; then
66 timestamp=$(stat -c %Y $f)
67 else
68 timestamp=0
69 fi
70 m wget -nv -N https://dl.google.com/go/$f || ret=$?
71 }
72 dl
73 # 8 is return code for 404. for a new release, they might not have a binary out yet
74 if [[ $ret == 8 ]]; then
75 ver=${vers[1]}
76 dl
77 fi
78
79 new_timestamp=$(stat -c %Y $f)
80 if [[ $timestamp != $new_timestamp ]]; then
81 install=true
82 fi
83 fi
84
85 if $install || [[ ! -e /usr/local/go/bin/go ]]; then
86 m sudo rm -rf /usr/local/go
87 m sudo tar -C /usr/local -xzf $f
88 fi
89
90 for x in go*.tar.gz; do
91 if [[ $x == $f ]]; then continue; fi
92 rm -fv $x
93 done