master
text 177 lines 3.9 KB
Raw
1 #!/bin/sh
2
3 GOCC=${GOCC=go}
4
5 die() {
6 echo "$@" >&2
7 exit 1
8 }
9
10 have_binary() {
11 type "$1" > /dev/null 2> /dev/null
12 }
13
14 check_writable() {
15 printf "" > "$1" && rm "$1"
16 }
17
18 try_download() {
19 url="$1"
20 output="$2"
21 command="$3"
22 util_name="$(set -- $command; echo "$1")"
23
24 if ! have_binary "$util_name"; then
25 return 1
26 fi
27
28 printf '==> Using %s to download "%s" to "%s"\n' "$util_name" "$url" "$output"
29 if eval "$command"; then
30 echo "==> Download complete!"
31 return
32 else
33 echo "error: couldn't download with $util_name ($?)"
34 return 1
35 fi
36 }
37
38 download() {
39 dl_url="$1"
40 dl_output="$2"
41
42 test "$#" -eq "2" || die "download requires exactly two arguments, was given $@"
43
44 if ! check_writable "$dl_output"; then
45 die "download error: cannot write to $dl_output"
46 fi
47
48 try_download "$dl_url" "$dl_output" "wget '$dl_url' -O '$dl_output'" && return
49 try_download "$dl_url" "$dl_output" "curl --silent --fail --output '$dl_output' '$dl_url'" && return
50 try_download "$dl_url" "$dl_output" "fetch '$dl_url' -o '$dl_output'" && return
51 try_download "$dl_url" "$dl_output" "http '$dl_url' > '$dl_output'" && return
52 try_download "$dl_url" "$dl_output" "ftp -o '$dl_output' '$dl_url'" && return
53
54 die "Unable to download $dl_url. exiting."
55 }
56
57 unarchive() {
58 ua_archivetype="$1"
59 ua_infile="$2"
60 ua_outfile="$3"
61 ua_distname="$4"
62 ua_binpostfix=""
63 ua_os=$(uname -o)
64
65 if [ "$ua_os" = "Msys" ] || [ "$ua_os" = "Cygwin" ] ; then
66 ua_binpostfix=".exe"
67 fi
68 ua_outfile="$ua_outfile$ua_binpostfix"
69
70 if ! check_writable "$ua_outfile"; then
71 die "unarchive error: cannot write to $ua_outfile"
72 fi
73
74 case "$ua_archivetype" in
75 tar.gz)
76 if have_binary tar; then
77 echo "==> using 'tar' to extract binary from archive"
78 < "$ua_infile" tar -Ozxf - "$ua_distname/$ua_distname$ua_binpostfix" > "$ua_outfile" \
79 || die "tar has failed"
80 else
81 die "no binary on system for extracting tar files"
82 fi
83 ;;
84 zip)
85 if have_binary unzip; then
86 echo "==> using 'unzip' to extract binary from archive"
87 unzip -p "$ua_infile" "$ua_distname/$ua_distname$ua_binpostfix" > "$ua_outfile" \
88 || die "unzip has failed"
89 else
90 die "no installed method for extracting .zip archives"
91 fi
92 ;;
93 *)
94 die "unrecognized archive type '$ua_archivetype'"
95 esac
96
97 chmod +x "$ua_outfile" || die "chmod has failed"
98 }
99
100 get_go_vars() {
101 if [ ! -z "$GOOS" ] && [ ! -z "$GOARCH" ]; then
102 printf "%s-%s" "$GOOS" "$GOARCH"
103 elif have_binary go; then
104 printf "%s-%s" "$($GOCC env GOOS)" "$($GOCC env GOARCH)"
105 else
106 die "no way of determining system GOOS and GOARCH\nPlease manually set GOOS and GOARCH then retry."
107 fi
108 }
109
110 mkurl() {
111 m_root="$1"
112 m_name="$2"
113 m_vers="$3"
114 m_archive="$4"
115 m_govars=$(get_go_vars) || die "could not get go env vars"
116
117 echo "https://ipfs.io$m_root/$m_name/$m_vers/${m_name}_${m_vers}_$m_govars.$m_archive"
118 }
119
120 distroot="$1"
121 distname="$2"
122 outpath="$3"
123 version="$4"
124
125 if [ -z "$distroot" ] || [ -z "$distname" ] || [ -z "$outpath" ] || [ -z "$version" ]; then
126 die "usage: dist_get <distroot> <distname> <outpath> <version>"
127 fi
128
129 case $version in
130 v*)
131 # correct input
132 ;;
133 *)
134 echo "invalid version '$version'" >&2
135 die "versions must begin with 'v', for example: v0.4.0"
136 ;;
137 esac
138
139 # TODO: don't depend on the go tool being installed to detect this
140 goenv=$(get_go_vars) || die "could not get go env vars"
141
142 case $goenv in
143 linux-*)
144 archive="tar.gz"
145 ;;
146 darwin-*)
147 archive="tar.gz"
148 ;;
149 windows-*)
150 archive="zip"
151 ;;
152 freebsd-*)
153 archive="tar.gz"
154 ;;
155 openbsd-*)
156 archive="tar.gz"
157 ;;
158 *)
159 echo "unrecognized system environment: $goenv" >&2
160 die "currently only linux, darwin, windows and freebsd are supported by this script"
161 esac
162
163
164 mkdir -p bin/tmp
165
166 url=$(mkurl "$distroot" "$distname" "$version" "$archive")
167 tmpfi="bin/tmp/$distname.$archive"
168
169 download "$url" "$tmpfi"
170 if [ $? -ne 0 ]; then
171 die "failed to download $url to $tmpfi"
172 fi
173
174 unarchive "$archive" "$tmpfi" "$outpath" "$distname"
175 if [ $? -ne 0 ]; then
176 die "failed to extract archive $tmpfi"
177 fi