#!/bin/bash
# Epic Travel & Expeditions - cPanel Deployment Package Creator
# This script creates a deployment-ready package for cPanel

echo "======================================"
echo "Epic Travel cPanel Package Creator"
echo "======================================"
echo ""

# Set variables
PACKAGE_NAME="epic-travel-cpanel-$(date +%Y%m%d-%H%M%S)"
PACKAGE_DIR="/app/cpanel_deployment/$PACKAGE_NAME"
BACKEND_SOURCE="/app/backend"
FRONTEND_SOURCE="/app/frontend"

echo "Creating package directory: $PACKAGE_DIR"
mkdir -p "$PACKAGE_DIR"/{backend,frontend}

# Copy backend files (excluding unnecessary files)
echo "Copying backend files..."
rsync -av --exclude='__pycache__' \
         --exclude='*.pyc' \
         --exclude='.env' \
         --exclude='venv' \
         --exclude='node_modules' \
         "$BACKEND_SOURCE/" "$PACKAGE_DIR/backend/"

# Copy MySQL-specific files
echo "Adding MySQL database configuration..."
cp /app/cpanel_deployment/backend/database.py "$PACKAGE_DIR/backend/"
cp /app/cpanel_deployment/backend/requirements.txt "$PACKAGE_DIR/backend/"
cp /app/cpanel_deployment/backend/.env.example "$PACKAGE_DIR/backend/"

# Build frontend for production
echo "Building frontend for production..."
cd "$FRONTEND_SOURCE"
yarn build

# Copy frontend build
echo "Copying frontend build..."
cp -r "$FRONTEND_SOURCE/build/"* "$PACKAGE_DIR/frontend/"
cp /app/cpanel_deployment/frontend/.htaccess "$PACKAGE_DIR/frontend/"

# Copy documentation and setup files
echo "Copying documentation..."
cp /app/cpanel_deployment/INSTALLATION.md "$PACKAGE_DIR/"
cp /app/cpanel_deployment/database_schema.sql "$PACKAGE_DIR/"
cp /app/cpanel_deployment/setup_admin.py "$PACKAGE_DIR/"

# Create README in package root
cat > "$PACKAGE_DIR/README.txt" << 'EOF'
EPIC TRAVEL & EXPEDITIONS - cPanel Deployment Package
=====================================================

This package contains everything needed to deploy Epic Travel & Expeditions 
to a cPanel server with MySQL database.

CONTENTS:
---------
- backend/          Python FastAPI backend with MySQL support
- frontend/         React production build
- database_schema.sql   MySQL database schema
- setup_admin.py    Admin password hash generator
- INSTALLATION.md   Complete installation guide

QUICK START:
------------
1. Read INSTALLATION.md for complete instructions
2. Create MySQL database in cPanel
3. Import database_schema.sql
4. Configure backend/.env with database credentials
5. Upload files to cPanel
6. Setup Python app in cPanel
7. Test the installation

For detailed instructions, see INSTALLATION.md

Contact: advisor@epictravelexpeditions.com
Phone: +1 (817) 266-2022
EOF

# Create compressed archive
echo "Creating compressed archive..."
cd /app/cpanel_deployment
tar -czf "$PACKAGE_NAME.tar.gz" "$PACKAGE_NAME"
zip -r "$PACKAGE_NAME.zip" "$PACKAGE_NAME" -q

# Calculate sizes
TAR_SIZE=$(du -h "$PACKAGE_NAME.tar.gz" | cut -f1)
ZIP_SIZE=$(du -h "$PACKAGE_NAME.zip" | cut -f1)

echo ""
echo "======================================"
echo "Package Created Successfully!"
echo "======================================"
echo ""
echo "Package Location:"
echo "  Directory: /app/cpanel_deployment/$PACKAGE_NAME"
echo "  Tar.gz:    /app/cpanel_deployment/$PACKAGE_NAME.tar.gz ($TAR_SIZE)"
echo "  Zip:       /app/cpanel_deployment/$PACKAGE_NAME.zip ($ZIP_SIZE)"
echo ""
echo "Next Steps:"
echo "  1. Download the package (tar.gz or zip)"
echo "  2. Read INSTALLATION.md"
echo "  3. Follow the installation steps"
echo ""
echo "======================================"
