#!/bin/sh # Copyright (c) 2022 - 2023 Purple Clay # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # in the Software without restriction, including without limitation the rights # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # Install script is heavily based on: https://github.com/Masterminds/glide.sh/blob/master/get : "${USE_SUDO:=true}" : "${INSTALL_DIR:=/usr/local/bin}" APP_NAME="dns53" HAS_CURL="$(type curl >/dev/null && echo true || echo false)" HAS_WGET="$(type wget >/dev/null && echo true || echo false)" initArch() { ARCH=$(uname -m) case $ARCH in armv5*) ARCH="arm";; armv6*) ARCH="arm";; armv7*) ARCH="arm";; aarch64) ARCH="arm64";; x86) ARCH="i386";; x86_64) ARCH="x86_64";; i686) ARCH="i386";; i386) ARCH="i386";; ppc64le) ARCH="ppc64le";; esac } initOS() { OS=$(uname|tr '[:upper:]' '[:lower:]') case "$OS" in # Minimalist GNU for Windows mingw*) OS='windows';; msys*) OS='windows';; esac } canDownload() { _supported="darwin-amd64\ndarwin-x86_64\nlinux-arm\nlinux-arm64\nlinux-arm386\nlinux-i386\nlinux-ppc64le\nlinux-x86_64\nwindows-arm\nwindows-i386\nwindows-x86_64" if ! echo "${_supported}" | grep -q "${OS}-${ARCH}"; then echo "No prebuilt binary currently exists for ${OS}-${ARCH}." exit 1 fi if [ "${HAS_CURL}" != "true" ] && [ "${HAS_WGET}" != "true" ]; then echo "Either curl or wget is required to download binary. Please install and try again" exit 1 fi } download() { if [ -z "$DESIRED_VERSION" ]; then if [ "${HAS_CURL}" = "true" ]; then TAG="v$(curl -s https://api.github.com/repos/purpleclay/$APP_NAME/releases/latest | grep "tag_name" | cut -d'v' -f2 | cut -d'"' -f1)" elif [ "${HAS_WGET}" = "true" ]; then TAG="v$(wget -q https://api.github.com/repos/purpleclay/$APP_NAME/releases/latest -O - 2>&1 | grep "tag_name" | cut -d'v' -f2 | cut -d'"' -f1)" fi else TAG=${DESIRED_VERSION} fi echo "Attempting to download ${APP_NAME} version ${TAG}..." PACKAGE_TYPE="tar.gz" if [ "${OS}" = "windows" ]; then PACKAGE_TYPE="zip" fi _archive="${APP_NAME}_${TAG#v}_${OS}-${ARCH}.${PACKAGE_TYPE}" DOWNLOAD_URL="https://github.com/purpleclay/${APP_NAME}/releases/download/${TAG}/${_archive}" DOWNLOAD_DIR="$(mktemp -dt ${APP_NAME}-install-XXXXXXX)" DOWNLOAD_FILE="${DOWNLOAD_DIR}/${_archive}" if [ "${HAS_CURL}" = "true" ]; then curl -L "$DOWNLOAD_URL" -o "$DOWNLOAD_FILE" elif [ "${HAS_WGET}" = "true" ]; then wget -q -O "$DOWNLOAD_FILE" "$DOWNLOAD_URL" fi } install() { echo "Installing ${APP_NAME}..." test ! -d "$INSTALL_DIR" && mkdir -p "$INSTALL_DIR" _extract_dir="$DOWNLOAD_DIR/${APP_NAME}-${TAG}" mkdir -p "$_extract_dir" tar xf "$DOWNLOAD_FILE" -C "${_extract_dir}" runAsRoot cp "${_extract_dir}/${APP_NAME}" "${INSTALL_DIR}/${APP_NAME}" echo "Installed ${APP_NAME} to ${INSTALL_DIR}" } runAsRoot() { if [ "$(id -u)" -ne 0 ] && [ "$USE_SUDO" = "true" ]; then sudo "${@}" else "${@}" fi } tidy() { if [ -d "${DOWNLOAD_DIR:-}" ]; then rm -rf "$DOWNLOAD_DIR" fi } verify() { set +e type "$APP_NAME" >/dev/null if [ "$?" = "1" ]; then echo "${APP_NAME} not found. Is ${INSTALL_DIR} on your PATH?" exit 1 fi # Test version INSTALLED_VERSION="$($APP_NAME version --short)" if [ "${INSTALLED_VERSION}" != "${TAG}" ]; then echo "Found version ${INSTALLED_VERSION} of ${APP_NAME} and not expected installed version of $TAG" exit 1 fi set -e } bye() { _result=$? if [ "$_result" != "0" ]; then echo "Failed to install ${APP_NAME}" fi tidy exit $_result } help () { echo "${APP_NAME} installer" echo echo "Flags:" echo " -d, --dir a directory where the binary will be installed (default '$INSTALL_DIR')" echo " --no-sudo install without using sudo" echo " -v, --version download and install a specific version (default 'latest')" echo " -h, --help Print help for the installer" } trap "bye" EXIT set -e # Parsing input arguments (if any) set -u while [ $# -gt 0 ]; do case $1 in '--version'|-v) shift if [ $# -ne 0 ]; then export DESIRED_VERSION="${1}" else echo "Please provide a valid version: e.g. --version v0.1.0 or -v v0.1.0" exit 0 fi ;; '--dir'|-d) shift if [ $# -ne 0 ]; then INSTALL_DIR="${1}" else echo "Please provide a valid location for the install directory" exit 0 fi ;; '--no-sudo') USE_SUDO="false" ;; '--help'|-h) help exit 0 ;; *) help echo exit 1 ;; esac shift done set +u initArch initOS canDownload download install verify tidy