#!/bin/bash
# Momentry Core 安裝腳本
set -e
APP_NAME="MomentryCore"
INSTALL_DIR="$HOME/Library/Application Support/$APP_NAME"
LAUNCHD_DIR="$HOME/Library/LaunchAgents"
LOG_DIR="$INSTALL_DIR/logs"
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
echo "================================================"
echo " Momentry Core 安裝程式"
echo "================================================"
# 1. 檢查依賴
echo ">>> 檢查系統依賴..."
MISSING=""
check() {
if eval "$2" >/dev/null 2>&1; then
echo " [OK] $1"
else
echo " [FAIL] $1 - $3"
MISSING="$MISSING $1"
fi
}
check "PostgreSQL" "psql --version" "brew install postgresql"
check "Redis" "redis-cli ping" "brew install redis"
check "Qdrant" "curl -s --max-time 3 http://localhost:6333/ready | grep -q ok" "brew install qdrant"
check "MongoDB" "mongosh --version || mongo --version" "brew install mongosh"
check "Python 3.11" "python3.11 --version" "brew install python@3.11"
check "ffprobe" "ffprobe -version" "brew install ffmpeg"
check "MarkBaseServer" "curl -s --max-time 3 http://localhost:8080/v1/models | grep -q model" "安裝 MarkBaseEngine"
if [ -n "$MISSING" ]; then
echo -e "\n${RED}缺少依賴:$MISSING${NC}"
echo "brew install postgresql redis mongosh ffmpeg python@3.11"
exit 1
fi
echo -e "\n${GREEN}所有依賴已安裝${NC}"
# 2. 建立目錄
echo ">>> 建立目錄..."
mkdir -p "$INSTALL_DIR"/{bin,config,tools/script,tools/cli,logs}
SRC_DIR="$(cd "$(dirname "$0")" && pwd)"
cp -f "$SRC_DIR/bin/momentry" "$INSTALL_DIR/bin/" 2>/dev/null || echo " [!] momentry binary 需手動放置"
cp -f "$SRC_DIR/bin/pipeline_worker" "$INSTALL_DIR/bin/" 2>/dev/null || echo " [!] pipeline_worker 需手動放置"
chmod +x "$INSTALL_DIR/bin/"* 2>/dev/null || true
cp -rf "$SRC_DIR/config/"* "$INSTALL_DIR/config/" 2>/dev/null || true
cp -rf "$SRC_DIR/tools/"* "$INSTALL_DIR/tools/" 2>/dev/null || true
echo " -> $INSTALL_DIR"
# 3. 產生 .env
ENV_FILE="$INSTALL_DIR/.env"
if [ ! -f "$ENV_FILE" ]; then
cat > "$ENV_FILE" << ENVEOF
DATABASE_URL=postgres://accusys@localhost:5432/momentry
DATABASE_SCHEMA=public
REDIS_URL=redis://:accusys@localhost:6379
MOMENTRY_REDIS_PREFIX=momentry:
MOMENTRY_SERVER_PORT=3002
MOMENTRY_OUTPUT_DIR=$HOME/momentry/output
QDRANT_URL=http://127.0.0.1:6333
MONGODB_URL=mongodb://localhost:27017
MOMENTRY_LLM_VISION_URL=http://127.0.0.1:8080/v1/chat/completions
MOMENTRY_LLM_VISION_MODEL=gemma-4-12b-it
MOMENTRY_LLM_CHAT_URL=http://127.0.0.1:8080/v1/chat/completions
MOMENTRY_LLM_CHAT_MODEL=gemma-4-12b-it
MOMENTRY_PYTHON_PATH=/opt/homebrew/bin/python3.11
MOMENTRY_SCRIPTS_DIR=$INSTALL_DIR/tools/script
MOMENTRY_CLI_DIR=$INSTALL_DIR/tools/cli
RUST_LOG=info
ENVEOF
echo " [OK] 已產生 .env"
else
echo " [OK] .env 已存在"
fi
# 4. 安裝 Launchd
echo ">>> 安裝 launchd services..."
cat > "$LAUNCHD_DIR/com.momentry-core.server.plist" << PLIST
Labelcom.momentry-core.server
Program$INSTALL_DIR/bin/momentry
ProgramArguments
momentryserver
--host0.0.0.0
--port3002
WorkingDirectory$INSTALL_DIR
EnvironmentVariables
DOTENV_PATH$ENV_FILE
KeepAliveSuccessfulExit
RunAtLoad
StandardOutPath$LOG_DIR/server.log
StandardErrorPath$LOG_DIR/server.log
PLIST
cat > "$LAUNCHD_DIR/com.momentry-core.worker.plist" << PLIST
Labelcom.momentry-core.worker
Program$INSTALL_DIR/bin/pipeline_worker
WorkingDirectory$INSTALL_DIR
EnvironmentVariables
DOTENV_PATH$ENV_FILE
KeepAliveSuccessfulExit
RunAtLoad
StandardOutPath$LOG_DIR/worker.log
StandardErrorPath$LOG_DIR/worker.log
PLIST
echo " [OK] server + worker plist"
# 5. 啟動
echo ">>> 啟動服務..."
launchctl bootout "gui/$(id -u)/com.momentry-core.server" 2>/dev/null || true
launchctl bootout "gui/$(id -u)/com.momentry-core.worker" 2>/dev/null || true
sleep 1
launchctl bootstrap "gui/$(id -u)" "$LAUNCHD_DIR/com.momentry-core.server.plist"
launchctl bootstrap "gui/$(id -u)" "$LAUNCHD_DIR/com.momentry-core.worker.plist"
sleep 3
if curl -s --max-time 5 "http://localhost:3002/api/v1/health" >/dev/null 2>&1; then
echo -e "${GREEN}[OK] API server 啟動成功${NC}"
else
echo -e "${YELLOW}[!] 檢查日誌: $LOG_DIR/server.log${NC}"
fi
echo ""
echo "================================================"
echo " 安裝完成!"
echo " 目錄: $INSTALL_DIR"
echo " API: http://localhost:3002"
echo "================================================"