ifndef TP_MODULES
# This part runs as a normal, top-level Makefile:
X:=$(shell false)
PWD         := $(shell pwd)
#KVER        := $(shell uname -r)
KBASE       := /lib/modules/$(KVER)
KSRC        ?= $(PWD)
#KBUILD      := $(KBASE)/build
KBUILD      := $(KSRC)
MOD_DIR     := $(KBASE)/kernel
IDIR        := include/linux
TP_DIR      := drivers/misc
TP_MODULES  := thinkpad_acpi.o
SHELL       := /bin/bash

DEBUG := 0

obj-m  := $(TP_MODULES)

.PHONY: default clean modules load unload install patch check_hdaps mk-hdaps.diff
export TP_MODULES

#####################################################################
# Main targets

default: modules

# Build the modules thinkpad_ec.ko, tp_smapi.ko and (if HDAPS=1) hdaps.ko
modules: $(KBUILD) $(patsubst %.o,%.c,$(TP_MODULES))
	$(MAKE) -C $(KBUILD) M=$(PWD) O=$(KBUILD) modules

clean:
	rm -f *.mod.* *.o *.ko *.cmd 
	rm -fr .??* Modules.symvers

load: check_hdaps unload modules
	@( [ `id -u` == 0 ] || { echo "Must be root to load modules"; exit 1; } )
	{ insmod ./thinkpad_acpi.ko; }; :
	@echo -e '\nRecent dmesg output:' ; dmesg | tail -10

unload:
	@( [ `id -u` == 0 ] || { echo "Must be root to unload modules"; exit 1; } )
	if lsmod | grep -q '^thinkpad_acpi '; then rmmod thinkpad_acpi; fi

install: modules
	@( [ `id -u` == 0 ] || { echo "Must be root to install modules"; exit 1; } )
	rm -f $(MOD_DIR)/extra/thinkpad_acpi.ko
	$(MAKE) -C $(KBUILD) M=$(PWD) O=$(KBUILD) modules_install
	depmod -a

else
#####################################################################
# This part runs as a submake in kernel Makefile context:

EXTRA_CFLAGS := $(CFLAGS) -I$(M)/include
obj-m        := $(TP_MODULES)

endif
