#!/bin/bash set -e # Target paths INSTALL_DIR="$HOME/Library/Application Support/TypeWhisper-Nhi" PLIST_PATH="$HOME/Library/LaunchAgents/com.typewhisper.nhi.plist" LOG_DIR="$HOME/Library/Logs" mkdir -p "$LOG_DIR" # Parse version from json get_version() { if [ -f "$1" ]; then grep -o '"version": *"[^"]*"' "$1" | cut -d'"' -f4 else echo "0.0.0" fi } PKG_VERSION=$(get_version "version.json") echo "==========================================================" echo " TypeWhisper-Nhi Installer (Package Version: $PKG_VERSION)" echo "==========================================================" # Check if already installed if [ -d "$INSTALL_DIR" ] && [ -f "$INSTALL_DIR/version.json" ]; then INSTALLED_VERSION=$(get_version "$INSTALL_DIR/version.json") echo "🔍 Detected existing installation: Version $INSTALLED_VERSION" if [ "$INSTALLED_VERSION" = "$PKG_VERSION" ]; then read -p "TypeWhisper-Nhi is already up to date. Force reinstall? (y/n): " answer if [ "$answer" != "y" ] && [ "$answer" != "Y" ]; then echo "Installation cancelled." exit 0 fi echo "🔄 Reinstalling Version $PKG_VERSION..." else echo "🔄 Updating TypeWhisper-Nhi from $INSTALLED_VERSION to $PKG_VERSION..." echo "📜 What's New:" if [ -f "changelog.md" ]; then head -n 25 changelog.md || true fi echo "----------------------------------------------------------" fi # Perform upgrade / overwrite of code files cp proxy.py "$INSTALL_DIR/proxy.py" cp version.json "$INSTALL_DIR/version.json" if [ -f "changelog.md" ]; then cp changelog.md "$INSTALL_DIR/changelog.md" fi if [ -f "readme.md" ]; then cp readme.md "$INSTALL_DIR/readme.md" fi # Ensure dependencies are up-to-date echo "🐍 Updating Python packages..." cd "$INSTALL_DIR" ./venv/bin/pip install --upgrade pip ./venv/bin/pip install fastapi uvicorn httpx python-multipart numpy onnxruntime transformers huggingface_hub kaldi-native-fbank sentencepiece scikit-learn # Log update echo "[$(date '+%Y-%m-%d %H:%M:%S')] Updated from $INSTALLED_VERSION to $PKG_VERSION" >> "$INSTALL_DIR/update_history.log" # Restart service echo "🔁 Restarting background service..." launchctl unload "$PLIST_PATH" 2>/dev/null || true launchctl load "$PLIST_PATH" echo "✅ Update complete!" exit 0 fi # Clean install flow echo "📂 Creating installation folder..." mkdir -p "$INSTALL_DIR" echo "📥 Copying files..." cp proxy.py "$INSTALL_DIR/proxy.py" cp version.json "$INSTALL_DIR/version.json" if [ -f "changelog.md" ]; then cp changelog.md "$INSTALL_DIR/changelog.md" fi if [ -f "readme.md" ]; then cp readme.md "$INSTALL_DIR/readme.md" fi cd "$INSTALL_DIR" # Download static FFmpeg binary echo "📥 Downloading static FFmpeg binary (no Homebrew required)..." curl -L -o ffmpeg.zip https://evermeet.cx/ffmpeg/getrelease/zip unzip -o ffmpeg.zip ffmpeg rm ffmpeg.zip chmod +x ffmpeg # Create python virtual environment and install dependencies echo "🐍 Setting up Python environment..." python3 -m venv venv ./venv/bin/pip install --upgrade pip ./venv/bin/pip install fastapi uvicorn httpx python-multipart numpy onnxruntime transformers huggingface_hub kaldi-native-fbank sentencepiece scikit-learn # Create LaunchAgent plist echo "⚙️ Creating macOS LaunchAgent..." cat < "$PLIST_PATH" Label com.typewhisper.nhi ProgramArguments $INSTALL_DIR/venv/bin/python3 $INSTALL_DIR/proxy.py RunAtLoad KeepAlive WorkingDirectory $INSTALL_DIR EnvironmentVariables PATH $INSTALL_DIR:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin StandardOutPath $LOG_DIR/typewhisper_nhi.log StandardErrorPath $LOG_DIR/typewhisper_nhi.err.log EOF # Load the agent echo "🚀 Starting background service..." launchctl unload "$PLIST_PATH" 2>/dev/null || true launchctl load "$PLIST_PATH" # Log fresh install echo "[$(date '+%Y-%m-%d %H:%M:%S')] Fresh install of Version $PKG_VERSION" >> "$INSTALL_DIR/update_history.log" echo "==========================================================" echo "🎉 Installation Successful!" echo "TypeWhisper-Nhi is now running in the background." echo "Please follow the instructions in the README to configure TypeWhisper." echo "=========================================================="