#!/bin/bash
# OpsCore Demo Launcher — macOS
# Double-click this file to launch the OpsCore demo.

clear
echo ""
echo "  ============================================"
echo "    OpsCore — One-Click Demo Launcher"
echo "    Private Business Operating System"
echo "  ============================================"
echo ""
echo "  Starting demo setup..."
echo ""

# Script directory (where this .command lives)
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
DEMO_DIR="$TMPDIR/opscore_demo"

mkdir -p "$DEMO_DIR"

# Copy demo files
cp -f "$SCRIPT_DIR/offline.html" "$DEMO_DIR/index.html" 2>/dev/null || true
cp -f "$SCRIPT_DIR/offline.html" "$DEMO_DIR/offline.html" 2>/dev/null || true

# Check for Python
if command -v python3 &>/dev/null; then
    PYTHON=python3
elif command -v python &>/dev/null; then
    PYTHON=python
else
    echo "  Python not found. Opening offline demo directly..."
    echo ""
    open "$SCRIPT_DIR/offline.html"
    echo "  [OK] Offline demo opened in browser."
    echo "  All 19 modules available. No server needed."
    echo ""
    read -p "  Press Enter to close..."
    exit 0
fi

PORT=8080

# Write Python server
cat > "$DEMO_DIR/serve.py" << 'PYEOF'
import http.server, webbrowser, os, sys

PORT = 8080
DEMO_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(DEMO_DIR)

class DemoHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path in ('/', ''):
            self.path = '/offline.html'
        super().do_GET()
    def log_message(self, *args):
        pass

print("  OpsCore demo: http://localhost:8080")
print("  Press Ctrl+C to stop.")
webbrowser.open('http://localhost:8080')
http.server.HTTPServer(('', PORT), DemoHandler).serve_forever()
PYEOF

echo "  [OK] Demo files ready"
echo "  [OK] Starting HTTP server on port 8080"
echo "  [OK] Opening browser..."
echo ""
echo "  License: FREE_DEMO_UNLIMITED"
echo "  Dataset: Shelter Housing Group"
echo "  Modules: All 19 unlocked"
echo ""
echo "  Press Ctrl+C to stop the demo server."
echo ""

$PYTHON "$DEMO_DIR/serve.py"
