#!/bin/sh # Mottainai CLI installer # Usage: curl -sSfL mottainai.app/cli/install.sh | sh set -eu BINARY="mo" REPO="gukosowa/mottainai-cli" INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}" USER_INSTALL_DIR="${HOME}/.local/bin" # ---------- detect OS ---------- OS="$(uname -s)" case "$OS" in Darwin) OS="darwin" ;; Linux) OS="linux" ;; *) echo "Unsupported OS: $OS" >&2 exit 1 ;; esac # ---------- detect arch ---------- ARCH="$(uname -m)" case "$ARCH" in x86_64 | amd64) ARCH="amd64" ;; arm64 | aarch64) ARCH="arm64" ;; *) echo "Unsupported architecture: $ARCH" >&2 exit 1 ;; esac echo "Detecting platform: ${OS}/${ARCH}" # ---------- resolve latest version ---------- LATEST_URL="https://github.com/${REPO}/releases/latest" VERSION="$(curl -sSfL -o /dev/null -w '%{url_effective}' "$LATEST_URL" \ | sed 's|.*/tag/||')" if [ -z "$VERSION" ]; then echo "Could not determine latest version" >&2 exit 1 fi echo "Downloading ${BINARY} ${VERSION}..." # ---------- download + extract ---------- TARBALL="${BINARY}_${VERSION}_${OS}_${ARCH}.tar.gz" URL="https://github.com/${REPO}/releases/download/${VERSION}/${TARBALL}" TMP="$(mktemp -d)" # shellcheck disable=SC2064 trap "rm -rf '$TMP'" EXIT curl -sSfL "$URL" -o "${TMP}/${TARBALL}" tar -xzf "${TMP}/${TARBALL}" -C "$TMP" if [ ! -f "${TMP}/${BINARY}" ]; then echo "Error: binary not found in archive" >&2 exit 1 fi chmod +x "${TMP}/${BINARY}" # ---------- install ---------- TARGET_DIR="$INSTALL_DIR" ensure_writable_dir() { target="$1" if ! mkdir -p "$target" 2>/dev/null; then return 1 fi probe="${target}/.${BINARY}-write-check-$$" if ! : >"$probe" 2>/dev/null; then return 1 fi rm -f "$probe" return 0 } if ! ensure_writable_dir "$TARGET_DIR"; then if [ "$INSTALL_DIR" != "/usr/local/bin" ]; then echo "Install directory is not writable: ${INSTALL_DIR}" >&2 exit 1 fi echo "No write access to ${INSTALL_DIR}; using ${USER_INSTALL_DIR}." if ! ensure_writable_dir "$USER_INSTALL_DIR"; then echo "User install directory is not writable: ${USER_INSTALL_DIR}" >&2 exit 1 fi TARGET_DIR="$USER_INSTALL_DIR" fi mv "${TMP}/${BINARY}" "${TARGET_DIR}/${BINARY}" echo "✓ Installed ${BINARY} to ${TARGET_DIR}/${BINARY}" if [ "$TARGET_DIR" = "$USER_INSTALL_DIR" ]; then case ":$PATH:" in *":${USER_INSTALL_DIR}:"*) ;; *) echo "" echo "Add ${USER_INSTALL_DIR} to your PATH (example):" echo " export PATH=\"${USER_INSTALL_DIR}:\$PATH\"" ;; esac fi echo "" echo "Next steps:" echo " ${BINARY} auth login"