github.com/zaquestion/lab@v0.25.1/install.sh (about)

     1  #!/bin/sh
     2  # Usage: [sudo] [BINDIR=/usr/local/bin] ./install.sh [<BINDIR>]
     3  #
     4  # Example:
     5  #     1. sudo ./install.sh /usr/local/bin
     6  #     2. sudo ./install.sh /usr/bin
     7  #     3. ./install.sh $HOME/usr/bin
     8  #     4. BINDIR=$HOME/usr/bin ./install.sh
     9  #
    10  # Default BINDIR=/usr/bin
    11  
    12  set -euf
    13  
    14  if [ -n "${DEBUG-}" ]; then
    15      set -x
    16  fi
    17  
    18  : "${BINDIR:=/usr/bin}"
    19  
    20  if [ $# -gt 0 ]; then
    21    BINDIR=$1
    22  fi
    23  
    24  _can_install() {
    25    if [ ! -d "${BINDIR}" ]; then
    26      mkdir -p "${BINDIR}" 2> /dev/null
    27    fi
    28    [ -d "${BINDIR}" ] && [ -w "${BINDIR}" ]
    29  }
    30  
    31  if ! _can_install && [ "$(id -u)" != 0 ]; then
    32    printf "Run script as sudo\n"
    33    exit 1
    34  fi
    35  
    36  if ! _can_install; then
    37    printf -- "Can't install to %s\n" "${BINDIR}"
    38    exit 1
    39  fi
    40  
    41  case "$(uname -m)" in
    42      x86_64)
    43          machine="amd64"
    44          ;;
    45      i386)
    46          machine="386"
    47          ;;
    48      *)
    49          machine=""
    50          ;;
    51  esac
    52  
    53  case $(uname -s) in
    54      Linux)
    55          os="linux"
    56          ;;
    57      Darwin)
    58          os="darwin"
    59          ;;
    60      *)
    61          printf "OS not supported\n"
    62          exit 1
    63          ;;
    64  esac
    65  
    66  printf "Fetching latest version\n"
    67  latest="$(curl -sL 'https://api.github.com/repos/zaquestion/lab/releases/latest' | grep 'tag_name' | grep -o 'v[0-9\.]\+' | cut -c 2-)"
    68  tempFolder="/tmp/lab_v${latest}"
    69  
    70  printf -- "Found version %s\n" "${latest}"
    71  
    72  mkdir -p "${tempFolder}" 2> /dev/null
    73  printf -- "Downloading lab_%s_%s_%s.tar.gz\n" "${latest}" "${os}" "${machine}"
    74  curl -sL "https://github.com/zaquestion/lab/releases/download/v${latest}/lab_${latest}_${os}_${machine}.tar.gz" | tar -C "${tempFolder}" -xzf -
    75  
    76  printf -- "Installing...\n"
    77  install -m755 "${tempFolder}/lab" "${BINDIR}/lab"
    78  
    79  printf "Cleaning up temp files\n"
    80  rm -rf "${tempFolder}"
    81  
    82  printf -- "Successfully installed lab in %s/\n" "${BINDIR}"