post_install() {
    cat <<'MSG'
==> Big Game Mode: open it from the application menu ("Big Game Mode").
    The privileged helper (bigame-daemon) starts on demand over D-Bus.
MSG
}

_stop_bus_spawned_helper() {
    # Before 1.0.0 the activation file named no SystemdService, so the bus
    # started the helper itself, as dbus-:1.N-com.biglinux.BiGameMode@M.service,
    # outside bigame-daemon.service and its sandbox. try-restart cannot reach
    # that instance; stopped, the next call starts the helper through the unit.
    systemctl stop 'dbus-*com.biglinux.BiGameMode@*.service' >/dev/null 2>&1 || :
}

post_upgrade() {
    _stop_bus_spawned_helper
    # A running helper keeps the old binary and sandbox until it restarts.
    # Stopped rather than restarted: this runs before pacman's hook reloads
    # systemd, so a restart here would bring it back under the old unit. The
    # next D-Bus call starts it under the new one.
    systemctl stop bigame-daemon.service >/dev/null 2>&1 || :
}

pre_remove() {
    # Give falcond back as it was before Big Game Mode first took charge of it
    # (enabled or not, running or not). Does nothing if it never did.
    if ! busctl call --timeout=10 com.biglinux.BiGameMode /com/biglinux/BiGameMode \
        com.biglinux.BiGameMode ReleaseGameBackend >/dev/null 2>&1; then
        echo "==> Big Game Mode could not hand falcond back as it found it; check it with: systemctl status falcond"
    fi
    systemctl stop bigame-daemon.service >/dev/null 2>&1 || :
    _stop_bus_spawned_helper

    cat <<'MSG'
==> Games changed by AI Graphics keep their OptiScaler files until restored.
    Use "Restore the game's graphics" in Big Game Mode before removing it, or
    reinstall later: the backups in ~/.local/state/bigame-mode/graphics stay.
==> Settings Big Game Mode wrote for your user stay as they are: launch
    options in Steam, Heroic and Lutris, lsfg-vk entries, MangoHud and vkBasalt
    styles, ~/.config/environment.d/bigame-mode.conf, and the per-game
    profiles in /usr/share/falcond/profiles/user, which falcond keeps applying.
MSG
}
