#!/usr/bin/env bash
#
# The `bluetooth-pair` machine action: introduce one wireless controller, and
# keep it.
#
# Takes an address, which is the one argument in this protocol that is not a
# name out of a list (architecture.md): there is no list of Bluetooth addresses
# to write down, so the *shape* is spelled out instead and checked three times
# - in the service before the request is written, in the runner before anything
# runs, and here.
#
# Pairing alone is not enough and that is the whole reason this action exists
# rather than a `pair` verb: the device is also *trusted*, because without that
# every reconnection would need somebody to say yes again and there is nobody
# in front of this machine to say it.

set -euo pipefail

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

main() {
    local address="${1:-}"
    require_address "${address}"
    bluetooth_verb pair "${address}"
}

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