#!/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


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}

BASEVERS := $(shell echo $(KVERS) | cut -d. -f-2)
ifeq ($(BASEVERS),2.6)
module=fglrx.ko
else
module=fglrx.o
endif

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

configure: configure-stamp

configure-stamp:
	if [ -f $(CURDIR)/debian/control.template ]; then \
		cat $(CURDIR)/debian/control.template > $(CURDIR)/debian/control; \
	fi
	dh_testdir
	touch configure-stamp

build: configure-stamp make.sh
	dh_testdir
ifeq ($(BASEVERS),2.6)
	$(MAKE) -C $(KSRC) SUBDIRS=$(CURDIR) modules
else
	KERNEL_PATH=$(KSRC) uname_r=$(KVERS) ./make.sh --nohints
endif

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	rm -f $(CURDIR)/debian/control $(CURDIR)/debian/dirs
	sed -e 's/#KVERS#/$(KVERS)/g' \
	    -e 's/#VERSION#/$(VERSION)/g' debian/control.template > $(CURDIR)/debian/control
	sed -e 's/#KVERS#/$(KVERS)/g' debian/dirs.template > $(CURDIR)/debian/dirs
	dh_installdirs
	dh_install $(module) lib/modules/$(KVERS)/misc

binary_modules: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installmodules
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(PKG_VERSION)
	dh_md5sums
ifdef KPKG_DEST_DIR
	dh_builddeb --destdir=$(KPKG_DEST_DIR)
else
	dh_builddeb --destdir=$(CURDIR)/../..
endif

clean:
	dh_testroot
	rm -f configure-stamp
	rm -f $(module) fglrx.mod.c *.o libfglrx_ip.a
	rm -f .version .*.o.flags .*.o.d .*.o.cmd .*.ko.cmd
	rm -rf .tmp_versions
	rm -rf patch
	dh_clean
	rm -f $(CURDIR)/debian/control
	rm -f $(CURDIR)/debian/dirs

# 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
