#!/bin/sh

# On Ubuntu 26.04+, the default sudo-rs does not support wildcards in
# sudoers command arguments. Switch to sudo-legacy (sudo.ws) BEFORE
# installing the sudoers file, so dpkg can validate it successfully.

if [ -f /etc/os-release ]; then
    . /etc/os-release
    if [ "$ID" = "ubuntu" ]; then
        VERSION_ID_MAJOR=$(echo "$VERSION_ID" | cut -d. -f1)
        if [ -n "$VERSION_ID_MAJOR" ] && [ "$VERSION_ID_MAJOR" -ge 26 ]; then
            if [ -f /usr/bin/sudo.ws ] && [ -x /usr/bin/sudo.ws ]; then
                CURRENT_SUDO=$(readlink -f /etc/alternatives/sudo 2>/dev/null || true)
                if [ "$CURRENT_SUDO" != "/usr/bin/sudo.ws" ]; then
                    echo "Switching sudo from sudo-rs to sudo-legacy for wildcard compatibility..."
                    update-alternatives --set sudo /usr/bin/sudo.ws || true
                fi
            fi
        fi
    fi
fi

exit 0
