#!/bin/sh
# Relayers CLI installer.
#
#   curl -fsSL https://dl.relayers.app/cli/install.sh | sh
#
# Installs the `relayers` command for Linux/macOS (amd64/arm64). Windows users
# download relayers_windows_amd64.zip and add relayers.exe to their PATH.
#
# Overrides (env):
#   RELAYERS_INSTALL_BASE   asset base URL (default https://dl.relayers.app/cli)
#   RELAYERS_INSTALL_DIR    install directory (default /usr/local/bin or ~/.local/bin)
set -eu

BASE_URL="${RELAYERS_INSTALL_BASE:-https://dl.relayers.app/cli}"
BIN="relayers"

info() { printf '\033[0;34m==>\033[0m %s\n' "$1"; }
warn() { printf '\033[0;33mnote:\033[0m %s\n' "$1" >&2; }
die()  { printf '\033[0;31merror:\033[0m %s\n' "$1" >&2; exit 1; }

case "$(uname -s)" in
  Linux)  OS=linux ;;
  Darwin) OS=darwin ;;
  *) die "unsupported OS. On Windows, download $BASE_URL/relayers_windows_amd64.zip and put relayers.exe on your PATH." ;;
esac
case "$(uname -m)" in
  x86_64|amd64)  ARCH=amd64 ;;
  arm64|aarch64) ARCH=arm64 ;;
  *) die "unsupported architecture: $(uname -m)" ;;
esac

ASSET="relayers_${OS}_${ARCH}.tar.gz"
URL="$BASE_URL/$ASSET"

if command -v curl >/dev/null 2>&1; then fetch() { curl -fsSL "$1"; }
elif command -v wget >/dev/null 2>&1; then fetch() { wget -qO- "$1"; }
else die "need curl or wget installed"; fi

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT

info "Downloading $ASSET"
fetch "$URL" > "$TMP/$ASSET" || die "download failed: $URL"

# Verify checksum when a hasher and the manifest are both available.
hasher=""
command -v sha256sum >/dev/null 2>&1 && hasher="sha256sum"
[ -z "$hasher" ] && command -v shasum >/dev/null 2>&1 && hasher="shasum -a 256"
if [ -n "$hasher" ] && fetch "$BASE_URL/checksums.txt" > "$TMP/checksums.txt" 2>/dev/null; then
  want="$(grep "  *$ASSET\$" "$TMP/checksums.txt" | awk '{print $1}' | head -1)"
  if [ -n "$want" ]; then
    got="$($hasher "$TMP/$ASSET" | awk '{print $1}')"
    [ "$want" = "$got" ] || die "checksum mismatch for $ASSET (expected $want, got $got)"
    info "Checksum verified"
  fi
fi

info "Extracting"
tar -xzf "$TMP/$ASSET" -C "$TMP"
[ -f "$TMP/$BIN" ] || die "archive did not contain '$BIN'"
chmod +x "$TMP/$BIN"

# Pick an install directory.
if [ -n "${RELAYERS_INSTALL_DIR:-}" ]; then
  DIR="$RELAYERS_INSTALL_DIR"
elif [ -d /usr/local/bin ] && [ -w /usr/local/bin ]; then
  DIR="/usr/local/bin"
else
  DIR="$HOME/.local/bin"
fi
mkdir -p "$DIR" 2>/dev/null || die "cannot create install dir $DIR"

if ! mv "$TMP/$BIN" "$DIR/$BIN" 2>/dev/null; then
  DIR="$HOME/.local/bin"; mkdir -p "$DIR"
  mv "$TMP/$BIN" "$DIR/$BIN" || die "cannot write to $DIR"
fi
info "Installed to $DIR/$BIN"
info "$("$DIR/$BIN" version 2>/dev/null | head -1)"

case ":$PATH:" in
  *":$DIR:"*) ;;
  *) warn "$DIR is not on your PATH. Add it, e.g.:"; printf '      export PATH="%s:$PATH"\n' "$DIR" ;;
esac

cat <<EOF

Done. Next steps:
  relayers login                 # sign in with your Relayers account
  relayers listen --port 3000    # tunnel webhooks to http://localhost:3000

Docs: https://docs.relayers.app
EOF
