###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2024 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

UDEV_RULES_SEQUENCE = 90
UDEV_PARENT_LOCATION = $(libdir)
#UDEV_PARENT_LOCATION = $(sysconfdir)

UDEV_RULES_LOCATION = $(UDEV_PARENT_LOCATION)/udev/rules.d
UDEV_RULES_DIRECTORY = $(INSTALL_ROOT)$(UDEV_RULES_LOCATION)

UDEV_PATH_PREFIX = $(UDEV_RULES_DIRECTORY)/$(UDEV_RULES_SEQUENCE)-$(PACKAGE_TARNAME)-
UDEV_RULES_EXTENSION = rules

UDEV_NAME_HID = hid.$(UDEV_RULES_EXTENSION)
UDEV_NAME_UINPUT = uinput.$(UDEV_RULES_EXTENSION)

UPDUSBDEVS = $(SRC_TOP)Tools/updusbdevs -quiet -quiet
UDEV_NAME_USB_TEMPLATE = usb-template.$(UDEV_RULES_EXTENSION)
UDEV_NAME_USB_CUSTOMIZED = usb-customized.$(UDEV_RULES_EXTENSION)
UDEV_NAME_USB_GENERIC = usb-generic.$(UDEV_RULES_EXTENSION)

all: usb-customized usb-generic

usb-customized:
	cp $(UDEV_NAME_USB_TEMPLATE) $(UDEV_NAME_USB_CUSTOMIZED)
	$(UPDUSBDEVS) -nogeneric udev:$(UDEV_NAME_USB_CUSTOMIZED)

usb-generic:
	cp $(UDEV_NAME_USB_TEMPLATE) $(UDEV_NAME_USB_GENERIC)
	$(UPDUSBDEVS) -onlygeneric udev:$(UDEV_NAME_USB_GENERIC)

install-wrapper: install-commands-directory
	$(INSTALL_SCRIPT) $(SRC_DIR)/udev-wrapper $(INSTALL_COMMANDS_DIRECTORY)

install-rules-directory:
	$(INSTALL_DIRECTORY) $(UDEV_RULES_DIRECTORY)

install-rules: install-rules-hid install-rules-uinput install-rules-usb

install-rules-hid: install-rules-directory
	$(INSTALL_DATA) $(SRC_DIR)/$(UDEV_NAME_HID) $(UDEV_PATH_PREFIX)$(UDEV_NAME_HID)

install-rules-uinput: install-rules-directory
	$(INSTALL_DATA) $(SRC_DIR)/$(UDEV_NAME_UINPUT) $(UDEV_PATH_PREFIX)$(UDEV_NAME_UINPUT)

install-rules-usb-customized: usb-customized install-rules-directory
	$(INSTALL_DATA) $(BLD_DIR)/$(UDEV_NAME_USB_CUSTOMIZED) $(UDEV_PATH_PREFIX)$(UDEV_NAME_USB_CUSTOMIZED)

install-rules-usb-generic: usb-generic install-rules-directory
	$(INSTALL_DATA) $(BLD_DIR)/$(UDEV_NAME_USB_GENERIC) $(UDEV_PATH_PREFIX)$(UDEV_NAME_USB_GENERIC)

install-rules-usb: install-rules-usb-customized install-rules-usb-generic

install: install-wrapper install-rules

uninstall:
	-rm -f $(UDEV_RULES_DIRECTORY)/?*-$(PACKAGE_TARNAME).$(UDEV_RULES_EXTENSION)
	-rm -f $(UDEV_RULES_DIRECTORY)/?*-$(PACKAGE_TARNAME)-*.$(UDEV_RULES_EXTENSION)
	-rm -f $(INSTALL_COMMANDS_DIRECTORY)/udev-*

clean::
	-rm -f $(UDEV_NAME_USB_CUSTOMIZED)
	-rm -f $(UDEV_NAME_USB_GENERIC)

distclean:: clean
	-rm -f $(UDEV_NAME_USB_TEMPLATE)

reload:
	udevadm control --reload

