From 38b9f67c3b874ff33383ec2eb18aba58e4e9dfa1 Mon Sep 17 00:00:00 2001
From: Mateusz Piotrowski <mpp302@gmail.com>
Date: Thu, 6 May 2021 01:26:50 +0200
Subject: [PATCH] Fix qmk flash on FreeBSD (#12085)

When the USB device is connected, FreeBSD creates not one, but three
device nodes in /dev, e.g.: /dev/ttyU0, /dev/ttyU0.init, and
/dev/ttyU0.lock.

As a result, this leads to the USB variable containing 3 paths
(and therefore, whitespace) and messages like this one:

    Device /dev/ttyU0
    /dev/ttyU0.init
    /dev/ttyU0.lock has appeared; assuming it is the controller.

This changes fixes the use of the -z flag of "[" (see test(1)). Also, it
removes undesired paths from the USB variable, leaving only
one path there (i.e., "/dev/ttyU0").
---
 tmk_core/avr.mk | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tmk_core/avr.mk b/tmk_core/avr.mk
index c1d3f6b39e..2bc7cc9553 100644
--- a/tmk_core/avr.mk
+++ b/tmk_core/avr.mk
@@ -160,6 +160,8 @@ define EXEC_AVRDUDE
 	list_devices() { \
 		if $(GREP) -q -s icrosoft /proc/version; then \
 		    wmic.exe path Win32_SerialPort get DeviceID 2>/dev/null | LANG=C perl -pne 's/COM(\d+)/COM.($$1-1)/e' | sed 's!COM!/dev/ttyS!' | xargs echo -n | sort; \
+		elif [ "`uname`" = "FreeBSD" ]; then \
+			ls /dev/tty* | grep -v '\.lock$$' | grep -v '\.init$$'; \
 		else \
 			ls /dev/tty*; \
 		fi; \
@@ -169,7 +171,7 @@ define EXEC_AVRDUDE
 	TMP1=`mktemp`; \
 	TMP2=`mktemp`; \
 	list_devices > $$TMP1; \
-	while [ -z $$USB ]; do \
+	while [ -z "$$USB" ]; do \
 		sleep 0.5; \
 		printf "."; \
 		list_devices > $$TMP2; \