From 524053e3c0eceab0eadf04cde6c2c0f1a77520fe Mon Sep 17 00:00:00 2001
From: Danny <nooges@users.noreply.github.com>
Date: Wed, 28 Feb 2018 16:40:43 -0500
Subject: [PATCH] Remap msys2 usb, fix USB port detection (#2301)

* Add AVRDUDE to MSYS2 setup

* Remap detected USB port in MSYS2 to COM port
---
 tmk_core/avr.mk       | 4 ++++
 util/msys2_install.sh | 1 +
 2 files changed, 5 insertions(+)

diff --git a/tmk_core/avr.mk b/tmk_core/avr.mk
index 106b771507a..853b8f02f20 100644
--- a/tmk_core/avr.mk
+++ b/tmk_core/avr.mk
@@ -183,6 +183,10 @@ avrdude: $(BUILD_DIR)/$(TARGET).hex check-size
 		done; \
 		echo ""; \
 		echo "Detected controller on USB port at $$USB"; \
+		if grep -q -s 'MINGW\|MSYS' /proc/version; then \
+			USB=`echo "$$USB" | perl -pne 's/\/dev\/ttyS(\d+)/COM.($$1+1)/e'`; \
+			echo "Remapped MSYS2 USB port to $$USB"; \
+		fi; \
 		sleep 1; \
 		avrdude -p $(MCU) -c avr109 -P $$USB -U flash:w:$(BUILD_DIR)/$(TARGET).hex; \
 	fi
diff --git a/util/msys2_install.sh b/util/msys2_install.sh
index 282053fe4c5..c6468575413 100644
--- a/util/msys2_install.sh
+++ b/util/msys2_install.sh
@@ -16,6 +16,7 @@ function install_avr {
     wget "http://ww1.microchip.com/downloads/en/DeviceDoc/avr8-gnu-toolchain-installer-3.5.4.91-win32.any.x86.exe"
     7z x avr8-gnu-toolchain-installer-3.5.4.91-win32.any.x86.exe
     rm avr8-gnu-toolchain-installer-3.5.4.91-win32.any.x86.exe
+    pacman --needed -S mingw-w64-x86_64-avrdude
 }
 
 function install_arm {