#!/usr/bin/env bash set -u echo "Starting tools update..." FAILED_UPDATES=0 log_section() { echo "========== $1 ==========" } command_exists() { command -v "$1" >/dev/null 2>&1 } require_command() { local cmd="$1" if ! command_exists "$cmd"; then echo "$cmd command not found, skipping." return 1 fi return 0 } run_update() { local name="$1" shift log_section "Updating $name" if ! "$@"; then echo "$name update failed." FAILED_UPDATES=$((FAILED_UPDATES + 1)) return 1 fi return 0 } update_fzf() { [ -d "$HOME/tools/fzf" ] || { echo "fzf directory not found at $HOME/tools/fzf, skipping." return 0 } require_command git || return 1 ( cd "$HOME/tools/fzf" && git pull --ff-only && ./install --bin --no-update-rc ) } update_ble_sh() { if command_exists ble-update; then ble-update return $? fi [ -d "$HOME/tools/ble.sh" ] || { echo "ble.sh not found, skipping." return 0 } require_command git || return 1 require_command make || return 1 ( cd "$HOME/tools/ble.sh" && git pull --ff-only && make ) } update_z() { [ -d "$HOME/tools/z" ] || { echo "z directory not found at $HOME/tools/z, skipping." return 0 } require_command git || return 1 ( cd "$HOME/tools/z" && git pull --ff-only ) } update_uv() { if ! command_exists uv; then echo "uv command not found, skipping." return 0 fi uv self update } update_nvm() { local nvm_dir="${NVM_DIR:-$HOME/.nvm}" local nvm_sh="$nvm_dir/nvm.sh" local api_url="https://api.github.com/repos/nvm-sh/nvm/releases/latest" local install_url_base="https://raw.githubusercontent.com/nvm-sh/nvm" local nvm_latest local nvm_current="" local installer [ -d "$nvm_dir" ] || { echo "nvm not found, skipping." return 0 } [ -s "$nvm_sh" ] || { echo "nvm.sh not found at $nvm_sh, skipping." return 0 } require_command curl || return 1 require_command bash || return 1 export NVM_DIR="$nvm_dir" # nvm is a shell function, so it must be sourced in non-interactive shells. . "$nvm_sh" nvm_latest=$( curl -fsSL "$api_url" | sed -n 's/.*"tag_name":[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1 ) if [ -z "$nvm_latest" ]; then echo "Failed to fetch latest nvm version from GitHub API." return 1 fi if command_exists nvm; then nvm_current=$(nvm --version 2>/dev/null || true) fi if [ -n "$nvm_current" ] && [ "v$nvm_current" = "$nvm_latest" ]; then echo "nvm is already up to date ($nvm_latest)." return 0 fi echo "Updating nvm to $nvm_latest..." installer=$(mktemp) || { echo "Failed to create temporary file for nvm installer." return 1 } if ! curl -fsSL "$install_url_base/$nvm_latest/install.sh" -o "$installer"; then echo "Failed to download nvm installer." rm -f "$installer" return 1 fi if ! bash "$installer"; then echo "nvm installer execution failed." rm -f "$installer" return 1 fi rm -f "$installer" return 0 } run_update "fzf" update_fzf run_update "ble.sh" update_ble_sh run_update "z" update_z run_update "uv" update_uv run_update "nvm" update_nvm if [ "$FAILED_UPDATES" -gt 0 ]; then log_section "Updates completed with $FAILED_UPDATES failure(s)" exit 1 fi log_section "All updates completed" echo "Done! Please remember to manually source ~/.bashrc or restart your terminal if needed."