# Makefile for the plustek scanner driver (kernel-module)
#
###############################################################################

#
# retrieve the version numbers
#
ifeq ($(LINUXVERSION),)
 LINUXVERSION = $(shell uname -r)
endif
LINUXRELEASE = $(shell uname -r | cut -d'.' -f3)

ifeq ($(VERSIONSTR),)
 ifeq ($(SUBDIRS),)
  VERSIONSTR = $(shell grep "define BACKEND_VERSION" $(M)/plustek_pp.c | cut -b25-50 )
 else
  VERSIONSTR = $(shell grep "define BACKEND_VERSION" $(SUBDIRS)/plustek_pp.c | cut -b25-50 )
 endif
endif

#
# extra flags
#
EXTRA_CFLAGS += -D_PTDRV_VERSTR=\"$(VERSIONSTR)\"

ifeq ($(DEBUG),y)
	EXTRA_CFLAGS += -DDEBUG
endif

#
# the module name
#
TARGET := pt_drv
MODULE := $(TARGET).ko

#
# our files...
#
NAMES := dac detect genericio image map misc models io procfs
NAMES := $(NAMES) motor p9636 ptdrv scale tpa p48xx p12 p12ccd
NAMES := $(addprefix plustek-pp_, $(NAMES))
OBJS  := $(addsuffix .o, $(NAMES))

#
# now the kernel magic
#
ifneq ($(KERNELRELEASE),)
obj-m := $(TARGET).o

$(TARGET)-objs := $(OBJS)

else
KDIR := /lib/modules/$(shell uname -r)/build
PWD  := $(shell pwd)

default:
	$(MAKE) -C $(KDIR) M=$(PWD) modules
endif

#
# the installation stuff
#
group = "root"
mode  = "644"
INST_DIR = /lib/modules/$(LINUXVERSION)/kernel/drivers/parport

#
# copy the driver to the modules directory
#
install:
	mkdir -p $(INST_DIR)
	install -c -m $(mode) $(MODULE) $(INST_DIR)
	/sbin/depmod -a

#
#
#
uninstall:
	rm -f $(INST_DIR)/$(MODULE)

#
# use modprobe to load the driver, remember to set the
# parameter in /etc/conf.modules (see INSTALL for more details)
#
load:   $(INST_DIR)/$(MODULE)
# invoke modprobe with all arguments we got
	/sbin/modprobe $(TARGET) || exit 1

# Remove stale nodes and replace them, then give gid and perms
	rm -f /dev/$(TARGET)*

# when using the devfs support, we check the /dev/scanner entries
# and only create links to the devfs nodes
# at least we create one link
	@if [ -e /dev/scanner/$(TARGET)* ]; then 				\
		ln -s /dev/scanner/$(TARGET)0 /dev/$(TARGET);		\
		for name in `ls /dev/scanner | grep $(TARGET)`; do	\
			ln -s /dev/scanner/$$name /dev/$$name ;     	\
		done												\
	else                      								\
		mknod /dev/$(TARGET) c `cat /proc/devices | sed -ne "s/\([0-9]*\) pt_drv/\1/p"` 0;  \
		mknod /dev/$(TARGET)0 c `cat /proc/devices | sed -ne "s/\([0-9]*\) pt_drv/\1/p"` 0;	\
		mknod /dev/$(TARGET)1 c `cat /proc/devices | sed -ne "s/\([0-9]*\) pt_drv/\1/p"` 1;	\
		mknod /dev/$(TARGET)2 c `cat /proc/devices | sed -ne "s/\([0-9]*\) pt_drv/\1/p"` 2; \
		mknod /dev/$(TARGET)3 c `cat /proc/devices | sed -ne "s/\([0-9]*\) pt_drv/\1/p"` 3;	\
																							\
		chgrp $(group) /dev/$(TARGET)*;	\
		chmod $(mode)  /dev/$(TARGET)*;	\
	fi

#
# unload the driver
#
unload:
	/sbin/modprobe -r $(TARGET) || exit 1

# Remove stale nodes
	rm -f /dev/$(TARGET)*

#
# cleanup the show
#
clean:
	@-rm -f *.o .depend depend dep $(MODULE) $(TARGET).o $(TARGET).mod.c .*.cmd
