#!/bin/bash
#
# Upload Simple Installer Script
# This script uploads the simple-installer.php and plugin zip file to the server
#

# 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
SIMPLE_INSTALLER="simple-installer.php"
PLUGIN_ZIP="plugin-backups/hvac-community-events-updated.zip"
REMOTE_DIR="/home/974670.cloudwaysapps.com/uberrxmprk/public_html"

# 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 info
echo "=============================================="
echo "Uploading Simple Installer"
echo "=============================================="
echo "Uploading to: ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}"
echo "Remote path: ${REMOTE_DIR}"
echo "=============================================="
echo

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

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

# Upload the installer and plugin
echo "Step 2: Uploading simple installer..."
${SCP_CMD} "${SIMPLE_INSTALLER}" ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}:"${REMOTE_DIR}/"

echo "Step 3: Uploading plugin package..."
${SCP_CMD} "${PLUGIN_ZIP}" ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}:"${REMOTE_DIR}/hvac-community-events-updated.zip"

# Fix file permissions
echo "Step 4: Setting file permissions..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "chmod 644 '${REMOTE_DIR}/${SIMPLE_INSTALLER}' '${REMOTE_DIR}/hvac-community-events-updated.zip'"

# Create .env file with Zoho credentials
echo "Step 5: Creating .env file with Zoho credentials..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "echo 'ZOHO_CLIENT_ID=${ZOHO_CLIENT_ID}' > '${REMOTE_DIR}/.env' && echo 'ZOHO_CLIENT_SECRET=${ZOHO_CLIENT_SECRET}' >> '${REMOTE_DIR}/.env' && echo 'UPSKILL_STAGING_URL=https://upskill-staging.measurequick.com/' >> '${REMOTE_DIR}/.env' && chmod 640 '${REMOTE_DIR}/.env'"

echo "Step 6: Verifying files..."
${SSH_CMD} ${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP} "ls -la '${REMOTE_DIR}/${SIMPLE_INSTALLER}' '${REMOTE_DIR}/hvac-community-events-updated.zip' '${REMOTE_DIR}/.env'"

echo
echo "=============================================="
echo "Upload complete!"
echo "=============================================="
echo "Now access the installer at:"
echo "https://upskill-staging.measurequick.com/${SIMPLE_INSTALLER}?key=upskill2025"
echo "=============================================="