#!/bin/sh
#
# Set X13s bluetooth public address
#

# Only for X13s
if [ "$(cat /sys/devices/virtual/dmi/id/product_family)" != \
     "ThinkPad X13s Gen 1" ]; then
	exit 0
fi

idx=
for hci in /sys/class/bluetooth/hci* ; do
	rp=$(realpath "${hci}")
	case "${rp}" in
		*/serial0/serial0-0/*)
			idx=${rp##*/hci}
			break
			;;
	esac
done

if [ -z "${idx}" ] ; then
	echo "No serial BT adapter found"
	exit 0
fi


# Bytes 1-3 from /etc/machine-id
nic=$(sed 's/\(..\)/\1:/g' /etc/machine-id | cut -c 1-8)

# Qualcomm OUI
oui="f0:fd:8c"

mac="${nic}:${oui}"
echo "Set public addr of hci${idx} to ${mac}"

/usr/bin/yes 2>/dev/null | /usr/bin/btmgmt --index "${idx}" public-addr "${mac}"
