#!/usr/bin/make -f
# -*- makefile -*-

DEBVERS  := $(shell dpkg-parsechangelog | grep ^Version: | cut -d' ' -f2 \
                | cut -d- -f1)
#VERSION  := $(shell echo '$(DEBVERS)' | sed -e 's/[+-].*//' -e 's/~//g')

PKG_driver  := cedarview-drm
PKG_version          := $(shell dpkg-parsechangelog | awk -F' ' '/^Version:/ {print $$2}' | awk -F- '{print $$1}')
PKG_source           := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
PKG_module      := cedarview_gfx

SRCPATH  := /var/lib/dkms/$(PKG_driver)/$(PKG_version)/build

NAME = $(shell dh_listpackages)
TMP     = $(CURDIR)/debian/tmp

PKG_kernel_source_dir := /usr/src
SRC = $(TMP)$(PKG_kernel_source_dir)
BLACKLISTS = $(TMP)/etc/modprobe.d
PKG_blacklist_file := /etc/modprobe.d/$(PKG_driver).conf

GRUB_BLACKLISTS = $(TMP)/usr/share/grub-gfxpayload-lists/blacklist

PKG_lightdm_config_dir := /etc/lightdm
PKG_lightdm_config_file := $(PKG_lightdm_config_dir)/lightdm.conf
LIGHTDM_CONF_DIR := $(TMP)$(PKG_lightdm_config_dir)
PKG_lightdm_custom_config_dir := /usr/share/$(PKG_driver)
PKG_lightdm_custom_config_file := $(PKG_lightdm_custom_config_dir)/lightdm.conf
LIGHTDM_CUSTOM_CONF_DIR := $(TMP)$(PKG_lightdm_custom_config_dir)
PKG_dkms_force_dir := /usr/share/dkms/modules_to_force_install
PKG_dkms_force_file := $(PKG_dkms_force_dir)/$(PKG_driver)
DKMS_FORCE_DIR := $(TMP)$(PKG_dkms_force_dir)

readme_list := "$(CURDIR)/temp_ids"

#These "define" bits are needed for the blacklist file
define newline


endef

define blacklist_file
# This file was installed by $(PKG_driver)
# Do not edit this file manually

blacklist psb_gfx
alias psb_gfx off
endef


%:
	dh $@ --with dkms

.PHONY: regen-from-templates
regen-from-templates:
	for i in $(CURDIR)/debian/dkms \
		 $(CURDIR)/debian/control \
		 $(CURDIR)/debian/install \
		 $(CURDIR)/debian/preinst \
		 $(CURDIR)/debian/postinst \
		 $(CURDIR)/debian/postrm \
		 $(CURDIR)/Makefile; do \
		sed -e 's|#VERSION#|$(PKG_version)|g' \
			-e 's|#SRCPATH#|$(SRCPATH)|g' \
			-e 's|#DRIVERNAME#|$(PKG_driver)|g' \
			-e "s|#DRIVERSRCNAME#|$(PKG_source)|g" \
			-e "s|#BLACKLIST_FILE#|$(PKG_blacklist_file)|g" \
			-e "s|#LIGHTDM_CONF_FILE#|$(PKG_lightdm_config_file)|g" \
			-e "s|#LIGHTDM_CONF_DIR#|$(PKG_lightdm_config_dir)|g" \
			-e "s|#LIGHTDM_CUSTOM_CONF_FILE#|$(PKG_lightdm_custom_config_file)|g" \
			-e "s|#LIGHTDM_CUSTOM_CONF_DIR#|$(PKG_lightdm_custom_config_dir)|g" \
			-e "s|#DKMS_FORCE_DIR#|$(PKG_dkms_force_dir)|g" \
		$$i.in > $$i; \
	done

override_dh_auto_build:
override_dh_auto_clean: regen-from-templates

override_dh_auto_install: regen-from-templates
	install -d "$(SRC)"
	mkdir "$(SRC)/$(NAME)-$(PKG_version)"
	cp -a `ls | grep -v debian` "$(SRC)/$(NAME)-$(PKG_version)"
	chmod 644 -R "$(SRC)/$(NAME)-$(PKG_version)"
	find "$(SRC)/$(NAME)-$(PKG_version)" -type d -exec chmod 755 {} \;

	install -d "$(BLACKLISTS)"
	# Permanently blacklist psb_gfx so that cdv can be loaded
	printf '$(subst $(newline),\n,${blacklist_file})' > \
		$(BLACKLISTS)/$(PKG_driver).conf

	# Install file to force DKMS to override modules
	install -d "$(DKMS_FORCE_DIR)"
	printf '$(PKG_driver)' > $(DKMS_FORCE_DIR)/$(PKG_driver)

	# Generate modaliases
	sed -n -e 's/.*{0x8086, 0x\([^, ]*\).*/\1/p' \
		'$(CURDIR)/debian/patches/07_kernel-3.2-cdv.diff' \
		> $(readme_list)

	install -d $(GRUB_BLACKLISTS)
	for id in $$(cat $(readme_list)); do \
		echo v8086d$$id.*; done \
	> $(GRUB_BLACKLISTS)/20_$(PKG_module)

	for id in $$(cat $(readme_list)); do \
		printf 'alias pci:v%08Xd%08Xsv*sd*bc03sc*i* %s %s\n' \
		0x8086 0x"$$id" "$(PKG_module)" "$(PKG_driver)"; done \
	> debian/$(PKG_driver).modaliases
	dh_modaliases
	rm debian/$(PKG_driver).modaliases

	rm -f $(readme_list)
