#!/usr/bin/env bash
#
# The `hotspot` machine action: whether the kiosk raises its own WiFi network.
#
#   on    the machine broadcasts its own network (SSID and passphrase from
#         /etc/archstar/hotspot.conf); phones join it to reach the page even
#         where the room has no network worth joining
#   auto  the same, but only where there is no cable - the nomad case. With a
#         cable there is already a network the room's phones can reach the page
#         on, and a second one would be a second thing to explain to guests
#
# There used to be an `off` as well, kept in case the hotspot misbehaved. It
# does not, and the wireless QR code on the television (issue #17) is now the
# only way anybody in the room is told which network to join - so a machine
# that can broadcast, broadcasts.
#
# The setting is the unit: archstar-hotspot.service enabled and started, or
# disabled and stopped. Everything about *how* the network is raised lives in
# /usr/bin/archstar-hotspot, which the unit runs - this action only says
# whether it should be.
#
# Like every setting it can be chosen on a machine that cannot honour it yet:
# with no wireless interface the launcher records why in
# /run/archstar/hotspot.json and exits cleanly, the page repeats the reason,
# and the choice waits for the day an adapter is plugged in.

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-hotspot.service'

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

# 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.
# Both remaining values enable the unit. The difference is not here: the
# launcher reads the setting and decides whether to raise the radio, so the one
# place that knows about cables is the one place that touches them. Restarting
# rather than starting, so a machine already broadcasting re-decides now that
# the rule has changed under it.
systemctl enable "${UNIT}"
if [[ "${ARCHSTAR_ACTION_RESTART}" == '1' ]]; then
    systemctl restart "${UNIT}"
fi

remember_setting hotspot "${value}"

case "${value}" in
    on)
        printf 'the hotspot is on; what it is called is on the settings page\n'
        ;;
    *)
        printf 'the hotspot will come up where there is no cable; the settings page says whether it did\n'
        ;;
esac
