MODULES = vmmon vmnet
SUBDIRS = $(MODULES:%=%-only)
MODFILES = $(foreach mod,$(MODULES),$(mod)-only/$(mod).ko)
VM_UNAME = $(shell uname -r)
MODDIR = /lib/modules/$(VM_UNAME)/misc
VMWARE_SOURCE = /usr/lib/vmware/modules/source
MODINFO = /sbin/modinfo
DEPMOD = /sbin/depmod

.PHONY: subdirs $(SUBDIRS) clean tarballs install

# Build the modules
all: subdirs tarballs

subdirs: $(SUBDIRS)

$(SUBDIRS):
	$(MAKE) -C $@ $(MAKECMDGOALS)

# Check module versions before installing
versioncheck: $(MODFILES)
	@for f in $(MODFILES); do \
	    mver=$$($(MODINFO) -F vermagic $$f);\
	    mver=$${mver%% *};\
	    test "$${mver}" = "$(VM_UNAME)" \
	        || ( echo "Version mismatch: module $$f $${mver}, kernel $(VM_UNAME)" ; exit 1 );\
	done

# Install the compiled modules and update dependencies
install: versioncheck
	install -D -t $(DESTDIR)$(MODDIR) $(MODFILES)
	if test -z "$(DESTDIR)"; then strip --strip-debug $(MODULES:%=$(DESTDIR)$(MODDIR)/%.ko); fi
	if test -z "$(DESTDIR)"; then $(DEPMOD) -a $(VM_UNAME); fi

clean: $(SUBDIRS)
	rm -f *.o
