#!/usr/bin/env sh set -e REPO="vernette/warpscout" BIN_NAME="warpscout" API_URL="https://api.github.com/repos/${REPO}/releases/latest" ASSUME_YES=0 WANT_VERSION="" if [ -e /etc/openwrt_release ]; then DEFAULT_DIR="/usr/bin" elif [ -n "${TERMUX_VERSION}" ]; then DEFAULT_DIR="${PREFIX}/bin" else DEFAULT_DIR="${HOME}/.local/bin" fi INSTALL_DIR="${INSTALL_DIR:-$DEFAULT_DIR}" for c in curl wget; do if command -v "$c" >/dev/null 2>&1; then DOWNLOADER="$c" break fi done # fetch fetch() { if [ "$DOWNLOADER" = "curl" ]; then if [ "$2" = "-" ]; then "$DOWNLOADER" -fsSL "$1" else "$DOWNLOADER" -fsSL -o "$2" "$1" fi return fi "$DOWNLOADER" -qO "$2" "$1" } usage() { cat < install this release instead of the latest (v0.9.0 or 0.9.0) --uninstall remove an installed ${BIN_NAME} and exit -y, --yes do not ask before replacing an installed ${BIN_NAME} -h, --help show this help Environment: INSTALL_DIR where to put the binary (default ${DEFAULT_DIR}) EOF } parse_args() { while [ $# -gt 0 ]; do case "$1" in --version) [ $# -ge 2 ] || die "--version needs a value." WANT_VERSION="${2#v}" shift 2 ;; --uninstall) uninstall ;; -y | --yes) ASSUME_YES=1 shift ;; -h | --help) usage exit 0 ;; *) die "Unknown option: $1" ;; esac done } die() { echo "Error: $1" >&2 exit 1 } uninstall() { if [ ! -e "${INSTALL_DIR}/${BIN_NAME}" ]; then echo "Nothing to remove: ${INSTALL_DIR}/${BIN_NAME} does not exist." exit 0 fi rm -f "${INSTALL_DIR}/${BIN_NAME}" echo "Removed ${INSTALL_DIR}/${BIN_NAME}" exit 0 } detect_platform() { # Termux reports itself as Linux under uname -s, only -o says Android. case "$(uname -o 2>/dev/null || uname -s)" in Android) OS="android" ;; Linux | GNU/Linux) OS="linux" ;; Darwin) OS="darwin" ;; *) die "Unsupported OS: $(uname -s). Windows builds are on the releases page." ;; esac case "$(uname -m)" in x86_64 | amd64) ARCH="amd64" ;; aarch64 | arm64) ARCH="arm64" ;; *) die "Unsupported architecture: $(uname -m)." ;; esac } resolve_version() { if [ -n "$WANT_VERSION" ]; then VERSION="$WANT_VERSION" return fi VERSION="$(fetch "$API_URL" - | grep -o '"tag_name": *"[^"]*"' | head -n 1 | cut -d'"' -f4)" VERSION="${VERSION#v}" [ -n "$VERSION" ] || die "Failed to resolve the latest release of ${REPO}." } check_installed() { [ -x "${INSTALL_DIR}/${BIN_NAME}" ] || return 0 CURRENT="$("${INSTALL_DIR}/${BIN_NAME}" version 2>/dev/null || echo "unknown")" if [ "$CURRENT" = "$VERSION" ]; then echo "${BIN_NAME} ${VERSION} is already installed in ${INSTALL_DIR}." exit 0 fi confirm_update } confirm_update() { headline="Update available" if [ -n "$WANT_VERSION" ]; then headline="Requested version differs" fi printf "%s: %s %s -> %s (in %s)\n" \ "$headline" "$BIN_NAME" "$CURRENT" "$VERSION" "$INSTALL_DIR" if [ "$ASSUME_YES" = 1 ] || ! (: /dev/null; then return 0 fi printf "Install %s now? [Y/n] " "$VERSION" read -r answer > ~/.profile" } main() { parse_args "$@" [ -n "$DOWNLOADER" ] || die "Need curl or wget." detect_platform resolve_version check_installed download_and_install printf "%s\n%s\n" "Installed ${BIN_NAME} ${VERSION} to ${INSTALL_DIR}" check_quarantine check_path } main "$@"