#!/usr/bin/env bash
#
# The `tv-power` machine action: whether the machine drives the television it
# is plugged into.
#
#   on    the television is woken and switched to this machine's input when
#         the kiosk starts, and put back to sleep when it shuts down
#   off   the machine never touches the television
#
# The setting is the unit: archstar-tv.service enabled and started, or
# disabled and stopped. Everything about *how* a television is talked to lives
# in /usr/bin/archstar-tv, which the unit runs - this action only says whether
# it should be.
#
# **`off` does not switch a television off.** Stopping the unit runs its
# `ExecStop`, and the obvious reading of that would be a machine that blanks
# the screen the moment somebody asks it to leave the screen alone - which is
# the opposite of what they asked for. The launcher holds that rule: it puts a
# television to sleep only while the machine itself is going down.
#
# `off` by default (configs/install.conf). Every other setting here ships as
# the thing the machine can do, and this one does not, deliberately: no
# ArchStar machine has yet been seen to drive a real television over CEC, and
# a feature that has never been watched working must not switch a stranger's
# television on the first time they update. It is one press away on the
# settings page, and the page says what the machine found.

set -euo pipefail

# The shared half of the actions. The runner never sees this directory: it
# executes names out of the actions directory beside it, and a library among
# them would be a name somebody could ask for.
ARCHSTAR_ACTIONS_LIB="${ARCHSTAR_ACTIONS_LIB:-/usr/lib/archstar-actions/lib/common.sh}"
# 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}"

UNIT='archstar-tv.service'

value="${1:-}"
require_one_of "${value}" on off

# Enable and start are separate on purpose. `enable` is symlinks and works
# everywhere, the installer's chroot included - which is how the default from
# install.conf is applied into a system that is not running. Start and stop
# only make sense against a live systemd, which is the same gate every action
# holds its restarts behind.
if [[ "${value}" == 'on' ]]; then
    systemctl enable "${UNIT}"
    # Restarting rather than starting: the unit stays "active" between the two
    # ends of the machine's life, so a plain start on a machine that already
    # has it up would do nothing at all - and somebody pressing this expects
    # the television in front of them to wake now.
    if [[ "${ARCHSTAR_ACTION_RESTART}" == '1' ]]; then
        systemctl restart "${UNIT}"
    fi
else
    systemctl disable "${UNIT}"
    if [[ "${ARCHSTAR_ACTION_RESTART}" == '1' ]]; then
        systemctl stop "${UNIT}"
    fi
fi

remember_setting tv-power "${value}"

if [[ "${value}" == 'on' ]]; then
    printf 'the television will wake with the machine; the settings page says whether it answered\n'
else
    printf 'the machine will not touch the television; it is left exactly as it is now\n'
fi
