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
    # $2: the version upgraded from.
    if [ "$(vercmp "$2" 3.0.0)" -lt 0 ]; then
        cat <<'MSG'
==> Big Game Mode 3.0.0 has a new layout: Home (Turbo and the game being
    played), Games, Optimization (recommendations, Tuning, Details, network
    and apps), Telemetry, Records (Turbo report, crashes, logs) and Settings.
    The game library shows each game's Linux compatibility, anti-cheat and
    players; Settings → Game Information turns it off.
==> A Big Game Mode already open in the tray gives way to this version the
    next time it is opened.
MSG
    fi
    # 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.
    Before removing Big Game Mode, run as your user (games closed):
        bigame-ui --restore-all-graphics
    or Settings → Advanced → Restore every game's graphics. Removed already?
    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
}
