#!/bin/bash

# Script to prepare for plugin update by cleaning up the old version of the plugin
# This will ensure the old broken files are removed before installing the new version

# Set variables
PLUGIN_NAME="hvac-community-events"
PLUGIN_ZIP="/Users/ben/dev/upskill-event-manager/wordpress-dev/plugin-backups/${PLUGIN_NAME}.zip"
INSTALL_INSTRUCTIONS="/Users/ben/dev/upskill-event-manager/wordpress-dev/plugin-backups/INSTALL-INSTRUCTIONS.md"

# Check if the plugin ZIP exists
if [ ! -f "$PLUGIN_ZIP" ]; then
  echo "Error: Plugin ZIP file not found at $PLUGIN_ZIP"
  echo "Please run deploy-fixed-plugin.sh first to create the plugin package."
  exit 1
fi

# Create installation instructions
cat > "$INSTALL_INSTRUCTIONS" << EOF
# Installation Instructions for Fixed HVAC Community Events Plugin

## Option 1: Through WordPress Admin (Recommended)

1. **Deactivate current plugin:**
   - Go to WordPress Admin → Plugins
   - Deactivate "HVAC Community Events" plugin

2. **Delete current plugin:**
   - In the same Plugins page, click "Delete" under HVAC Community Events
   - Confirm deletion

3. **Install new version:**
   - Go to WordPress Admin → Plugins → Add New → Upload Plugin
   - Select the file: \`${PLUGIN_NAME}.zip\`
   - Click "Install Now"
   - After installation completes, click "Activate Plugin"

## Option 2: Via FTP

1. **Backup your site first** (important!)

2. **FTP Access:**
   - Connect to your server via FTP
   - Navigate to \`wp-content/plugins/\` directory

3. **Remove old plugin:**
   - Delete the \`${PLUGIN_NAME}\` directory completely

4. **Upload new plugin:**
   - Upload the \`${PLUGIN_NAME}.zip\` file
   - Extract it on the server (or extract locally and upload the folder)

5. **Activate plugin:**
   - Go to WordPress Admin → Plugins
   - Activate "HVAC Community Events" plugin

## After Installation

1. Test the Zoho CRM Sync functionality
2. Verify all site features are working correctly
3. Delete this ZIP file and instructions from the server for security

EOF

echo "Created installation instructions at: $INSTALL_INSTRUCTIONS"
echo ""
echo "IMPORTANT STEPS FOR CLEAN INSTALLATION:"
echo "--------------------------------------"
echo "1. Deactivate the current plugin"
echo "2. Delete the current plugin completely"
echo "3. Install the new plugin package"
echo "4. Activate the new plugin"
echo ""
echo "See $INSTALL_INSTRUCTIONS for detailed instructions."