2023-07-02 22:42:52 +00:00
|
|
|
#!/usr/bin/env nix-shell
|
|
|
|
#!nix-shell -i bash -p gnused
|
2023-01-07 03:43:28 +00:00
|
|
|
# usage: install-bluetooth <source_dir> <destdir>
|
2022-12-03 11:05:09 +00:00
|
|
|
# source_dir contains plain-text files of any filename.
|
2023-01-07 03:43:28 +00:00
|
|
|
# for each file, this extracts the MAC and creates a symlink in destdir which
|
2022-12-03 11:05:09 +00:00
|
|
|
# points to the original file, using the MAC name as file path
|
|
|
|
#
|
|
|
|
# bluetooth connection structure is /var/lib/bluetooth/<HOST_MAC>/<DEVICE_MAX>/{attributes,info}
|
2023-01-07 03:43:28 +00:00
|
|
|
# bluetoothd/main.conf options can be found here:
|
|
|
|
# - <https://pythonhosted.org/BT-Manager/config.html>
|
2023-01-07 11:31:35 +00:00
|
|
|
# can be set via nixos' `hardware.bluetooth.settings`
|
2022-12-03 11:05:09 +00:00
|
|
|
|
2023-01-07 03:43:28 +00:00
|
|
|
srcdir="$1"
|
|
|
|
destdir="$2"
|
2023-07-08 10:34:46 +00:00
|
|
|
stamp="$3"
|
2022-12-03 11:05:09 +00:00
|
|
|
|
2023-01-07 03:43:28 +00:00
|
|
|
if [ "x$destdir" = "x" ]
|
2022-12-03 11:05:09 +00:00
|
|
|
then
|
2023-01-07 03:43:28 +00:00
|
|
|
devmac=$(cat /sys/kernel/debug/bluetooth/hci0/identity | cut -f 1 -d' ' | tr "a-z" "A-Z")
|
2022-12-03 11:05:09 +00:00
|
|
|
# default to the first MAC address on the host
|
2023-01-07 03:43:28 +00:00
|
|
|
destdir="/var/lib/bluetooth/$devmac"
|
|
|
|
test -d "$destdir" || mkdir "$destdir" || test -d "$destdir"
|
2022-12-03 11:05:09 +00:00
|
|
|
fi
|
|
|
|
|
2023-01-07 03:43:28 +00:00
|
|
|
for f in $(ls "$srcdir")
|
2022-12-03 11:05:09 +00:00
|
|
|
do
|
2023-01-07 03:43:28 +00:00
|
|
|
mac=$(sed -rn 's/# MAC=(.*)/\1/p' "$srcdir/$f")
|
|
|
|
condir="$destdir/$mac"
|
2023-01-07 11:31:35 +00:00
|
|
|
if ! test -f "$condir/info"
|
|
|
|
then
|
|
|
|
# don't *overwrite* pairings. instead, only copy the device data if the host doesn't yet know about it.
|
|
|
|
# unfortunately, it seems that for most BT devices i can't share link keys across hosts.
|
|
|
|
# perhaps i could using `bdaddr` to force a shared host MAC across all hosts, but that doesn't work for all manufacturers.
|
|
|
|
# instead, my bluetooth "secrets" are mostly just a list of MACs i want a host to trust.
|
|
|
|
mkdir "$condir"
|
|
|
|
cp "$srcdir/$f" "$condir/info"
|
|
|
|
touch "$condir/attributes"
|
|
|
|
fi
|
2022-12-03 11:05:09 +00:00
|
|
|
done
|
2023-07-08 10:34:46 +00:00
|
|
|
|
|
|
|
if [ "x$stamp" != "x" ]
|
|
|
|
then
|
|
|
|
touch "$stamp"
|
|
|
|
fi
|