
#########################################################################
#
#  Makefile for the udev rules shipped with mbgtools-lx.
#
#########################################################################

BASEDIR := ..
include $(BASEDIR)/Makefile


# For 2.6.x and newer kernels, the devices are implemented as character devices
# using their own dynamic major number, and minor numbers starting at 0.
# The device nodes are automatically created by udev, where the rules are
# located in a rules.d directory, to which our rules file has to be copied.
# Udev versions before 055 used a different syntax than later versions, so
# we provide 2 different files with either syntax and try to figure out
# during installation which file to use.
# Very old systems may not provide a rules.d directory. The udev rules of
# such systems need to be updated manually.

# Try to find udev version using the udevadm tool:
UDEVADM ?= $(call chk_which_exec, "udevadm" )
ifneq ($(UDEVADM),)
  UDEV_VER ?= $(shell $(UDEVADM) --version | awk '{print $$1;}' )
endif

# If the version could not be determined there may be an udev
# version installed which does not yet provide the udevadm tool.
# In this case try to get the udev version from udevinfo:
UDEVINFO ?= $(call chk_which_exec, "udevinfo" )
UDEV_VER ?= $(shell $(UDEVINFO) -V | awk '{print $$3}' | sed -e 's/[^0-9]//g' )


UDEV_VER_055 = 055
UDEV_RULES_D = $(DESTDIR)$(sysconfdir)/udev/rules.d
UDEV_RULES_MBGCLOCK = 55-mbgclock.rules
UDEV_RULES_MBGCLOCK_BEFORE_055 = $(UDEV_RULES_MBGCLOCK).before-udev-$(UDEV_VER_055)

DIFF_SEPARATOR = "---------------------------------------------------------------------"


ifndef STAGED_BUILD
  UDEV_RULES_TO_INSTALL := $(shell \
    if [ $(UDEV_VER) -lt $(UDEV_VER_055) ]; then \
      echo "$(UDEV_RULES_MBGCLOCK_BEFORE_055)"; \
    else \
      echo "$(UDEV_RULES_MBGCLOCK)"; \
    fi; )
else
  UDEV_RULES_TO_INSTALL = $(UDEV_RULES_MBGCLOCK)
endif



.PHONY: install_udev
install_udev:
ifneq ($(UID),0)
	$(call make_as_root, $@ ALLOW_UDEV_UPDATE=$(ALLOW_UDEV_UPDATE))
else
  ifdef STAGED_BUILD
	  mkdir -p $(UDEV_RULES_D)
  endif
	@if test -d $(UDEV_RULES_D); then \
	  # udev rules dir exists. \
	  if test -f $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK); then \
	    # udev rules file also exists. \
	    if diff -Nq $(UDEV_RULES_TO_INSTALL) $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK); then \
	      # udev rules file is unchanged. \
	      echo "Udev rules file $(UDEV_RULES_MBGCLOCK) in $(UDEV_RULES_D)/ is unchanged."; \
	    else \
	      # Our udev rules file differs from the existing one. \
	      if [ "$(ALLOW_UDEV_UPDATE)" = "0" ] ; then \
	        echo "Overwriting the udev rule file was explicitly prohibited."; \
	      elif ! [ "$(ALLOW_UDEV_UPDATE)" = "" ] ; then \
	        echo "Overwriting the udev rule file was explicitly allowed:"; \
	        cp -v $(UDEV_RULES_TO_INSTALL) $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK); \
	        chmod 644 $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK); \
	      else \
	        # By default, show the differences and copy interactively. \
	        echo ""; \
	        echo "$(DIFF_SEPARATOR)"; \
	        diff -N $(UDEV_RULES_TO_INSTALL) $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK); \
	        echo "$(DIFF_SEPARATOR)"; \
	        echo ""; \
	        cp -i -v $(UDEV_RULES_TO_INSTALL) $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK); \
	        chmod 644 $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK); \
	      fi; \
	    fi \
	  else \
	    echo "Copying new udev rules file $(UDEV_RULES_TO_INSTALL) to $(UDEV_RULES_D)/."; \
	    cp $(UDEV_RULES_TO_INSTALL) $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK); \
	    chmod 644 $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK); \
	  fi \
	else \
	  echo "WARNING:"; \
	  echo "    No udev rules directory found!"; \
	  echo ""; \
	  echo "    If you want the device nodes to be automatically created by udev,"; \
	  echo "    you manually have to add appropriate udev rules."; \
	  echo ""; \
	  echo "    See the README file from the driver package for details."; \
	fi
	@echo ""
endif


.PHONY: uninstall_udev
uninstall_udev:
ifneq ($(UID),0)
	$(call make_as_root, $@)
else
  ifdef STAGED_BUILD
	  rm -rf $(UDEV_RULES_D)
  else
	  rm -f $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK)
	  rm -f $(UDEV_RULES_D)/$(UDEV_RULES_MBGCLOCK_BEFORE_055)
  endif
endif


.PHONY: install
install: install_udev


.PHONY: uninstall
uninstall: uninstall_udev

