#!/bin/bash
#
# Initialize a new project directory with OpenCode configuration for full-stack development
# Cross-platform support for macOS and Linux
#
# Usage: ./init-project.sh <project-directory>
#
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Detect OS
detect_os() {
case "$(uname -s)" in
Darwin*) echo "macos" ;;
Linux*) echo "linux" ;;
CYGWIN*|MINGW*|MSYS*) echo "windows" ;;
*) echo "unknown" ;;
esac
}
OS=$(detect_os)
# Cross-platform readlink -f equivalent
resolve_path() {
local target="$1"
if [[ "$target" != /* ]]; then
target="$(pwd)/$target"
fi
if [ "$OS" = "macos" ]; then
if command -v python3 >/dev/null 2>&1; then
python3 -c "import os; print(os.path.realpath('$target'))"
elif command -v python >/dev/null 2>&1; then
python -c "import os; print(os.path.realpath('$target'))"
else
local dir
local base
if [ -d "$target" ]; then
(cd "$target" 2>/dev/null && pwd)
elif [ -f "$target" ]; then
dir=$(dirname "$target")
base=$(basename "$target")
(cd "$dir" 2>/dev/null && echo "$(pwd)/$base")
else
dir=$(dirname "$target")
base=$(basename "$target")
if [ -d "$dir" ]; then
echo "$(cd "$dir" && pwd)/$base"
else
mkdir -p "$dir"
echo "$(cd "$dir" && pwd)/$base"
fi
fi
fi
else
if [ -e "$target" ]; then
readlink -f "$target"
else
local dir=$(dirname "$target")
local base=$(basename "$target")
mkdir -p "$dir"
echo "$(readlink -f "$dir")/$base"
fi
fi
}
# Get the directory where this script is located
get_script_dir() {
local source="${BASH_SOURCE[0]}"
local dir
while [ -L "$source" ]; do
dir=$(dirname "$source")
source=$(readlink "$source")
[[ $source != /* ]] && source="$dir/$source"
done
dir=$(dirname "$source")
resolve_path "$dir"
}
SCRIPT_DIR="$(get_script_dir)"
# Function to print colored output
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[OK]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Show usage
show_usage() {
echo ""
echo "Usage: $0 <project-directory>"
echo ""
echo "Initialize a project directory with full-stack MCP configuration."
echo ""
echo "Examples:"
echo " $0 ~/projects/my-app"
echo " $0 /path/to/existing/project"
echo " $0 ./new-project"
echo ""
echo "This creates:"
echo " - opencode.json with auto-approved permissions"
echo " - .opencode/agent/ directory with full-stack agent files"
echo ""
}
# Check if project directory is provided
if [ $# -eq 0 ]; then
print_error "No project directory specified"
show_usage
exit 1
fi
# Handle --help flag
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
show_usage
exit 0
fi
PROJECT_DIR="$1"
# Convert to absolute path
PROJECT_DIR=$(resolve_path "$PROJECT_DIR")
echo ""
print_info "Initializing Full-Stack MCP configuration for: ${YELLOW}$PROJECT_DIR${NC}"
print_info "Detected OS: $OS"
echo ""
# Create project directory if it doesn't exist
if [ ! -d "$PROJECT_DIR" ]; then
print_warning "Directory doesn't exist. Creating: $PROJECT_DIR"
mkdir -p "$PROJECT_DIR"
print_success "Created directory"
fi
# Check if opencode.json already exists
CONFIG_FILE="$PROJECT_DIR/opencode.json"
if [ -f "$CONFIG_FILE" ]; then
print_warning "opencode.json already exists in $PROJECT_DIR"
read -p "Overwrite? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_info "Keeping existing configuration"
else
rm "$CONFIG_FILE"
fi
fi
# Create the opencode.json configuration if needed
if [ ! -f "$CONFIG_FILE" ]; then
print_info "Creating opencode.json with project-specific settings..."
cat > "$CONFIG_FILE" << 'EOF'
{
"$schema": "https://opencode.ai/config.json",
"permission": {
"edit": "allow",
"bash": "allow",
"webfetch": "allow"
},
"mcp": {
"sequential-thinking": {
"type": "local",
"enabled": true,
"command": ["npx", "-y", "@modelcontextprotocol/server-sequential-thinking"]
},
"context7": {
"type": "local",
"enabled": true,
"command": ["npx", "-y", "@upstash/context7-mcp"]
}
}
}
EOF
print_success "Created opencode.json at: $CONFIG_FILE"
fi
# Copy agent files (required for manager tools to work)
AGENTS_SRC="$SCRIPT_DIR/.opencode/agent"
AGENTS_DST="$PROJECT_DIR/.opencode/agent"
if [ -d "$AGENTS_SRC" ]; then
print_info "Copying OpenCode agents for full-stack manager tools..."
mkdir -p "$AGENTS_DST"
# Use cp with proper flags for both platforms
if [ "$OS" = "macos" ]; then
cp "$AGENTS_SRC"/*.md "$AGENTS_DST/" 2>/dev/null || true
else
cp -u "$AGENTS_SRC"/*.md "$AGENTS_DST/" 2>/dev/null || cp "$AGENTS_SRC"/*.md "$AGENTS_DST/" 2>/dev/null || true
fi
AGENT_COUNT=$(find "$AGENTS_DST" -name "*.md" 2>/dev/null | wc -l | tr -d ' ')
if [ "$AGENT_COUNT" -gt 0 ]; then
print_success "Copied $AGENT_COUNT agent files to .opencode/agent/"
else
print_warning "No agent files were copied"
fi
else
print_warning "No agent files found in $AGENTS_SRC"
fi
# Optionally copy WORKING_INSTRUCTIONS.md
if [ -f "$SCRIPT_DIR/WORKING_INSTRUCTIONS.md" ]; then
if [ ! -f "$PROJECT_DIR/WORKING_INSTRUCTIONS.md" ]; then
print_info "Would you like to copy WORKING_INSTRUCTIONS.md to your project?"
echo " This file contains guidelines for the OpenCode model."
read -p "Copy? (Y/n): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
cp "$SCRIPT_DIR/WORKING_INSTRUCTIONS.md" "$PROJECT_DIR/"
print_success "Copied WORKING_INSTRUCTIONS.md"
fi
fi
fi
echo ""
print_success "Project initialization complete!"
echo ""
print_info "Configuration details:"
echo " Project: $PROJECT_DIR"
echo " Config file: $CONFIG_FILE"
echo " Permissions: Full auto-approval within project directory"
echo ""
print_info "Available managers:"
echo " - frontend-manager (React, Vue, CSS, accessibility)"
echo " - backend-manager (Node.js, Express, FastAPI)"
echo " - database-manager (PostgreSQL, MongoDB, Prisma)"
echo " - api-manager (REST, GraphQL, WebSocket)"
echo " - devops-manager (Docker, CI/CD, deployment)"
echo " - testing-manager (Unit, Integration, E2E tests)"
echo " - security-manager (Auth, OWASP, validation)"
echo " - performance-manager (Caching, optimization)"
echo ""
print_info "To use OpenCode with this project:"
echo " ${YELLOW}cd $PROJECT_DIR${NC}"
echo " ${YELLOW}opencode run -m cerebras/zai-glm-4.6 \"your prompt here\"${NC}"
echo ""