#!/bin/bash
# Install Python dependencies for Homebridge Solar Monitor Plugin

echo "=== Installing Python Dependencies ==="

# Check if Python 3 is installed
if ! command -v python3 &> /dev/null; then
    echo "❌ Python 3 not found. Installing..."
    sudo apt-get update
    sudo apt-get install -y python3 python3-pip
fi

# Check if pip is installed
if ! command -v pip3 &> /dev/null; then
    echo "❌ pip3 not found. Installing..."
    sudo apt-get install -y python3-pip
fi

# Install Python dependencies
echo "Installing Python packages..."
pip3 install -r requirements.txt

# Install Chrome/Chromium for web scraping
echo "Installing Chrome/Chromium..."
if command -v apt-get &> /dev/null; then
    # Debian/Ubuntu/Raspberry Pi
    sudo apt-get update
    sudo apt-get install -y chromium-browser
elif command -v yum &> /dev/null; then
    # CentOS/RHEL
    sudo yum install -y chromium
elif command -v brew &> /dev/null; then
    # macOS
    brew install --cask google-chrome
else
    echo "⚠️  Please install Chrome/Chromium manually"
fi

# Download ChromeDriver for ARM (Raspberry Pi)
if [[ $(uname -m) == "arm"* ]]; then
    echo "Downloading ChromeDriver for ARM..."
    wget -q https://chromedriver.storage.googleapis.com/138.0.7204.49/chromedriver_linux64.zip
    unzip -q chromedriver_linux64.zip
    chmod +x chromedriver
    rm chromedriver_linux64.zip
    echo "✅ ChromeDriver installed for ARM"
fi

echo "✅ Dependencies installed successfully!"
echo ""
echo "Next steps:"
echo "1. Restart Homebridge: sudo systemctl restart homebridge"
echo "2. Check logs: sudo journalctl -u homebridge -f"
echo "3. Configure plugin in Homebridge UI" 