#  Template Makefile for external module compilation

KDIR      := /lib/modules/$(shell uname -r)/build
PWD       := $(shell pwd)
KERNEL_24 := $(if $(wildcard $(KDIR)/Rules.make),1,0)

ifeq ($(KERNEL_24),1)
$(error Need to be a 2.6 kernel)
endif

ifneq ($(KERNELRELEASE),)
# We're in kbuild, so follow its rules

obj-m                    := lpvi.o

else

# We aren't in the kbuild system yet, so invoke it

CUPS_LIBS := $(shell cups-config --image --libs)
CUPS_FLAGS:= $(shell cups-config --cflags)

ifeq ($(CUPS_LIBS),)
$(error Need cups-config)
endif

.PHONY: all module cups clean install install-module install-cups
all: module cups
install: install-module install-cups
module: lpvi.ko
cups: lpvi

lpvi: lpvi-backend.o $(CUPS_LIBS)
	$(CC) $(LDFLAGS) -o $@ $^ -laio

lpvi-backend.o: lpvi-backend.c lpvi.h
	$(CC) $(CFLAGS) $(CUPS_FLAGS) -c -o $@ lpvi-backend.c

install-cups: cups
	install lpvi /usr/lib/cups/backend
	install rastertoraster /usr/lib/cups/filter
	install -m 0644 SPARCPrinter.ppd /usr/share/cups/model
	
lpvi.ko: lpvi.c lpvi.h
	$(MAKE) -C $(KDIR) M=$(PWD)

install-module: module
	$(MAKE) -C $(KDIR) M=$(PWD) modules_install

clean:
	rm -f lpvi lpvi-backend.o
	$(MAKE) -C $(KDIR) M=$(PWD) $@

endif ### ifneq($(KERNELRELEASE),)
