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

{ # this ensures the entire script is downloaded #

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

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

latest_version() {
Benoit Lavenier's avatar
Benoit Lavenier committed
18
  echo "v2.0.2" #lastest
Benoit Lavenier's avatar
Benoit Lavenier committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
}

api_release_url() {
  echo "https://api.github.com/repos/duniter/cesium2/releases/tags/$(latest_version)"
}

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 /')
36
    wget ${ARGS}
Benoit Lavenier's avatar
Benoit Lavenier committed
37 38 39 40 41 42 43 44
  fi
}

install_from_github() {

  local RELEASE=`curl -XGET -i $(api_release_url)`
  local ARCHIVE_URL=`echo "$RELEASE" | grep -P "\"browser_download_url\": \"[^\"]+" | grep -oP "https://[a-zA-Z0-9/.-]+-web.zip"`
  local TMP_DIR=/tmp/cesium2-install
45 46 47
  local ARCHIVE_FILE=${TMP_DIR}/cesium2-$(latest_version)-web.zip
  if [[ -d "$INSTALL_DIR" ]]; then
    if [[ -f "$ARCHIVE_FILE" ]]; then
Benoit Lavenier's avatar
Benoit Lavenier committed
48
      echo "WARNING: Deleting existing archive [$ARCHIVE_FILE]"
49
      rm ${ARCHIVE_FILE}
Benoit Lavenier's avatar
Benoit Lavenier committed
50 51 52 53 54 55
    fi
    
  else
    mkdir -p "$INSTALL_DIR"
  fi

56
  if [[ -d "${TMP_DIR}" ]]; then
Benoit Lavenier's avatar
Benoit Lavenier committed
57
    echo "WARNING: Deleting existing temp directory [$TMP_DIR]"
58
    rm -rf ${TMP_DIR}
Benoit Lavenier's avatar
Benoit Lavenier committed
59 60 61 62 63 64 65 66 67
  fi
  mkdir -p "$TMP_DIR"

  echo "Downloading [$ARCHIVE_URL]"
  download "$ARCHIVE_URL" -o "$ARCHIVE_FILE" || {
      echo >&2 "Failed to download '$ARCHIVE_URL'"
      return 4
    }
  echo "Unarchive to $INSTALL_DIR"
68 69 70
  unzip -o ${ARCHIVE_FILE} -d ${TMP_DIR}
  cp -rf ${TMP_DIR}/* ${INSTALL_DIR}
  rm -rf ${TMP_DIR}
Benoit Lavenier's avatar
Benoit Lavenier committed
71 72 73

  echo

74
  echo "Successfully installed at ${INSTALL_DIR}"
Benoit Lavenier's avatar
Benoit Lavenier committed
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
}

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
}

[[ "_$CESIUM2_ENV" = "_testing" ]] || do_install $1

} # this ensures the entire script is downloaded #