#!/bin/bash
#
# HVAC Community Events Plugin Deployment Script
# This script deploys the updated plugin with Zoho CRM integration fixes
#

# Exit on error
set -e

# Load environment variables
if [ -f ".env" ]; then
    source .env
else
    echo "Error: .env file not found"
    exit 1
fi

# Define variables
PLUGIN_NAME="hvac-community-events"
PLUGIN_ZIP="plugin-backups/hvac-community-events-updated.zip"
PLUGIN_DIR="${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html}/wp-content/plugins/${PLUGIN_NAME}"
BACKUP_DIR="${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html}/wp-content/plugins/${PLUGIN_NAME}.bak.$(date +%Y%m%d%H%M%S)"
TEMP_DIR="${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html}/wp-content/upgrade/temp-${PLUGIN_NAME}"

# Check if required variables are set
if [ -z "${UPSKILL_STAGING_SSH_USER}" ] || [ -z "${UPSKILL_STAGING_IP}" ]; then
    echo "Error: Required environment variables not set. Please check your .env file."
    exit 1
fi

# Display deployment info
echo "=============================================="
echo "HVAC Community Events Plugin Deployment"
echo "=============================================="
echo "Deploying to: ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}"
echo "Plugin path: ${PLUGIN_DIR}"
echo "Backup path: ${BACKUP_DIR}"
echo "=============================================="
echo

# Check if plugin zip exists
if [ ! -f "${PLUGIN_ZIP}" ]; then
    echo "Error: Plugin ZIP file not found at ${PLUGIN_ZIP}"
    exit 1
fi

# Define SSH command with password if available
if [ -n "${UPSKILL_STAGING_PASS}" ]; then
    SSH_CMD="sshpass -p \"${UPSKILL_STAGING_PASS}\" ssh -o StrictHostKeyChecking=no"
else
    SSH_CMD="ssh -o StrictHostKeyChecking=no"
fi

# Define SCP command with password if available
if [ -n "${UPSKILL_STAGING_PASS}" ]; then
    SCP_CMD="sshpass -p \"${UPSKILL_STAGING_PASS}\" scp -o StrictHostKeyChecking=no"
else
    SCP_CMD="scp -o StrictHostKeyChecking=no"
fi

# Create deployment steps
echo "Step 1: Checking server connection..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "echo Server connection successful."

echo "Step 2: Deactivating existing plugin..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "cd ${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html} && wp plugin deactivate ${PLUGIN_NAME} --skip-plugins --skip-themes || echo 'Plugin not active or not found'"

echo "Step 3: Creating backup of existing plugin..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "if [ -d '${PLUGIN_DIR}' ]; then mv '${PLUGIN_DIR}' '${BACKUP_DIR}'; echo 'Backup created at ${BACKUP_DIR}'; else echo 'No existing plugin to backup'; fi"

echo "Step 4: Creating temporary directory..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "mkdir -p '${TEMP_DIR}'"
# Verify the directory was created
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "ls -la '$(dirname ${TEMP_DIR})' && echo 'Temp directory created successfully'"

echo "Step 5: Uploading plugin package..."
# First upload to home directory, then move to temp dir
${SCP_CMD} "${PLUGIN_ZIP}" ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}:~/plugin.zip
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "mv ~/plugin.zip '${TEMP_DIR}/'"

echo "Step 6: Extracting plugin package..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "cd '${TEMP_DIR}' && unzip -q plugin.zip && mv ${PLUGIN_NAME}-updated '${PLUGIN_DIR}'"

echo "Step 7: Fixing file permissions..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "chmod -R 755 '${PLUGIN_DIR}'"

echo "Step 8: Cleaning up temporary files..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "rm -rf '${TEMP_DIR}'"

echo "Step 9: Creating .env file with Zoho credentials..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "echo 'ZOHO_CLIENT_ID=${ZOHO_CLIENT_ID}' > '${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html}/.env' && echo 'ZOHO_CLIENT_SECRET=${ZOHO_CLIENT_SECRET}' >> '${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html}/.env' && echo 'UPSKILL_STAGING_URL=https://upskill-staging.measurequick.com/' >> '${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html}/.env' && chmod 640 '${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html}/.env'"

echo "Step 10: Activating plugin..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "cd ${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html} && wp plugin activate ${PLUGIN_NAME} --skip-plugins --skip-themes"

echo "Step 11: Flushing rewrite rules..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "cd ${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html} && wp rewrite flush --skip-plugins --skip-themes"

echo "Step 12: Verifying installation..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "cd ${UPSKILL_STAGING_PATH:-/home/974670.cloudwaysapps.com/uberrxmprk/public_html} && wp plugin is-active ${PLUGIN_NAME} --skip-plugins --skip-themes && echo 'Plugin successfully activated' || echo 'WARNING: Plugin activation failed'"

echo
echo "=============================================="
echo "Deployment completed!"
echo "=============================================="
echo "Don't forget to verify the Zoho CRM integration at:"
echo "https://upskill-staging.measurequick.com/wp-admin/admin.php?page=hvac-zoho-sync"
echo "=============================================="