#!/bin/bash

# BestCDN Installation Script

echo "BestCDN Installation Script"
echo "=========================="

# Check if Python 3 is installed
if ! command -v python3 &> /dev/null; then
    echo "Error: Python 3 is not installed. Please install Python 3.7+ first."
    exit 1
fi

echo "✓ Python 3 found"

# Create virtual environment
echo "Creating virtual environment..."
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
echo "Activating virtual environment..."
source venv/bin/activate

# Upgrade pip
echo "Upgrading pip..."
pip install --upgrade pip

# Install dependencies
echo "Installing dependencies..."
pip install -r requirements.txt

if [ $? -ne 0 ]; then
    echo "Error: Failed to install dependencies"
    exit 1
fi

echo "✓ Dependencies installed"

# Create necessary directories
echo "Creating directories..."
mkdir -p ip_lists results logs

echo "✓ Directories created"

# Create example IP list files
echo "Creating example IP list files..."
mkdir -p ip_lists

# Create example files if they don't exist
for provider in cloudflare fastly edgeone esa; do
    if [ ! -f "ip_lists/${provider}.txt" ]; then
        echo "# ${provider} IP list - Replace with your actual IPs" > "ip_lists/${provider}.txt"
        echo "# Example: 192.168.1.1" >> "ip_lists/${provider}.txt"
        echo "# Example: 10.0.0.0/24" >> "ip_lists/${provider}.txt"
        echo "Created example file: ip_lists/${provider}.txt"
    else
        echo "File already exists: ip_lists/${provider}.txt"
    fi
done

echo ""
echo "Installation completed successfully!"
echo ""
echo "To use BestCDN:"
echo "1. Activate the virtual environment:"
echo "   source venv/bin/activate"
echo ""
echo "2. Edit IP list files in ip_lists/ directory"
echo ""
echo "3. Run the example:"
echo "   python3 example_usage.py"
echo ""
echo "For more information, see README.md"
echo ""
echo "IMPORTANT: Always activate the virtual environment before running:"
echo "   source venv/bin/activate"