#!/usr/bin/env bash
cat <<-'EOF' | sudo tee /etc/udev/rules.d/99-saleae-logic.rules
	# Rules for the Saleae Logic analyzer to allow to run the programs a normal user
	# being part of the plugdev group. Simply copy the file to /etc/udev/rules.d/
	# and plug the device

	#BUS!="usb", ACTION!="add", SUBSYSTEM!=="usb_device", GOTO="saleae_logic_rules_end"
	SUBSYSTEM!="usb", GOTO="saleae_logic_rules_end"

	# Saleae Logic analyzer (USB Based)
	# Bus 006 Device 006: ID 0925:3881 Lakeview Research
	# Bus 001 Device 009: ID 21a9:1004 Product: Logic S/16, Manufacturer: Saleae LLC

	ATTRS{idVendor}=="0925", ATTRS{idProduct}=="3881", MODE="664", GROUP="plugdev"
	ATTRS{idVendor}=="21a9", ATTRS{idProduct}=="1004", MODE="664", GROUP="plugdev"

	LABEL="saleae_logic_rules_end"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/99-pm3.rules
	SUBSYSTEM!="usb", GOTO="pm3_rules_end"

	ATTRS{idVendor}=="17a4", ATTRS{idProduct}=="0001", MODE="0666", GROUP="plugdev"

	LABEL="pm3_rules_end"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/99-wemos-esp32.rules
	SUBSYSTEM!="usb", GOTO="wemos-esp32_rules_end"

	ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", MODE="0666", GROUP="plugdev"

	LABEL="wemos-esp32_rules_end"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/99-platformio-udev.rules
	# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
	#
	# Licensed under the Apache License, Version 2.0 (the "License");
	# you may not use this file except in compliance with the License.
	# You may obtain a copy of the License at
	#
	#    http://www.apache.org/licenses/LICENSE-2.0
	#
	# Unless required by applicable law or agreed to in writing, software
	# distributed under the License is distributed on an "AS IS" BASIS,
	# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	# See the License for the specific language governing permissions and
	# limitations under the License.

	#####################################################################################
	#
	# INSTALLATION
	#
	# Please visit > https://docs.platformio.org/en/latest/faq.html#platformio-udev-rules
	#
	#####################################################################################

	#
	# Boards
	#

	# CP210X USB UART
	ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# FT231XS USB UART
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6015", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Prolific Technology, Inc. PL2303 Serial Port
	ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# QinHeng Electronics HL-340 USB-Serial adapter
	ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Arduino boards
	ATTRS{idVendor}=="2341", ATTRS{idProduct}=="[08][02]*", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"
	ATTRS{idVendor}=="2a03", ATTRS{idProduct}=="[08][02]*", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Arduino SAM-BA
	ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="6124", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{MTP_NO_PROBE}="1"

	# Digistump boards
	ATTRS{idVendor}=="16d0", ATTRS{idProduct}=="0753", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Maple with DFU
	ATTRS{idVendor}=="1eaf", ATTRS{idProduct}=="000[34]", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# USBtiny
	ATTRS{idProduct}=="0c9f", ATTRS{idVendor}=="1781", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# USBasp V2.0
	ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05dc", MODE:="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Teensy boards
	ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="04[789B]?", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"
	ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="04[789A]?", ENV{MTP_NO_PROBE}="1"
	SUBSYSTEMS=="usb", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="04[789ABCD]?", MODE:="0666"
	KERNEL=="ttyACM*", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="04[789B]?", MODE:="0666"

	#TI Stellaris Launchpad
	ATTRS{idVendor}=="1cbe", ATTRS{idProduct}=="00fd", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	#TI MSP430 Launchpad
	ATTRS{idVendor}=="0451", ATTRS{idProduct}=="f432", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	#GD32V DFU Bootloader
	ATTRS{idVendor}=="28e9", ATTRS{idProduct}=="0189", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	#
	# Debuggers
	#

	# Black Magic Probe
	SUBSYSTEM=="tty", ATTRS{interface}=="Black Magic GDB Server", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"
	SUBSYSTEM=="tty", ATTRS{interface}=="Black Magic UART Port", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# opendous and estick
	ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="204f", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Original FT232/FT245 VID:PID
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Original FT2232 VID:PID
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Original FT4232 VID:PID
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6011", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Original FT232H VID:PID
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6014", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# DISTORTEC JTAG-lock-pick Tiny 2
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="8220", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# TUMPA, TUMPA Lite
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="8a98", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="8a99", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# XDS100v2
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="a6d0", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Xverve Signalyzer Tool (DT-USB-ST), Signalyzer LITE (DT-USB-SLITE)
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bca0", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bca1", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# TI/Luminary Stellaris Evaluation Board FTDI (several)
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bcd9", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# TI/Luminary Stellaris In-Circuit Debug Interface FTDI (ICDI) Board
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bcda", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# egnite Turtelizer 2
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="bdc8", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Section5 ICEbear
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="c140", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="c141", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Amontec JTAGkey and JTAGkey-tiny
	ATTRS{idVendor}=="0403", ATTRS{idProduct}=="cff8", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# TI ICDI
	ATTRS{idVendor}=="0451", ATTRS{idProduct}=="c32a", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# STLink probes
	ATTRS{idVendor}=="0483", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Hilscher NXHX Boards
	ATTRS{idVendor}=="0640", ATTRS{idProduct}=="0028", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Hitex probes
	ATTRS{idVendor}=="0640", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Altera USB Blaster
	ATTRS{idVendor}=="09fb", ATTRS{idProduct}=="6001", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Amontec JTAGkey-HiSpeed
	ATTRS{idVendor}=="0fbb", ATTRS{idProduct}=="1000", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# SEGGER J-Link
	ATTRS{idVendor}=="1366", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Raisonance RLink
	ATTRS{idVendor}=="138e", ATTRS{idProduct}=="9000", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Debug Board for Neo1973
	ATTRS{idVendor}=="1457", ATTRS{idProduct}=="5118", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Olimex probes
	ATTRS{idVendor}=="15ba", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# USBprog with OpenOCD firmware
	ATTRS{idVendor}=="1781", ATTRS{idProduct}=="0c63", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# TI/Luminary Stellaris In-Circuit Debug Interface (ICDI) Board
	ATTRS{idVendor}=="1cbe", ATTRS{idProduct}=="00fd", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Marvell Sheevaplug
	ATTRS{idVendor}=="9e88", ATTRS{idProduct}=="9e8f", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# Keil Software, Inc. ULink
	ATTRS{idVendor}=="c251", ATTRS{idProduct}=="2710", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"

	# CMSIS-DAP compatible adapters
	ATTRS{product}=="*CMSIS-DAP*", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/99-nodemcu.rules
	SUBSYSTEM!="usb", GOTO="nodemcu_rules_end"

	ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", MODE="0666", GROUP="plugdev"

	LABEL="nodemcu_rules_end"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/99-ch341a-prog.rules
	SUBSYSTEM!="usb", GOTO="ch431a-prog_rules_end"

	ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="5512", MODE="0666", GROUP="plugdev"

	LABEL="ch431a-prog_rules_end"
EOF

echo 'blacklist usbtest' >>/etc/modprobe.d/blacklist.conf

cat <<-'EOF' | sudo tee /etc/udev/rules.d/99-xf2-logic.rules
	SUBSYSTEM!="usb", GOTO="fx2-probe--programming_rules_end"

	ATTRS{idVendor}=="04b4", ATTRS{idProduct}=="8613", MODE="0664", GROUP="plugdev"

	LABEL="fx2-probe--programming_rules_end"
EOF

echo 'blacklist usbtest' >>/etc/modprobe.d/blacklist.conf

cat <<-'EOF' | sudo tee /etc/udev/rules.d/60-blackmagic-probe.rules
	SUBSYSTEM!="usb", GOTO="blackmagic-probe-programming_rules_end"

	ATTRS{idVendor}=="1d50", ATTRS{idProduct}=="6017", MODE="0666", GROUP="plugdev"

	LABEL="blackmagic-probe-programming_rules_end"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/60-st-linkv2.rules
	## Now in stlink-utils 49-stlinkv2.rules
	#SUBSYSTEM!="usb", GOTO="st-link-v2-programming_rules_end"
	#
	#ATTRS{idVendor}=="0483", ATTRS{idProduct}=="3748", MODE="0666", GROUP="plugdev"
	#
	#LABEL="st-link-v2-programming_rules_end"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/60-st-linkv2_1.rules
	## Now in stlink-utils 49-stlinkv2-1.rules
	#SUBSYSTEM!="usb", GOTO="st-link-v2-1-programming_rules_end"
	#
	#ATTRS{idVendor}=="0483", ATTRS{idProduct}=="374b", MODE="0666", GROUP="plugdev"
	#
	#LABEL="st-link-v2-1-programming_rules_end"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/60-cp210x.rules
	# udev rules file for CP210x device to be programmable py
	# cp210x-program

	SUBSYSTEM!="tty", GOTO="cp210x-programming_rules_end"

	ATTRS{idVendor}=="10C4", ATTRS{idProduct}=="EA60", MODE="0666", GROUP="plugdev"
	ATTRS{idVendor}=="10C4", ATTRS{idProduct}=="EA61", MODE="0666", GROUP="plugdev"

	LABEL="cp210x-programming_rules_end"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/99-ftdi.rules
	SUBSYSTEM=="usb", GOTO="ftdi_rules_end"

	ATTR{product}=="ftdi", ATTR{idProduct}=="0403", ATTRS{idVendor}=="6001", MODE="0666", GROUP="plugdev"

	LABEL="ftdi_rules_end"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/99-USBasp.rules
	SUBSYSTEM=="usb", GOTO="USBasp_rules_end"

	ATTR{product}=="USBasp", ATTR{idProduct}=="05dc", ATTRS{idVendor}=="16c0", MODE="0666", GROUP="plugdev"

	LABEL="USBasp_rules_end"
EOF

cat <<-'EOF' | sudo tee /etc/udev/rules.d/81-thinkpad-dock.rules <<'EOF'
	KERNEL=="dock.0", ACTION=="change", RUN+="/usr/local/sbin/thinkpad-dock.sh"
EOF

cat <<-'EOF' | sudo tee  /etc/udev/rules.d/51-android.rules
	SUBSYSTEM!="usb", GOTO="android_rules_end"

	ATTR{idVendor}=="18d1", ATTR{idProduct}=="4ee7", MODE="0666", GROUP="plugdev", SYMLINK+="android_adb", OWNER="default"
	ATTR{idVendor}=="18d1", ATTR{idProduct}=="d001", MODE="0666", GROUP="plugdev", SYMLINK+="android_adb", OWNER="default"

	LABEL="android_rules_end"
EOF

# sudo usermod -aG plugdev $LOGNAME (https://developer.android.com/studio/run/device)
# sudo udevadm control --reload-rules
