#!/usr/bin/env bash
set -e

b=$'\033[1m' d=$'\033[2m' g=$'\033[32m' c=$'\033[36m' y=$'\033[33m' r=$'\033[0m'

OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)

case "$OS" in
  darwin) OS=macos ;;
  linux) OS=linux ;;
  *) echo "${y}Unsupported OS: $OS${r}" >&2; exit 1 ;;
esac

case "$ARCH" in
  x86_64) ARCH=x64 ;;
  arm64|aarch64) ARCH=arm64 ;;
  *) echo "${y}Unsupported arch: $ARCH${r}" >&2; exit 1 ;;
esac

BINARY="toes-${OS}-${ARCH}"
URL="__TOES_URL__/dist/${BINARY}"

if [ -n "$TOES_INSTALL_DIR" ]; then
  INSTALL_DIR="$TOES_INSTALL_DIR"
elif [ -d /usr/local/bin ] && [ -w /usr/local/bin ]; then
  INSTALL_DIR=/usr/local/bin
else
  INSTALL_DIR="$HOME/.local/bin"
fi
mkdir -p "$INSTALL_DIR"

echo ""
echo "  ${b}🐾 toes cli${r}"
echo "  ${d}─────────────────────────────${r}"
echo ""
echo "  ${d}Fetching ${OS}/${ARCH}...${r}"
curl -fsSL "$URL" -o "$INSTALL_DIR/toes"
chmod +x "$INSTALL_DIR/toes"
echo "  ${g}Installed to${r} ${b}$INSTALL_DIR/toes${r}"
echo ""

if ! echo "$PATH" | tr ':' '\n' | grep -qx "$INSTALL_DIR"; then
  echo "  ${y}Add $INSTALL_DIR to your \$PATH, then:${r}"
fi
echo "  Run ${c}toes${r} to get started."
echo ""
