#!/bin/bash
# Setup script for deploying QuickBooks POS Help with Nginx

echo "============================================================"
echo "QuickBooks POS Help - Production Deployment Setup"
echo "============================================================"
echo ""

# Check if nginx is installed
if ! command -v nginx &> /dev/null; then
    echo "Installing Nginx..."
    sudo apt update
    sudo apt install -y nginx
else
    echo "✓ Nginx is already installed"
fi

# Copy nginx configuration
echo ""
echo "Setting up Nginx configuration..."
sudo cp nginx_setup.conf /etc/nginx/sites-available/qbpos-help

# Create symbolic link
if [ ! -L /etc/nginx/sites-enabled/qbpos-help ]; then
    sudo ln -s /etc/nginx/sites-available/qbpos-help /etc/nginx/sites-enabled/
    echo "✓ Configuration linked"
else
    echo "✓ Configuration already linked"
fi

# Test nginx configuration
echo ""
echo "Testing Nginx configuration..."
sudo nginx -t

if [ $? -eq 0 ]; then
    echo ""
    echo "Restarting Nginx..."
    sudo systemctl restart nginx
    sudo systemctl enable nginx
    
    echo ""
    echo "============================================================"
    echo "✓ Deployment Complete!"
    echo "============================================================"
    echo ""
    echo "Your site is now accessible at:"
    echo "  http://$(hostname -I | awk '{print $1}')/POS_Help.html"
    echo ""
    echo "Optimizations enabled:"
    echo "  ✓ Static file caching (1 year)"
    echo "  ✓ Gzip compression"
    echo "  ✓ Minimal logging"
    echo "  ✓ High performance for 100,000+ users"
    echo ""
    echo "To view logs:"
    echo "  sudo tail -f /var/log/nginx/qbpos-error.log"
    echo ""
else
    echo ""
    echo "⚠ Configuration test failed. Please check the error above."
fi
