#!/usr/bin/make -f

include /usr/share/dpatch/dpatch.make

DEB_NAME=psb-kernel-source
VERSION := $(shell dpkg-parsechangelog | grep '^Version:' | cut -d' ' -f2 | cut -d- -f1 | cut -d\: -f2)
KERNEL_VER := $(shell ls -1 /lib/modules)

build: build-stamp
build-stamp: patch
	$(MAKE) LINUXDIR=/lib/modules/$(KERNEL_VER)/build DRM_MODULES="psb"
	touch $@

clean: clean-patched unpatch
clean-patched:
	dh clean

install: build install-stamp
install-stamp:
	dh_clean -k
	dh_installdirs
	mkdir $(CURDIR)/debian/$(DEB_NAME)/usr/src/$(DEB_NAME)-$(VERSION)
	cp *.c *.h Config.in Doxyfile *.txt Kconfig Makefile* Module.* \
		modules.order README.drm create_linux_pci_lists.sh \
		$(CURDIR)/debian/$(DEB_NAME)/usr/src/$(DEB_NAME)-$(VERSION)
	cat debian/dkms.conf.in | sed -e "s/#VERSION#/$(VERSION)/" > \
		$(CURDIR)/debian/$(DEB_NAME)/usr/src/$(DEB_NAME)-$(VERSION)/dkms.conf
	mkdir -p $(CURDIR)/debian/psb-modules/lib/modules/$(KERNEL_VER)/updates/char/drm
	cp psb.ko drm.ko $(CURDIR)/debian/psb-modules/lib/modules/$(KERNEL_VER)/updates/char/drm
	touch $@

binary-arch: install-stamp
	dh $@

binary-indep: install-stamp
	dh $@

binary: binary-arch binary-indep
