#!/usr/bin/env bash
# Yomia CLI installer.
#
#   bash <(curl -fsSL https://yomia.dev/install.sh)
#
# Downloads the latest `yomia` release for your OS/arch from GitHub, verifies
# its checksum, and installs it. Override with environment variables:
#
#   YOMIA_VERSION       release tag to install (default: latest, e.g. v1.2.3)
#   YOMIA_INSTALL_DIR   where to put the binary (default: /usr/local/bin,
#                       falling back to ~/.local/bin when that isn't writable)
set -euo pipefail

REPO="yomia-org/cli"
BINARY="yomia"

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

command -v curl >/dev/null 2>&1 || err "curl is required"
command -v tar  >/dev/null 2>&1 || err "tar is required"

os=$(uname -s)
case "$os" in
  Linux)  os=linux ;;
  Darwin) os=darwin ;;
  *) err "unsupported OS: $os (Linux and macOS only)" ;;
esac

arch=$(uname -m)
case "$arch" in
  x86_64|amd64)  arch=amd64 ;;
  arm64|aarch64) arch=arm64 ;;
  *) err "unsupported architecture: $arch" ;;
esac

version=${YOMIA_VERSION:-}
if [ -z "$version" ]; then
  info "Finding the latest release"
  version=$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" \
    | grep -o '"tag_name":[[:space:]]*"[^"]*"' | head -1 | sed -E 's/.*"([^"]*)"$/\1/')
  [ -n "$version" ] || err "could not determine the latest version (set YOMIA_VERSION)"
fi
num=${version#v}

asset="${BINARY}_${num}_${os}_${arch}.tar.gz"
base="https://github.com/$REPO/releases/download/$version"

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

info "Downloading $asset ($version)"
curl -fsSL "$base/$asset" -o "$tmp/$asset" || err "download failed: $base/$asset"

# Verify the checksum when the release publishes one.
if curl -fsSL "$base/checksums.txt" -o "$tmp/checksums.txt" 2>/dev/null; then
  want=$(grep " $asset\$" "$tmp/checksums.txt" | awk '{print $1}')
  if [ -n "$want" ]; then
    info "Verifying checksum"
    if command -v sha256sum >/dev/null 2>&1; then
      got=$(sha256sum "$tmp/$asset" | awk '{print $1}')
    else
      got=$(shasum -a 256 "$tmp/$asset" | awk '{print $1}')
    fi
    [ "$want" = "$got" ] || err "checksum mismatch for $asset"
  fi
fi

tar -xzf "$tmp/$asset" -C "$tmp"
[ -f "$tmp/$BINARY" ] || err "archive did not contain $BINARY"
chmod +x "$tmp/$BINARY"

dir=${YOMIA_INSTALL_DIR:-}
if [ -z "$dir" ]; then
  if { [ -d /usr/local/bin ] && [ -w /usr/local/bin ]; } || [ "$(id -u)" = 0 ]; then
    dir=/usr/local/bin
  else
    dir="$HOME/.local/bin"
  fi
fi
mkdir -p "$dir"

if mv "$tmp/$BINARY" "$dir/$BINARY" 2>/dev/null; then
  :
elif [ "$dir" = /usr/local/bin ] && command -v sudo >/dev/null 2>&1; then
  info "Installing to $dir (needs sudo)"
  sudo mv "$tmp/$BINARY" "$dir/$BINARY"
else
  err "could not write to $dir (set YOMIA_INSTALL_DIR to a writable path)"
fi

info "Installed $BINARY to $dir/$BINARY"
case ":$PATH:" in
  *":$dir:"*) ;;
  *) printf '\nAdd it to your PATH:\n  export PATH="%s:$PATH"\n' "$dir" ;;
esac
printf '\nRun \033[1m%s login\033[0m to get started.\n' "$BINARY"
