#!/usr/bin/env bash
#
# The `restart-game` machine action: bring UltraStar back up so it sees new
# songs, or a changed setting.
#
# Not `systemctl restart`. A clean stop of the kiosk unit is a *successful*
# stop, and a successful stop powers the machine off - that is how quitting the
# game turns the appliance off. The only way to get the game back is to make it
# fail: SIGKILL to everything in the unit, and `Restart=on-failure` starts it
# again two seconds later. SIGTERM would let cage exit cleanly, which is the one
# thing this must not do.
#
# All of that lives in the shared library, because every setting action needs
# it too and a second copy of a rule this exact is a second rule.

set -euo pipefail

# The path is a runtime one and the linter reads these on standard input, so
# it could not follow it even if it were spelled literally.
# shellcheck source=/dev/null
source "${ARCHSTAR_ACTIONS_LIB:-/usr/lib/archstar-actions/lib/common.sh}"

main() {
    if restart_the_game; then
        printf 'the game is restarting\n'
    else
        # Not a failure: a unit somebody masked to debug over SSH is not
        # running on purpose, and starting it from here would undo that.
        printf 'there was nothing to restart\n'
    fi
}

main "$@"
