#!/bin/bash
echo "🚀 Starting cPanel Deployment Process..."

# Error handling: Stop on first error
set -e
trap 'echo "❌ Error occurred at line $LINENO. Deployment failed!"' ERR

echo "📂 Current directory: $(pwd)"

echo "⚠️ Skipping database setup in this script. Run it manually after sourcing Node.js."

echo "🔧 Fixing storage and execution permissions..."
# Ensure permissions are correct for directories and files
find . -type d -exec chmod 0755 {} \;
find . -type f -exec chmod 0644 {} \;
# Give execution permission to scripts
find . -type f -name "*.sh" -exec chmod +x {} \;
if [ -f "app.cjs" ]; then
  chmod +x app.cjs
fi
echo "✅ Permissions fixed!"

echo ""
echo "🎉 Deployment script completed successfully!"
echo "--------------------------------------------------------"
echo "NEXT STEPS in cPanel:"
echo "1. Go to 'Setup Node.js App' in cPanel."
echo "2. Create a new Application (or edit existing one)."
echo "   - Node.js version: 18 or 20 (recommended)"
echo "   - Application mode: Production"
echo "   - Application root: /billing.patchopt.com"
echo "   - Application URL: billing.patchopt.com"
echo "   - Application startup file: app.cjs"
echo "3. Save and click 'Start App' (or Restart)."
echo "--------------------------------------------------------"
