#!/bin/bash

# Sync script to push changes from Git repository to WordPress.org SVN
# Usage: ./sync-to-svn.sh [commit-message]

set -e  # Exit on error

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Configuration
GIT_REPO="/Users/stevenmathew/Local Sites/test-origin/app/public/wp-content/plugins/SM-Easy-Post-Migrator"
SVN_REPO="$HOME/Desktop/sm-easy-post-migrator/trunk"

# Check if SVN repo exists
if [ ! -d "$SVN_REPO" ]; then
    echo -e "${RED}Error: SVN repository not found at $SVN_REPO${NC}"
    exit 1
fi

# Check if git repo exists
if [ ! -d "$GIT_REPO" ]; then
    echo -e "${RED}Error: Git repository not found at $GIT_REPO${NC}"
    exit 1
fi

echo -e "${GREEN}Starting sync from Git to SVN...${NC}"

# Get commit message from argument or use default
COMMIT_MSG="${1:-Sync changes from git repository}"

# Update SVN first
echo -e "${YELLOW}Updating SVN repository...${NC}"
cd "$(dirname "$SVN_REPO")"
svn update

# Files to exclude (development files that shouldn't be in SVN)
EXCLUDE_PATTERNS=(
    ".git"
    ".DS_Store"
    "*.swp"
    "*.swo"
    "*~"
    ".idea"
    ".vscode"
    "vendor"
    "composer.lock"
    "*.zip"
    "node_modules"
)

# Function to check if file should be excluded
should_exclude() {
    local file="$1"
    for pattern in "${EXCLUDE_PATTERNS[@]}"; do
        if [[ "$file" == *"$pattern"* ]]; then
            return 0
        fi
    done
    return 1
}

# Copy all files from git to svn (excluding .git and other dev files)
echo -e "${YELLOW}Copying files from Git to SVN...${NC}"
cd "$GIT_REPO"

# Find all files and copy them
find . -type f ! -path "./.git/*" | while read -r file; do
    # Remove leading ./
    file="${file#./}"
    
    # Check if should exclude
    if should_exclude "$file"; then
        continue
    fi
    
    # Get relative path
    rel_path="$file"
    target_file="$SVN_REPO/$rel_path"
    target_dir="$(dirname "$target_file")"
    
    # Create directory if it doesn't exist
    mkdir -p "$target_dir"
    
    # Copy file
    cp "$file" "$target_file"
    echo "  Copied: $file"
done

# Check for deleted files in SVN that should be removed
echo -e "${YELLOW}Checking for deleted files...${NC}"
cd "$SVN_REPO"
find . -type f ! -path "./.svn/*" | while read -r svn_file; do
    svn_file="${svn_file#./}"
    git_file="$GIT_REPO/$svn_file"
    
    # Skip if it's a file that should be excluded
    if should_exclude "$svn_file"; then
        continue
    fi
    
    # If file doesn't exist in git repo, mark for deletion
    if [ ! -f "$git_file" ]; then
        echo "  File removed: $svn_file"
        svn remove "$svn_file" 2>/dev/null || true
    fi
done

# Add new files to SVN
echo -e "${YELLOW}Adding new files to SVN...${NC}"
cd "$SVN_REPO"
svn add --force . 2>/dev/null | grep -v "^A.*\.svn" || true

# Show status
echo -e "${YELLOW}SVN Status:${NC}"
svn status | grep -v "^$" || echo "  No changes"

# Ask for confirmation before committing
echo ""
read -p "Do you want to commit these changes to SVN? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo -e "${YELLOW}Sync cancelled. Changes are staged but not committed.${NC}"
    exit 0
fi

# Commit to SVN
echo -e "${YELLOW}Committing to SVN...${NC}"
svn commit -m "$COMMIT_MSG"

echo -e "${GREEN}✓ Sync completed successfully!${NC}"
echo -e "${GREEN}Changes have been committed to WordPress.org SVN repository.${NC}"

