#!/usr/bin/env bash
#
# The `bluetooth` machine action: look around, and switch the radio.
#
#   refresh    write down what BlueZ already knows, and nothing else
#   discover   open a window: visible, pairable and scanning for half a minute
#   on / off   the adapter
#
# `discover` and not "discoverable": a kiosk that is permanently visible and
# permanently accepting pairings is a kiosk anybody in the building can pair
# with. The window is opened by somebody pressing a button on the page, which
# is the closest thing this machine has to a person saying yes, and it shuts
# itself whatever happens inside it.
#
# The work is in archstar_gamepad.bluetooth, beside the gamepad bridge, because
# a wireless controller is a controller: it is introduced here and driven
# there.

set -euo pipefail

ARCHSTAR_ACTIONS_LIB="${ARCHSTAR_ACTIONS_LIB:-/usr/lib/archstar-actions/lib/common.sh}"
# shellcheck source=/dev/null
source "${ARCHSTAR_ACTIONS_LIB}"

VERBS=('refresh' 'discover' 'on' 'off')

main() {
    local verb="${1:-}"
    require_one_of "${verb}" "${VERBS[@]}"
    bluetooth_verb "${verb}"
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi
