github.com/upyun/upx@v0.4.7-0.20240419023638-b184a7cb7c10/install.sh (about)

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