#!/bin/bash
# Wrapper to execute Big Driver Manager GUI.
# Resolves the install directory by picking the most recent Python
# site-packages install, falling back to /usr/share or the dev tree.

set -o pipefail

find_install_dir() {
    local candidate
    # Newest Python 3.x site-packages first.
    while IFS= read -r candidate; do
        [[ -e "${candidate}" ]] || continue
        printf '%s\n' "${candidate}"
        return 0
    done < <(printf '%s\n' /usr/lib/python3.*/site-packages/big-driver-manager | sort -Vr)

    if [[ -e /usr/share/big-driver-manager ]]; then
        printf '%s\n' /usr/share/big-driver-manager
        return 0
    fi

    local self_dir
    self_dir="$(dirname "$(readlink -f "$0")")"
    if [[ -e "${self_dir}/../share/big-driver-manager" ]]; then
        printf '%s\n' "${self_dir}/../share/big-driver-manager"
        return 0
    fi

    return 1
}

BIG_DRIVER_MANAGER_DIR="$(find_install_dir)" || {
    echo "big-driver-manager: installation directory not found" >&2
    exit 1
}

exec python3 "${BIG_DRIVER_MANAGER_DIR}/main.py" "$@"
