#!/usr/bin/make -f
# Copyright (C) 2002-2006 Flavio Stanchina
# Licensed under the GNU General Public License, version 2.
# Adapted from nvidia-kernel-src.

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

export CCACHE_DISABLE := 1


upstream_version	:= $(shell dpkg-parsechangelog | sed -nr '/^Version:/s/Version: (.*:)?(.*)-(.*)/\2/p')

baseepoch := $(shell expr $(KDREV) : '\([0-9]*\):.*' 2>/dev/null )
ifneq ($(baseepoch),)
      EPOCH=$(baseepoch):
endif
REV := $(shell expr $(KDREV) : '[0-9]*:\(.*\)' 2>/dev/null)
ifeq ($(REV),)
     REV=$(KDREV)
endif
VERSION := $(shell dpkg-parsechangelog | grep '^Version:' | cut -d' ' -f2)
ifeq ($(REV),)
PKG_VERSION=$(EPOCH)$(VERSION)
else
PKG_VERSION=$(EPOCH)$(VERSION)+$(REV)
endif

# determine fglrx-source package version and warn user if it doesn't match
SRC_PKG_VERSION := $(shell dpkg -s fglrx-source | grep '^Version:' | cut -d' ' -f2)
ifneq ($(SRC_PKG_VERSION),$(VERSION))
$(warning *** the unpacked source ($(VERSION)) doesn't match the fglrx-source package ($(SRC_PKG_VERSION)))
$(warning *** if this is not what you want, erase $(CURDIR) and start over)
endif

# if you need to redefine these
KSRC  := $(KSRC)
KVERS := ${KVERS}

module=fglrx.ko

CHANGES_FILE=$(KPKG_DEST_DIR)/fglrx-kernel-$(KVERS)_$(PKG_VERSION)_$(ARCH).changes

configure: configure-stamp

configure-stamp:
	sed -e 's/#KVERS#/$(KVERS)/g' \
	    -e 's/#VERSION#/$(VERSION)/g' debian/control.template > debian/control
	sed 's/#KVERS#/$(KVERS)/g' debian/install.template > debian/install
	touch $@

build: configure-stamp
	dh_testdir
	dh_prep
	$(MAKE) -C $(KSRC) SUBDIRS=$(CURDIR) modules

binary_modules: build
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_install
	dh_installdocs
	dh_installchangelogs
	dh_installmodules
	dh_bugfiles
	dh_lintian
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(PKG_VERSION) -V'fglrx:version=$(upstream_version)'
	dh_md5sums
ifdef KPKG_DEST_DIR
	dh_builddeb --destdir=$(KPKG_DEST_DIR)
else
	dh_builddeb --destdir=$(CURDIR)/../..
endif

clean:
	test -f debian/control || cp debian/control.template debian/control
	dh_testroot
	rm -f configure-stamp
	rm -f $(module) fglrx.mod.c *.o
	rm -f .version .*.o.flags .*.o.d .*.o.cmd .*.ko.cmd
	rm -rf .tmp_versions
	rm -rf patch
	dh_clean debian/control debian/install

# The kdist_configure target is called by make-kpkg modules_config. It
# should configure the module so it is ready for compilation (mostly
# useful for calling configure)
kdist_config: configure

kdist_configure: configure


# the kdist_image target is called by make-kpkg modules_image. It is
# responsible for compiling the module and creating the package. It
# should also clean up after making the module. Please note we use a
# separate binary_modules target to make testing the package building
# easier
kdist_image:
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary_modules


# the kdist_clean target is called by make-kpkg modules_clean. 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:
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

kdist: kdist_clean kdist_image
	dpkg-genchanges -b -u$(KPKG_DEST_DIR) > $(CHANGES_FILE)
	-debsign -m"$(KMAINT)" $(CHANGES_FILE)

.PHONY: configure build install binary_modules clean kdist_config kdist_configure kdist_image kdist_clean kdist
