#!/bin/bash
# ADMS Sync Agent Setup Script for Linux/Mac
# This script installs dependencies and creates configuration

echo ""
echo "========================================"
echo "ADMS Sync Agent Setup"
echo "========================================"
echo ""

# Check if Python is installed
if ! command -v python3 &> /dev/null; then
    echo "ERROR: Python3 is not installed"
    echo "Install with: apt-get install python3 python3-pip (Ubuntu/Debian)"
    echo "             brew install python3 (Mac)"
    exit 1
fi

echo "✓ Python found"
python3 --version

# Create virtual environment (REQUIRED for Ubuntu 24.04+)
echo ""
echo "Creating virtual environment (required for Ubuntu 24.04+)..."
python3 -m venv venv
if [ $? -ne 0 ]; then
    echo "ERROR: Failed to create virtual environment"
    exit 1
fi

echo "✓ Virtual environment created"

# Activate virtual environment
source venv/bin/activate
echo "✓ Virtual environment activated"

# Install dependencies
echo ""
echo "Installing dependencies..."
pip install -r requirements.txt
if [ $? -ne 0 ]; then
    echo "ERROR: Failed to install dependencies"
    echo ""
    echo "Troubleshooting:"
    echo "- Make sure you have python3-full installed: apt install python3-full"
    echo "- Try again with: pip install --upgrade pip setuptools wheel"
    exit 1
fi

echo "✓ Dependencies installed successfully"

# Create config
echo ""
echo "Creating configuration file..."
python3 sync_agent.py --create-config
if [ $? -ne 0 ]; then
    echo "ERROR: Failed to create configuration"
    exit 1
fi

echo "✓ Configuration created"

# Show next steps
echo ""
echo "========================================"
echo "Setup Complete!"
echo "========================================"
echo ""
echo "IMPORTANT: Activate virtual environment before running agent"
echo ""
echo "Next steps:"
echo "1. Activate venv: source venv/bin/activate"
echo "2. Edit sync_agent_config.json"
echo "3. Set your cloud URL"
echo "4. Set device IPs and passwords"
echo ""
echo "Then run:"
echo "  python3 sync_agent.py --device 192.168.1.100"
echo "  (to test with one device)"
echo ""
echo "Or run:"
echo "  python3 sync_agent.py"
echo "  (to start the sync agent)"
echo ""
echo "To run at startup, create systemd service:"
echo "  See SYNC_AGENT_SETUP.md for details"
echo ""
