#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e

# --- Check for required system packages ---
MISSING_PKGS=""

if ! dpkg -s libxcb-cursor0 >/dev/null 2>&1; then
    MISSING_PKGS+="libxcb-cursor0 "
fi

if ! dpkg -s ffmpeg >/dev/null 2>&1; then
    MISSING_PKGS+="ffmpeg "
fi

if [ -n "$MISSING_PKGS" ]; then
    echo "The following system packages are missing: ${MISSING_PKGS}"
    echo "Requesting sudo privileges to install missing packages..."
    sudo -v
    sudo apt update
    sudo apt install -y $MISSING_PKGS
fi

# --- Setup virtual environment if not already present ---
if [ ! -d ".venv" ]; then
    echo "Virtual environment not found. Creating .venv..."
    python3 -m venv .venv
    echo "Activating virtual environment..."
    source .venv/bin/activate
    echo "Upgrading pip..."
    pip install --upgrade pip
    if [ -f "requirements.txt" ]; then
        echo "Installing requirements from requirements.txt..."
        pip install -r requirements.txt
    else
        echo "requirements.txt not found. Skipping package installation."
    fi
else
    echo "Virtual environment found. Activating .venv..."
    source .venv/bin/activate
fi

# --- Run the Python script ---
python wancutter.py
