#!/usr/bin/env bash
# gesher — the mesh, in your terminal.  (Linux · macOS · Termux)
#   curl -fsSL https://gmesh.ap1.space | bash
set -e
echo "== installing gesher (mesh terminal switcher) =="
has(){ command -v "$1" >/dev/null 2>&1; }
node_ok(){ has node && has npm; }
if ! node_ok; then
  echo "-- installing Node.js"
  S=""; [ "$(id -u)" -ne 0 ] && has sudo && S=sudo
  if   has brew;    then brew install node
  elif has pkg;     then pkg install -y nodejs            # Termux
  elif has apt-get; then $S apt-get update -qq && $S apt-get install -y nodejs npm
  elif has dnf;     then $S dnf install -y nodejs npm
  elif has pacman;  then $S pacman -S --needed --noconfirm nodejs npm
  elif has apk;     then $S apk add nodejs npm
  elif has zypper;  then $S zypper install -y nodejs npm
  fi
fi
node_ok || { echo "Node.js is required — get it from https://nodejs.org then run this again."; exit 1; }
npm install -g https://gmesh.ap1.space/gesher-mesh.tgz
hash -r 2>/dev/null || true
echo
if has gesher; then
  echo "== done.  type:  gesher  =="
else
  echo "== installed. add npm's global bin to PATH:  export PATH=\"\$(npm prefix -g)/bin:\$PATH\"  then:  gesher =="
fi
