#!/bin/sh set -eu # OpenFang Updater # https://openfang.sh REPO="RightNow-AI/openfang" INSTALL_DIR="$HOME/.openfang" BIN_DIR="$HOME/.openfang/bin" BINARY="openfang" main() { need_cmd curl need_cmd tar need_cmd uname # Check current installation _current="" if command -v openfang >/dev/null 2>&1; then _current="$(openfang --version 2>/dev/null || echo "unknown")" say "Current: $_current" elif [ -x "${BIN_DIR}/${BINARY}" ]; then _current="$("${BIN_DIR}/${BINARY}" --version 2>/dev/null || echo "unknown")" say "Current: $_current" else say "OpenFang not found — installing fresh" fi _os="$(uname -s)" _arch="$(uname -m)" case "$_os" in Linux) case "$_arch" in x86_64|amd64) _target="x86_64-unknown-linux-gnu" ;; aarch64|arm64) _target="aarch64-unknown-linux-gnu" ;; *) err "Unsupported architecture: $_arch" ;; esac ;; Darwin) case "$_arch" in x86_64|amd64) _target="x86_64-apple-darwin" ;; aarch64|arm64) _target="aarch64-apple-darwin" ;; *) err "Unsupported architecture: $_arch" ;; esac ;; *) err "Unsupported OS: $_os (use irm https://openfang.sh/update.ps1 | iex on Windows)" ;; esac _url="https://github.com/${REPO}/releases/latest/download/openfang-${_target}.tar.gz" say "Detected: $_os $_arch -> $_target" say "Downloading latest release..." _tmpdir="$(mktemp -d 2>/dev/null || mktemp -d -t openfang)" trap 'rm -rf "$_tmpdir"' EXIT _code=$(curl -fsSL -w "%{http_code}" "$_url" -o "${_tmpdir}/openfang.tar.gz") || true if [ "$_code" = "404" ]; then err "Release not found for ${_target}. Check https://github.com/${REPO}/releases" fi if [ ! -f "${_tmpdir}/openfang.tar.gz" ] || [ "$(wc -c < "${_tmpdir}/openfang.tar.gz")" -lt 1000 ]; then err "Download failed (HTTP ${_code}). Check https://github.com/${REPO}/releases" fi say "Extracting..." tar -xzf "${_tmpdir}/openfang.tar.gz" -C "$_tmpdir" # Find the binary _bin="$(find "$_tmpdir" -name "$BINARY" -type f -perm +111 2>/dev/null | head -1)" if [ -z "$_bin" ]; then _bin="$(find "$_tmpdir" -name "$BINARY" -type f | head -1)" fi if [ -z "$_bin" ]; then err "Could not find openfang binary in archive" fi mkdir -p "$BIN_DIR" cp "$_bin" "${BIN_DIR}/${BINARY}" chmod +x "${BIN_DIR}/${BINARY}" # Show result _new="" if "${BIN_DIR}/${BINARY}" --version >/dev/null 2>&1; then _new="$("${BIN_DIR}/${BINARY}" --version 2>/dev/null || echo "unknown")" fi say "" if [ -n "$_current" ] && [ -n "$_new" ]; then if [ "$_current" = "$_new" ]; then say "Already up to date: $_new" else say "Updated: $_current -> $_new" fi elif [ -n "$_new" ]; then say "Installed: $_new" else say "Updated to latest release" fi say "" } say() { printf " \033[1;36mopenfang\033[0m %s\n" "$1" } err() { printf " \033[1;36mopenfang\033[0m \033[1;31merror:\033[0m %s\n" "$1" >&2 exit 1 } need_cmd() { if ! command -v "$1" >/dev/null 2>&1; then err "need '$1' (command not found)" fi } main