aboutsummaryrefslogtreecommitdiff
path: root/gitstatus/install
blob: 42428f7430154c6ac42e127b590b89763a92a70b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/bin/sh
#
# This script does not have a stable API.

_gitstatus_install_main() {
  if [ -n "${ZSH_VERSION:-}" ]; then
    emulate -L sh -o no_unset
  else
    set -u
  fi

  local argv1="$1"
  shift

  local no_check= no_install= uname_s= uname_m= gitstatus_dir= dl_status=
  local opt= OPTARG= OPTIND=1

  while getopts ':s:m:d:p:fnh' opt "$@"; do
    case "$opt" in
      h)
        command cat <<\END
Usage: install [-s KERNEL] [-m ARCH] [-d DIR] [-p CMD] [-f|-n] [-- CMD [ARG]...]

If positional arguments are specified, call this on success:

  CMD [ARG]... DAEMON VERSION INSTALLED

DAEMON is path to gitstatusd. VERSION is a glob pattern for the
version this daemon should support; it's supposed to be passed as
-G to gitstatusd. INSTALLED is 1 if gitstatusd has just been
downloaded and 0 otherwise.

Options:

  -s KERNEL  use this instead of lowercase `uname -s`
  -m ARCH    use this instead of lowercase `uname -m`
  -d DIR     use this instead of `dirname "$0"`
  -p CMD     eval this every second while downloading gitstatusd
  -f         download gitstatusd even if there is one locally
  -n         do not download gitstatusd (fail instead)
END
        return
      ;;
      n)
        if [ -n "$no_install" ]; then
          >&2 echo "[gitstatus] error: duplicate option: -$opt"
          return 1
        fi
        no_install=1
      ;;
      f)
        if [ -n "$no_check" ]; then
          >&2 echo "[gitstatus] error: duplicate option: -$opt"
          return 1
        fi
        no_check=1
      ;;
      d)
        if [ -n "$gitstatus_dir" ]; then
          >&2 echo "[gitstatus] error: duplicate option: -$opt"
          return 1
        fi
        if [ -z "$OPTARG" ]; then
          >&2 echo "[error] incorrect value of -$opt: $OPTARG"
          return 1
        fi
        gitstatus_dir="$OPTARG"
      ;;
      p)
        if [ -n "$dl_status" ]; then
          >&2 echo "[gitstatus] error: duplicate option: -$opt"
          return 1
        fi
        if [ -z "$OPTARG" ]; then
          >&2 echo "[error] incorrect value of -$opt: $OPTARG"
          return 1
        fi
        dl_status="$OPTARG"
      ;;
      m)
        if [ -n "$uname_m" ]; then
          >&2 echo "[gitstatus] error: duplicate option: -$opt"
          return 1
        fi
        if [ -z "$OPTARG" ]; then
          >&2 echo "[error] incorrect value of -$opt: $OPTARG"
          return 1
        fi
        uname_m="$OPTARG"
      ;;
      s)
        if [ -n "$uname_s" ]; then
          >&2 echo "[gitstatus] error: duplicate option: -$opt"
          return 1
        fi
        if [ -z "$OPTARG" ]; then
          >&2 echo "[error] incorrect value of -$opt: $OPTARG"
          return 1
        fi
        uname_s="$OPTARG"
      ;;
      \?) >&2 echo "[gitstatus] error: invalid option: -$OPTARG"           ; return 1;;
      :)  >&2 echo "[gitstatus] error: missing required argument: -$OPTARG"; return 1;;
      *)  >&2 echo "[gitstatus] internal error: unhandled option: -$opt"   ; return 1;;
    esac
  done

  shift "$((OPTIND - 1))"

  : "${gitstatus_dir:=$argv1}"

  if [ -n "$no_check" -a -n "$no_install" ]; then
    >&2 echo "[gitstatus] error: incompatible options: -f, -n"
    return 1
  fi

  if [ -z "$uname_s" ]; then
    uname_s="$(command uname -s)" || return
    uname_s="$(printf '%s' "$uname_s" | command tr '[A-Z]' '[a-z]')" || return
  fi
  if [ -z "$uname_m" ]; then
    uname_m="$(command uname -m)" || return
    uname_m="$(printf '%s' "$uname_m" | command tr '[A-Z]' '[a-z]')" || return
  fi

  local daemon="${GITSTATUS_DAEMON:-}"
  local cache_dir="${GITSTATUS_CACHE_DIR:-${XDG_CACHE_HOME:-$HOME/.cache}/gitstatus}"

  if [ -z "$no_check" ]; then
    if [ -n "${daemon##/*}" ]; then
      >&2 echo "[gitstatus] error: GITSTATUS_DAEMON is not absolute path: $daemon"
      return 1
    fi
    if [ -z "$daemon" ]; then
      daemon="$gitstatus_dir"/usrbin/gitstatusd
      if [ ! -e "$daemon" ]; then
        daemon="$daemon"-"$uname_s"-"$uname_m"
        if [ ! -e "$daemon" ]; then
          daemon=
        fi
      fi
    fi
    if [ -n "$daemon" ]; then
      local gitstatus_version= libgit2_version=
      if ! . "$gitstatus_dir"/build.info; then
        >&2 echo "[gitstatus] internal error: failed to source build.info"
        return 1
      fi
      if [ -z "$gitstatus_version" ]; then
        >&2 echo "[gitstatus] internal error: empty gitstatus_version in build.info"
        return 1
      fi
      [ $# = 0 ] || "$@" "$daemon" "$gitstatus_version" 0
      return
    fi
  fi

  while IFS= read -r line; do
    line="${line###*}"
    [ -n "$line" ] || continue

    local uname_s_glob= uname_m_glob= file= version= sha256=
    eval "$line" || return

    if [ -z "$uname_s_glob" -o \
         -z "$uname_m_glob" -o \
         -z "$file"         -o \
         -z "$version"      -o \
         -z "$sha256" ]; then
      >&2 echo "[gitstatus] internal error: invalid install.info line: $line"
      return 1
    fi

    case "$uname_s" in
      $uname_s_glob) ;;
      *) continue;;
    esac
    case "$uname_m" in
      $uname_m_glob) ;;
      *) continue;;
    esac

    # Found a match. The while loop will terminate during this iteration.

    if [ -z "$no_check" ]; then
      # Check if a suitable gitstatusd already exists.
      local daemon="$cache_dir"/"$file"
      if [ -e "$daemon" ]; then
        [ $# = 0 ] || "$@" "$daemon" "$version" 0
        return
      fi
      daemon="$daemon"-"$uname_s"-"$uname_m"
      if [ -e "$daemon" ]; then
        local gitstatus_version= libgit2_version=
        if ! . "$gitstatus_dir"/build.info; then
          >&2 echo "[gitstatus] internal error: failed to source build.info"
          return 1
        fi
        if [ -z "$gitstatus_version" ]; then
          >&2 echo "[gitstatus] internal error: empty gitstatus_version in build.info"
          return 1
        fi
        [ $# = 0 ] || "$@" "$daemon" "$gitstatus_version" 0
        return
      fi
    fi

    # No suitable gitstatusd exists. Need to download.

    if [ -n "$no_install" ]; then
      >&2 echo "[gitstatus] error: no gitstatusd found and installation is disabled"
      return 1
    fi

    local daemon="$cache_dir"/"$file"

    if [ -n "${cache_dir##/*}" ]; then
      >&2 echo "[gitstatus] error: GITSTATUS_CACHE_DIR is not absolute: $cache_dir"
      return 1
    fi
    [ -d "$cache_dir" ] || mkdir -p -- "$cache_dir" || return

    local tmpdir
    if ! command -v mktemp >/dev/null 2>&1 ||
        ! tmpdir="$(command mktemp -d "${TMPDIR:-/tmp}"/gitstatus-install.XXXXXXXXXX)"; then
      tmpdir="${TMPDIR:-/tmp}/gitstatus-install.tmp.$$"
      mkdir -p -- "$tmpdir" || return
    fi

    if ! command -v curl >/dev/null 2>&1 && ! command -v wget >/dev/null 2>&1; then
      >&2 echo "[gitstatus] error: please install curl or wget"
      return 1
    fi

    (
      run_cmd() {
        command -v "$1" >/dev/null 2>/dev/null || return 127
        local trapped= pid die ret
        trap 'trapped=1' $sig
        # The only reason for suppressing stderr is that `curl -f` cannot be silenced:
        # `-s` doesn't work despite what the docs say.
        command "$@" 2>/dev/null &
        ret="$?"
        if [ "$ret" = 0 ]; then
          pid="$!"
          die="trap - $sig; kill -- $pid 2>/dev/null; wait -- $pid 2>/dev/null; exit 1"
          trap "$die" $sig
          [ -z "$trapped" ] || eval "$die"
          wait -- "$pid" 2>/dev/null
          ret="$?"
        fi
        trap - $sig
        [ -z "$trapped" ] || exit
        return "$ret"
      }

      check_sha256() {
        local data_file="$tmpdir"/"$1".tar.gz
        local hash_file="$tmpdir"/"$1".tar.gz.sha256
        local hash=
        if command -v shasum >/dev/null 2>/dev/null; then
          if run_cmd shasum -b -a 256 -- "$data_file" >"$hash_file"; then
            IFS= read -r hash <"$hash_file" || hash=
            hash="${hash%% *}"
          fi
        elif command -v sha256sum >/dev/null 2>/dev/null; then
          if run_cmd sha256sum -b -- "$data_file" >"$hash_file"; then
            IFS= read -r hash <"$hash_file" || hash=
            hash="${hash%% *}"
          fi
        elif command -v sha256 >/dev/null 2>/dev/null; then
          if run_cmd sha256 -- "$data_file" </dev/null >"$hash_file"; then
            IFS= read -r hash <"$hash_file" || hash=
            # Ignore sha256 output if it's from hashalot. It's incompatible.
            if [ ${#hash} -lt 64 ]; then
              hash=
            else
              hash="${hash##* }"
            fi
          fi
        fi
        [ "$1" = 1 -a -z "$hash" -o "$hash" = "$sha256" ]
      }

      local url1="https://github.com/romkatv/gitstatus/releases/download/$version/$file.tar.gz"
      local url2="https://gitee.com/romkatv/gitstatus/raw/release-$version/release/$file.tar.gz"
      local sig='INT QUIT TERM ILL PIPE'

      fetch() {
        if [ "$1" != 1 ] && command -v sleep >/dev/null 2>/dev/null; then
          if ! run_cmd sleep "$1"; then
            echo -n >"$tmpdir"/"$1".status
            return 1
          fi
        fi
        local cmd part url ret
        for cmd in 'curl -q -fsSL' 'wget --no-config -qO-'; do
          part=0
          while true; do
            if [ "$part" = 2 ]; then
              ret=1
              break
            elif [ "$part" = 0 ]; then
              url="$2"
            else
              url="$2"."$part"
            fi
            run_cmd $cmd -- "$url" >>"$tmpdir"/"$1".tar.gz
            ret="$?"
            [ "$ret" = 0 ] || break
            check_sha256 "$1" && break
            part=$((part+1))
          done
          [ "$ret" = 0 ] && break
          run_cmd rm -f -- "$tmpdir"/"$1".tar.gz && continue
          ret="$?"
          break
        done
        echo -n >"$tmpdir"/"$1".status
        return "$ret"
      }

      local trapped=
      trap 'trapped=1' $sig
      fetch 1 "$url1" & 
      local pid1="$!"
      fetch 2 "$url2" & 
      local pid2="$!"

      local die="trap - $sig; kill -- $pid1 $pid2 2>/dev/null; wait -- $pid1 $pid2 2>/dev/null; exit 1"
      trap "$die" $sig
      [ -z "$trapped" ] || eval "$die"

      local n=
      while true; do
        [ -z "$dl_status" ] || eval "$dl_status" || eval "$die"
        if command -v sleep >/dev/null 2>/dev/null; then
          command sleep 1
        elif command -v true >/dev/null 2>/dev/null; then
          command true
        fi
        if [ -n "$pid1" -a -e "$tmpdir"/1.status ]; then
          wait -- "$pid1" 2>/dev/null
          local ret="$?"
          pid1=
          if [ "$ret" = 0 ]; then
            if [ -n "$pid2" ]; then
              kill -- "$pid2" 2>/dev/null
              wait -- "$pid2" 2>/dev/null
            fi
            n=1
            break
          elif [ -z "$pid2" ]; then
            break
          else
            die="trap - $sig; kill -- $pid2 2>/dev/null; wait -- $pid2 2>/dev/null; exit 1"
            trap "$die" $sig
          fi
        elif [ -n "$pid2" -a -e "$tmpdir"/2.status ]; then
          wait -- "$pid2" 2>/dev/null
          local ret="$?"
          pid2=
          if [ "$ret" = 0 ]; then
            if [ -n "$pid1" ]; then
              kill -- "$pid1" 2>/dev/null
              wait -- "$pid1" 2>/dev/null
            fi
            n=2
            break
          elif [ -z "$pid1" ]; then
            break
          else
            die="trap - $sig; kill -- $pid1 2>/dev/null; wait -- $pid1 2>/dev/null; exit 1"
            trap "$die" $sig
          fi
        fi
      done

      trap - $sig

      if [ -z "$n" ]; then
        >&2 echo "[gitstatus] error: failed to download gitstatusd from any mirror"
        >&2 echo ""
        >&2 echo "  1. $url1"
        >&2 echo "  2. $url2"
        exit 1
      fi

      command tar -C "$tmpdir" -xzf "$tmpdir"/"$n".tar.gz || exit

      local tmpfile
      if ! command -v mktemp >/dev/null 2>&1 ||
         ! tmpfile="$(command mktemp "$cache_dir"/gitstatusd.XXXXXXXXXX)"; then
        tmpfile="$cache_dir"/gitstatusd.tmp.$$
      fi

      command mv -f -- "$tmpdir"/gitstatusd-* "$tmpfile" || exit
      command mv -f -- "$tmpfile" "$cache_dir"/"$file" && exit
      command rm -f -- "$cache_dir"/"$file"
      command mv -f -- "$tmpfile" "$cache_dir"/"$file" && exit
      command rm -f -- "$tmpfile"
      exit 1
    )

    local ret="$?"
    command rm -rf -- "$tmpdir"
    [ "$ret" = 0 ] || return

    [ $# = 0 ] || "$@" "$daemon" "$version" 1
    return
  done <"$gitstatus_dir"/install.info

  >&2 echo "[gitstatus] error: no gitstatusd found for $uname_s $uname_m"
  >&2 echo ""
  >&2 echo "See: https://github.com/romkatv/gitstatus/blob/master/README.md#compiling"
  return 1
}

if [ -z "${0##*/*}" ]; then
  _gitstatus_install_main "${0%/*}" "$@"
else
  _gitstatus_install_main . "$@"
fi