#!/bin/bash
# WP Plugin Deploy — Usage: ./deploy.sh [version]

GIT_DIR="/Users/nazim/Local Sites/test/app/public/wp-content/plugins/rapidpress"
SVN_TRUNK="/Users/nazim/Documents/Web Development/SVN/rapidpress/trunk"
WP_USER="nazim848"
WP_PASS="svn_nmaAEPmeHbE8n0dvlBmD8cQrbKvCcsgD75373157"
VERSION=$1

echo "Syncing files..."
rsync -a --delete "$GIT_DIR/" "$SVN_TRUNK/" \
  --exclude='.git' \
  --exclude='node_modules' \
  --exclude='.DS_Store' \
  --exclude='*.map' \
  --exclude='package*.json' \
  --exclude='webpack.config.js' \
  --exclude='README.md' \
  --exclude='AGENTS.md' \
  --exclude='.cursor' \
  --exclude='.cursorignore' \
  --exclude='.gitignore'


echo "Adding new files..."
svn add "$SVN_TRUNK"/* --force --quiet

echo "Removing deleted files..."
svn status "$SVN_TRUNK" | grep '^!' | awk '{print $2}' | xargs -I{} svn delete "{}"

echo "Committing to trunk..."
svn commit "$SVN_TRUNK" -m "Deploy${VERSION:+ $VERSION}" --username "$WP_USER" --password "$WP_PASS" --no-auth-cache

if [ -n "$VERSION" ]; then
  SVN_ROOT=$(svn info "$SVN_TRUNK" | grep 'Repository Root' | awk '{print $NF}')
  SLUG=$(basename "$SVN_ROOT")
  echo "Tagging $VERSION..."
  svn copy "https://plugins.svn.wordpress.org/rapidpress/trunk" \
           "https://plugins.svn.wordpress.org/rapidpress/tags/$VERSION" \
           -m "Tagging $VERSION" --username "$WP_USER" --password "$WP_PASS" --no-auth-cache
fi

echo "Done!"