# MCP Remote Server Makefile
# Provides convenient commands for development and production deployment

.PHONY: help install build start stop restart status logs dev clean test lint format pm2-setup pm2-start pm2-stop pm2-restart pm2-status pm2-logs pm2-delete

# Default target
help:
	@echo "MCP Remote Server - Available Commands:"
	@echo ""
	@echo "Development:"
	@echo "  make install     - Install dependencies"
	@echo "  make build       - Build TypeScript to JavaScript"
	@echo "  make dev         - Start development server with hot reload"
	@echo "  make test        - Run tests"
	@echo "  make lint        - Run ESLint"
	@echo "  make format      - Format code with Prettier"
	@echo "  make clean       - Clean build artifacts"
	@echo ""
	@echo "Production (Direct):"
	@echo "  make start       - Build and start server"
	@echo "  make stop        - Stop server (if running in background)"
	@echo ""
	@echo "Production (PM2):"
	@echo "  make pm2-setup   - Install PM2 globally (requires sudo/admin)"
	@echo "  make pm2-start   - Start server with PM2"
	@echo "  make pm2-stop    - Stop PM2 server"
	@echo "  make pm2-restart - Restart PM2 server"
	@echo "  make pm2-status  - Show PM2 status"
	@echo "  make pm2-logs    - Show PM2 logs"
	@echo "  make pm2-delete  - Delete PM2 process"
	@echo ""

# Development commands
install:
	@echo "Installing dependencies..."
	npm install

build:
	@echo "Building TypeScript..."
	npm run build

dev:
	@echo "Starting development server..."
	npm run dev

test:
	@echo "Running tests..."
	npm test

lint:
	@echo "Running ESLint..."
	npm run lint

format:
	@echo "Formatting code..."
	npm run format

clean:
	@echo "Cleaning build artifacts..."
	rm -rf dist/
	rm -rf node_modules/.cache/

# Direct production commands
start: build
	@echo "Starting server..."
	npm start

stop:
	@echo "Stopping server..."
	@pkill -f "node dist/index.js" || echo "No server process found"

# PM2 commands
pm2-setup:
	@echo "Installing PM2 globally..."
	npm install -g pm2

pm2-start: build
	@echo "Starting server with PM2..."
	pm2 start ecosystem.config.js

pm2-stop:
	@echo "Stopping PM2 server..."
	pm2 stop mcp-remote-server

pm2-restart:
	@echo "Restarting PM2 server..."
	pm2 restart mcp-remote-server

pm2-status:
	@echo "PM2 status:"
	pm2 status

pm2-logs:
	@echo "Showing PM2 logs..."
	pm2 logs mcp-remote-server

pm2-delete:
	@echo "Deleting PM2 process..."
	pm2 delete mcp-remote-server

# Utility commands
restart: stop start

status:
	@echo "Checking server status..."
	@curl -s http://localhost:3040/health || echo "Server not responding"

logs:
	@echo "Server logs (if running with PM2):"
	@pm2 logs mcp-remote-server --lines 50 || echo "Use 'make pm2-logs' for PM2 logs"
