post_install() {
    set -e
    VENV=/usr/lib/piperread/venv
    if [ ! -d "$VENV" ]; then
        python3 -m venv "$VENV"
    fi
    echo "piperread: downloading the Piper engine and its dependencies (about 75 MB to download, 200 to 250 MB once installed, network required)..."
    "$VENV"/bin/pip install --no-cache-dir 'piper-tts>=1.8,<2'

    echo ""
    echo "piperread is installed. It ships no voice: download one with"
    echo "  piperread --download-voice"
    echo "(see 'piperread --list-voices' for the choice and the licences)."
    echo "Voices are stored in ~/.local/share/piperread/voices/ and work offline."
}

post_upgrade() {
    post_install
}

post_remove() {
    # Ni le venv (pip) ni __pycache__ (bytecode écrit à l'exécution) ne sont
    # possédés par pacman : sans ce ménage le dossier survit au retrait.
    rm -rf /usr/lib/piperread/venv
    find /usr/lib/piperread -depth -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
    rmdir /usr/lib/piperread 2>/dev/null || true
}
