1#!/bin/sh
2set -e
3#  Code generated by godownloader. DO NOT EDIT.
4#
5
6usage() {
7  this=$1
8  cat <<EOF
9$this: download go binaries for client9/misspell
10
11Usage: $this [-b] bindir [tag]
12  -b sets bindir or installation directory, Defaults to ./bin
13   [tag] is a tag from
14   https://github.com/client9/misspell/releases
15   If tag is missing, then an attempt to find the latest will be found.
16
17   Consider setting GITHUB_TOKEN to avoid triggering GitHub rate limits.
18   See the following for more details:
19   https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
20
21 Generated by godownloader
22  https://github.com/goreleaser/godownloader
23
24EOF
25  exit 2
26}
27
28parse_args() {
29  #BINDIR is ./bin unless set be ENV
30  # over-ridden by flag below
31
32  BINDIR=${BINDIR:-./bin}
33  while getopts "b:h?" arg; do
34    case "$arg" in
35      b) BINDIR="$OPTARG" ;;
36      h | \?) usage "$0" ;;
37    esac
38  done
39  shift $((OPTIND - 1))
40  TAG=$1
41}
42# this function wraps all the destructive operations
43# if a curl|bash cuts off the end of the script due to
44# network, either nothing will happen or will syntax error
45# out preventing half-done work
46execute() {
47  TMPDIR=$(mktmpdir)
48  log_debug "downloading tarball ${TARBALL_URL}"
49  http_download "${TMPDIR}/${TARBALL}" "${TARBALL_URL}"
50  log_debug "downloading checksum ${CHECKSUM_URL}"
51  http_download "${TMPDIR}/${CHECKSUM}" "${CHECKSUM_URL}"
52  hash_sha256_verify "${TMPDIR}/${TARBALL}" "${TMPDIR}/${CHECKSUM}"
53
54  (cd "${TMPDIR}" && untar "${TARBALL}")
55  install -d "${BINDIR}"
56  install "${TMPDIR}/${BINARY}" "${BINDIR}/"
57  log_info "installed as ${BINDIR}/${BINARY}"
58}
59is_supported_platform() {
60  platform=$1
61  found=1
62  case "$platform" in
63    darwin/amd64) found=0 ;;
64    linux/amd64) found=0 ;;
65    windows/amd64) found=0 ;;
66  esac
67  case "$platform" in
68    darwin/386) found=1 ;;
69    windows/386) found=1 ;;
70  esac
71  return $found
72}
73check_platform() {
74  if is_supported_platform "$PLATFORM"; then
75    # optional logging goes here
76    true
77  else
78    log_crit "platform $PLATFORM is not supported.  Make sure this script is up-to-date and file request at https://github.com/${PREFIX}/issues/new"
79    exit 1
80  fi
81}
82tag_to_version() {
83  if [ -z "${TAG}" ]; then
84    log_info "checking GitHub for latest tag"
85    TAG=$(github_last_release "$OWNER/$REPO")
86  fi
87  # if version starts with 'v', remove it
88  VERSION=${TAG#v}
89}
90adjust_format() {
91  # change format (tar.gz or zip) based on ARCH
92  true
93}
94adjust_os() {
95  # adjust archive name based on OS
96  case ${OS} in
97    386) OS=32bit ;;
98    amd64) OS=64bit ;;
99    darwin) OS=mac ;;
100  esac
101  true
102}
103adjust_arch() {
104  # adjust archive name based on ARCH
105  case ${ARCH} in
106    386) ARCH=32bit ;;
107    amd64) ARCH=64bit ;;
108    darwin) ARCH=mac ;;
109  esac
110  true
111}
112
113cat /dev/null <<EOF
114------------------------------------------------------------------------
115https://github.com/client9/shlib - portable posix shell functions
116Public domain - http://unlicense.org
117https://github.com/client9/shlib/blob/master/LICENSE.md
118but credit (and pull requests) appreciated.
119------------------------------------------------------------------------
120EOF
121is_command() {
122  command -v "$1" >/dev/null
123}
124echoerr() {
125  echo "$@" 1>&2
126}
127log_prefix() {
128  echo "$0"
129}
130_logp=6
131log_set_priority() {
132  _logp="$1"
133}
134log_priority() {
135  if test -z "$1"; then
136    echo "$_logp"
137    return
138  fi
139  [ "$1" -ge "$_logp" ]
140}
141log_debug() {
142  log_priority 7 && echoerr "$(log_prefix)" "DEBUG" "$@"
143}
144log_info() {
145  log_priority 6 && echoerr "$(log_prefix)" "INFO" "$@"
146}
147log_err() {
148  log_priority 3 && echoerr "$(log_prefix)" "ERR" "$@"
149}
150log_crit() {
151  log_priority 2 && echoerr "$(log_prefix)" "CRIT" "$@"
152}
153uname_os() {
154  os=$(uname -s | tr '[:upper:]' '[:lower:]')
155  case "$os" in
156    msys_nt) os="windows" ;;
157  esac
158  echo "$os"
159}
160uname_arch() {
161  arch=$(uname -m)
162  case $arch in
163    x86_64) arch="amd64" ;;
164    x86) arch="386" ;;
165    i686) arch="386" ;;
166    i386) arch="386" ;;
167    aarch64) arch="arm64" ;;
168    armv5*) arch="arm5" ;;
169    armv6*) arch="arm6" ;;
170    armv7*) arch="arm7" ;;
171  esac
172  echo ${arch}
173}
174uname_os_check() {
175  os=$(uname_os)
176  case "$os" in
177    darwin) return 0 ;;
178    dragonfly) return 0 ;;
179    freebsd) return 0 ;;
180    linux) return 0 ;;
181    android) return 0 ;;
182    nacl) return 0 ;;
183    netbsd) return 0 ;;
184    openbsd) return 0 ;;
185    plan9) return 0 ;;
186    solaris) return 0 ;;
187    windows) return 0 ;;
188  esac
189  log_crit "uname_os_check '$(uname -s)' got converted to '$os' which is not a GOOS value. Please file bug at https://github.com/client9/shlib"
190  return 1
191}
192uname_arch_check() {
193  arch=$(uname_arch)
194  case "$arch" in
195    386) return 0 ;;
196    amd64) return 0 ;;
197    arm64) return 0 ;;
198    armv5) return 0 ;;
199    armv6) return 0 ;;
200    armv7) return 0 ;;
201    ppc64) return 0 ;;
202    ppc64le) return 0 ;;
203    mips) return 0 ;;
204    mipsle) return 0 ;;
205    mips64) return 0 ;;
206    mips64le) return 0 ;;
207    s390x) return 0 ;;
208    amd64p32) return 0 ;;
209  esac
210  log_crit "uname_arch_check '$(uname -m)' got converted to '$arch' which is not a GOARCH value.  Please file bug report at https://github.com/client9/shlib"
211  return 1
212}
213untar() {
214  tarball=$1
215  case "${tarball}" in
216    *.tar.gz | *.tgz) tar -xzf "${tarball}" ;;
217    *.tar) tar -xf "${tarball}" ;;
218    *.zip) unzip "${tarball}" ;;
219    *)
220      log_err "untar unknown archive format for ${tarball}"
221      return 1
222      ;;
223  esac
224}
225mktmpdir() {
226  test -z "$TMPDIR" && TMPDIR="$(mktemp -d)"
227  mkdir -p "${TMPDIR}"
228  echo "${TMPDIR}"
229}
230http_download() {
231  local_file=$1
232  source_url=$2
233  header=$3
234  headerflag=''
235  destflag=''
236  if is_command curl; then
237    cmd='curl --fail -sSL'
238    destflag='-o'
239    headerflag='-H'
240  elif is_command wget; then
241    cmd='wget -q'
242    destflag='-O'
243    headerflag='--header'
244  else
245    log_crit "http_download unable to find wget or curl"
246    return 1
247  fi
248  if [ -z "$header" ]; then
249    $cmd $destflag "$local_file" "$source_url"
250  else
251    $cmd $headerflag "$header" $destflag "$local_file" "$source_url"
252  fi
253}
254github_api() {
255  local_file=$1
256  source_url=$2
257  header=""
258  case "$source_url" in
259    https://api.github.com*)
260      test -z "$GITHUB_TOKEN" || header="Authorization: token $GITHUB_TOKEN"
261      ;;
262  esac
263  http_download "$local_file" "$source_url" "$header"
264}
265github_last_release() {
266  owner_repo=$1
267  version=$2
268  test -z "$version" && version="latest"
269  giturl="https://github.com/${owner_repo}/releases/${version}"
270  json=$(http_download "-" "$giturl" "Accept:application/json")
271  version=$(echo "$json" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//')
272  test -z "$version" && return 1
273  echo "$version"
274}
275hash_sha256() {
276  TARGET=${1:-/dev/stdin}
277  if is_command gsha256sum; then
278    hash=$(gsha256sum "$TARGET") || return 1
279    echo "$hash" | cut -d ' ' -f 1
280  elif is_command sha256sum; then
281    hash=$(sha256sum "$TARGET") || return 1
282    echo "$hash" | cut -d ' ' -f 1
283  elif is_command shasum; then
284    hash=$(shasum -a 256 "$TARGET" 2>/dev/null) || return 1
285    echo "$hash" | cut -d ' ' -f 1
286  elif is_command openssl; then
287    hash=$(openssl -dst openssl dgst -sha256 "$TARGET") || return 1
288    echo "$hash" | cut -d ' ' -f a
289  else
290    log_crit "hash_sha256 unable to find command to compute sha-256 hash"
291    return 1
292  fi
293}
294hash_sha256_verify() {
295  TARGET=$1
296  checksums=$2
297  if [ -z "$checksums" ]; then
298    log_err "hash_sha256_verify checksum file not specified in arg2"
299    return 1
300  fi
301  BASENAME=${TARGET##*/}
302  want=$(grep "${BASENAME}" "${checksums}" 2>/dev/null | tr '\t' ' ' | cut -d ' ' -f 1)
303  if [ -z "$want" ]; then
304    log_err "hash_sha256_verify unable to find checksum for '${TARGET}' in '${checksums}'"
305    return 1
306  fi
307  got=$(hash_sha256 "$TARGET")
308  if [ "$want" != "$got" ]; then
309    log_err "hash_sha256_verify checksum for '$TARGET' did not verify ${want} vs $got"
310    return 1
311  fi
312}
313cat /dev/null <<EOF
314------------------------------------------------------------------------
315End of functions from https://github.com/client9/shlib
316------------------------------------------------------------------------
317EOF
318
319OWNER=client9
320REPO="misspell"
321BINARY=misspell
322FORMAT=tar.gz
323OS=$(uname_os)
324ARCH=$(uname_arch)
325PREFIX="$OWNER/$REPO"
326
327# use in logging routines
328log_prefix() {
329	echo "$PREFIX"
330}
331PLATFORM="${OS}/${ARCH}"
332GITHUB_DOWNLOAD=https://github.com/${OWNER}/${REPO}/releases/download
333
334uname_os_check "$OS"
335uname_arch_check "$ARCH"
336
337parse_args "$@"
338
339check_platform
340
341tag_to_version
342
343adjust_format
344
345adjust_os
346
347adjust_arch
348
349log_info "found version: ${VERSION} for ${TAG}/${OS}/${ARCH}"
350
351NAME=${BINARY}_${VERSION}_${OS}_${ARCH}
352TARBALL=${NAME}.${FORMAT}
353TARBALL_URL=${GITHUB_DOWNLOAD}/${TAG}/${TARBALL}
354CHECKSUM=${BINARY}_${VERSION}_checksums.txt
355CHECKSUM_URL=${GITHUB_DOWNLOAD}/${TAG}/${CHECKSUM}
356
357# Adjust binary name if windows
358if [ "$OS" = "windows" ]; then
359  BINARY="${BINARY}.exe"
360fi
361
362execute
363