post_install() {
    set -e
    VENV=/usr/lib/vosk-cli-dictation/venv
    if [ ! -d "$VENV" ]; then
        python3 -m venv --system-site-packages "$VENV"
    fi
    "$VENV"/bin/pip install --no-cache-dir vosk pynput

    echo ""
    echo "vosk-cli-dictation is installed. To fetch a speech model, run:"
    echo "  vosk-cli-dictation --download-model en   (or fr, de, es)"
    echo "A model is also offered on first run if none is present."
    echo ""
    echo "Only if you use the ydotool fallback for text injection, add your"
    echo "user to the 'input' group (access to /dev/uinput):"
    echo "  sudo usermod -aG input \$USER   (then log out and back in)"
    echo "xdotool (X11) and wtype (Wayland) do not need this."
}

post_upgrade() {
    post_install
}

post_remove() {
    # venv (pip) and __pycache__ (bytecode cache written at runtime) are not
    # pacman-owned files, unlike the rest of the tree — same defect already
    # found and fixed on the .deb and .rpm channels (postrm / %postun,
    # 14/09/2026): without this cleanup, pacman leaves the directories behind.
    rm -rf /usr/lib/vosk-cli-dictation/venv
    find /usr/lib/vosk-cli-dictation -depth -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
    rmdir /usr/lib/vosk-cli-dictation/config /usr/lib/vosk-cli-dictation/src /usr/lib/vosk-cli-dictation 2>/dev/null || true
}
