#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

include /usr/share/quilt/quilt.make

# some default definitions, important!
#
# Name of the source package
psource:=i915-4.2-3.13-source
pdkms:=i915-4.2-3.13-dkms
sname:=i915-4.2-3.13
sversion:=$(shell dpkg-parsechangelog|grep "^Version:"|cut -d" " -f2|rev|cut -d- -f2-|rev|cut -d':' -f2)

### KERNEL SETUP
### Setup the stuff needed for making kernel module packages
### taken from /usr/share/kernel-package/sample.module.rules

# prefix of the target package name
PACKAGE=i915-4.2-3.13-dkms
# modifieable for experiments or debugging m-a
MA_DIR ?= /usr/share/modass
# load generic variable handling
-include $(MA_DIR)/include/genels ric.make
# load default rules, including kdist, kdist_image, ...
-include $(MA_DIR)/include/common-rules.make

# module assistant calculates all needed things for us and sets
# following variables:
# KSRC (kernel source directory), KVERS (kernel version string), KDREV
# (revision of the Debian kernel-image package), CC (the correct
# compiler), VERSION (the final package version string), PKGNAME (full
# package name with KVERS included), DEB_DESTDIR (path to store DEBs)

# The kdist_configure target is called by make-kpkg modules_config and
# by kdist* rules by dependency. It should configure the module so it is
# ready for compilation (mostly useful for calling configure).
# prep-deb-files from module-assistant creates the neccessary debian/ files
kdist_configure: prep-deb-files

# the kdist_clean target is called by make-kpkg modules_clean and from
# kdist* rules. It is responsible for cleaning up any changes that have
# been made by the other kdist_commands (except for the .deb files created)
kdist_clean: clean
	$(MAKE) $(MFLAGS) -f debian/rules clean

### end  KERNEL SETUP

build: patch

clean: clean-patched unpatch
clean-patched:
	dh_testdir
	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	# Create the directories to install the source into
	dh_installdirs -p$(psource)  usr/src/modules/$(sname)/debian
	dh_installdirs -p$(pdkms)  usr/src/$(sname)-$(sversion)
	# Copy only the driver source to the proper locations
	cp -a Makefile include drivers \
	    debian/$(psource)/usr/src/modules/$(sname)
	cp -a Makefile include drivers debian/$(pdkms)/usr/src/$(sname)-$(sversion)
	# Copy the needed debian/ pieces to the proper location
	cp debian/*.modules.in* \
		debian/$(psource)/usr/src/modules/$(sname)/debian
	install -m 0755 debian/rules.modules debian/$(psource)/usr/src/modules/$(sname)/debian/rules
	cd debian/$(psource)/usr/src && tar c modules | bzip2 -9 > $(sname).tar.bz2 && rm -rf modules
	# Prepare dkms.conf from the dkms.conf.in template
	sed "s/__VERSION__/$(sversion)/g" debian/dkms.conf.in > debian/$(pdkms)/usr/src/$(sname)-$(sversion)/dkms.conf
	dh_install

binary-arch: build install

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_install
	dh_modaliases
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install kdist kdist_configure kdist_image kdist_clean
