install.sh 2.23 KB
Newer Older
Benoit Lavenier's avatar
Benoit Lavenier committed
1 2 3 4 5 6 7 8 9
#!/bin/bash

{ # this ensures the entire script is downloaded #

is_installed() {
  type "$1" > /dev/null 2>&1
}

if [ "_$1" != "_" ]; then
10
  GCHANGE_DIR="$1"
Benoit Lavenier's avatar
Benoit Lavenier committed
11
fi
12
if [ "_$GCHANGE_DIR" = "_" ]; then
Benoit Lavenier's avatar
Benoit Lavenier committed
13
  DIRNAME=`pwd`
14
  GCHANGE_DIR="$DIRNAME/gchange"
Benoit Lavenier's avatar
Benoit Lavenier committed
15 16 17
fi

latest_version() {
Benoit Lavenier's avatar
Benoit Lavenier committed
18
  echo "v0.6.6" #lastest
Benoit Lavenier's avatar
Benoit Lavenier committed
19 20 21
}

api_release_url() {
22
  echo "https://api.github.com/repos/duniter-gchange/gchange-client/releases/tags/$(latest_version)"
Benoit Lavenier's avatar
Benoit Lavenier committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
}

download() {
  if is_installed "curl"; then
    curl -qkL $*
  elif is_installed "wget"; then
    # Emulate curl with wget
    ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \
                           -e 's/-L //' \
                           -e 's/-I /--server-response /' \
                           -e 's/-s /-q /' \
                           -e 's/-o /-O /' \
                           -e 's/-C - /-c /')
    wget $ARGS
  fi
}

install_from_github() {

  local RELEASE=`curl -XGET -i $(api_release_url)`
43
  local GCHANGE_URL=`echo "$RELEASE" | grep -P "\"browser_download_url\": \"[^\"]+" | grep -oP "https://[a-zA-Z0-9/.-]+-web.zip"`
44
  local GCHANGE_ARCHIVE=$GCHANGE_DIR/gchange.zip
45 46 47 48
  if [ -d "$GCHANGE_DIR" ]; then
    if [ -f "$GCHANGE_ARCHIVE" ]; then
      echo "WARNING: Deleting existing archive [$GCHANGE_ARCHIVE]"
      rm $GCHANGE_ARCHIVE
Benoit Lavenier's avatar
Benoit Lavenier committed
49 50
    fi
  else
51
    mkdir -p "$GCHANGE_DIR"
Benoit Lavenier's avatar
Benoit Lavenier committed
52 53
  fi

54 55 56
  echo "Downloading [$GCHANGE_URL]"
  download "$GCHANGE_URL" -o "$GCHANGE_ARCHIVE" || {
      echo >&2 "Failed to download '$GCHANGE_URL'"
Benoit Lavenier's avatar
Benoit Lavenier committed
57 58
      return 4
    }
59 60 61
  echo "Unarchive to $GCHANGE_DIR"
  unzip -o $GCHANGE_ARCHIVE -d $GCHANGE_DIR
  rm $GCHANGE_ARCHIVE
Benoit Lavenier's avatar
Benoit Lavenier committed
62 63 64

  echo

65
  echo "ğchange successfully installed at $GCHANGE_DIR"
Benoit Lavenier's avatar
Benoit Lavenier committed
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
}

do_install() {

  if ! is_installed "curl"; then
    echo "=> curl is not available. You will likely need to install 'curl' package."
    exit 1
  fi
  if ! is_installed "unzip"; then
    echo "=> unzip is not available. You will likely need to install 'unzip' package."
    exit 1
  fi

  install_from_github
}

#
# Unsets the various functions defined
# during the execution of the install script
#
reset() {
  unset -f reset is_installed latest_version \
    download install_from_github do_install
}

91
[ "_$GCHANGE_ENV" = "_testing" ] || do_install $1
Benoit Lavenier's avatar
Benoit Lavenier committed
92 93

} # this ensures the entire script is downloaded #