#!/bin/bash

# WordPress.org SVN Deployment Script
# This script helps deploy your plugin to WordPress.org SVN repository

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 - UPDATE THESE VALUES
SVN_URL="https://plugins.svn.wordpress.org/dp-admin-access-menu/"
PLUGIN_VERSION="1.0.0"
LOCAL_PLUGIN_DIR="/Users/priyanshukast/Downloads/dp-admin-menu-access"
SVN_WORKING_DIR="dp-admin-access-menu-svn"

echo -e "${GREEN}WordPress.org SVN Deployment Script${NC}"
echo "=========================================="
echo ""
echo -e "${YELLOW}⚠️  IMPORTANT: Wait at least 1 hour after plugin approval${NC}"
echo -e "${YELLOW}   before attempting to commit. Your account needs time${NC}"
echo -e "${YELLOW}   to be granted commit access.${NC}"
echo ""

# Check if SVN is installed
if ! command -v svn &> /dev/null; then
    echo -e "${RED}Error: SVN is not installed.${NC}"
    echo "Install it with: brew install subversion"
    exit 1
fi

# Check if local plugin directory exists
if [ ! -d "$LOCAL_PLUGIN_DIR" ]; then
    echo -e "${RED}Error: Local plugin directory not found: $LOCAL_PLUGIN_DIR${NC}"
    exit 1
fi

# Step 1: Checkout or update SVN repository
if [ -d "$SVN_WORKING_DIR" ]; then
    echo -e "${YELLOW}SVN directory exists. Updating...${NC}"
    cd "$SVN_WORKING_DIR"
    svn update
    cd ..
else
    echo -e "${GREEN}Checking out SVN repository...${NC}"
    svn checkout "$SVN_URL" "$SVN_WORKING_DIR"
fi

# Step 2: Copy plugin files to trunk
echo -e "${GREEN}Copying plugin files to trunk...${NC}"
cd "$SVN_WORKING_DIR/trunk"

# Remove existing files (except .svn)
find . -type f ! -path '*/\.svn/*' -delete
find . -type d ! -path '*/\.svn/*' ! -path '.' -exec rm -rf {} + 2>/dev/null || true

# Copy files from local plugin directory
cp -r "$LOCAL_PLUGIN_DIR"/* .

# Remove files that shouldn't be in SVN
rm -rf .git .gitignore .DS_Store node_modules *.md 2>/dev/null || true
# Keep readme.txt (it's required)
if [ ! -f "readme.txt" ]; then
    echo -e "${RED}Warning: readme.txt not found!${NC}"
fi

# Step 3: Handle assets
echo -e "${GREEN}Setting up assets...${NC}"
cd ..
mkdir -p assets

# Copy screenshots if they exist
if [ -d "$LOCAL_PLUGIN_DIR/assets/screenshots" ]; then
    cp "$LOCAL_PLUGIN_DIR/assets/screenshots"/*.png assets/ 2>/dev/null || true
    echo "Screenshots copied to assets/"
fi

# Copy icon if it exists
if [ -f "$LOCAL_PLUGIN_DIR/assets/screenshots/icon-256x256.png" ]; then
    cp "$LOCAL_PLUGIN_DIR/assets/screenshots/icon-256x256.png" assets/
    echo "Icon copied to assets/"
fi

# Step 4: Create version tag if it doesn't exist
if [ ! -d "tags/$PLUGIN_VERSION" ]; then
    echo -e "${GREEN}Creating version tag $PLUGIN_VERSION...${NC}"
    svn copy trunk "tags/$PLUGIN_VERSION"
else
    echo -e "${YELLOW}Tag $PLUGIN_VERSION already exists. Updating...${NC}"
    # Copy files to existing tag
    rm -rf "tags/$PLUGIN_VERSION"/*
    cp -r trunk/* "tags/$PLUGIN_VERSION/"
fi

# Step 5: Add new files to SVN
echo -e "${GREEN}Adding files to SVN...${NC}"
svn add trunk/* --force 2>/dev/null || true
svn add tags/$PLUGIN_VERSION/* --force 2>/dev/null || true
svn add assets/* --force 2>/dev/null || true

# Step 6: Show status
echo ""
echo -e "${GREEN}SVN Status:${NC}"
svn status

echo ""
echo -e "${YELLOW}=========================================="
echo "Ready to commit!"
echo "==========================================${NC}"
echo ""
echo "Review the changes above, then run:"
echo -e "${GREEN}cd $SVN_WORKING_DIR${NC}"
echo -e "${GREEN}svn commit -m \"Initial release version $PLUGIN_VERSION\"${NC}"
echo ""
echo -e "${YELLOW}Note: You'll be prompted for your WordPress.org credentials${NC}"
echo ""

