#!/usr/bin/make -f
# 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 ' ' -f 2)

ifeq ($(ATH_RATE),)
    ATH_RATE=ath_rate/sample
endif

ifeq ($(ATH_RATE),ath_rate/sample)
    ATH_RATE_VERSION=sample
endif
ifeq ($(ATH_RATE),ath_rate/amrr)
    ATH_RATE_VERSION=amrr
endif
ifeq ($(ATH_RATE),ath_rate/onoe)
    ATH_RATE_VERSION=onoe
endif

PKG_VERSION=$(EPOCH)$(VERSION)-$(ATH_RATE_VERSION)+$(REV)

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

ifeq ($(ARCH),)
	ARCH=$(shell dpkg --print-architecture)
endif

ifeq ($(ARCH),powerpc)
	ARCH_TARGET=powerpc-eabi
else 
	ARCH_TARGET=$(ARCH)-elf
endif  

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 module
	dh_testdir

module: 
	$(MAKE) -C $(CURDIR)  KERNELPATH=$(KSRC) KERNELRELEASE=${KVERS} TARGET=$(ARCH_TARGET)

install: build
	dh_testdir
	dh_testroot
	dh_clean -k

	sed -e 's/#KVERS#/$(KVERS)/g' \
	    -e 's/#VERSION#/$(VERSION)/g' debian/control.template > $(CURDIR)/debian/control

	$(MAKE) -f debian/rules install-`echo ${KVERS} | sed -e 's/\([0-9]*\.[0-9]*\).*/\1/'`

install-2.4:
	dh_installdirs                            lib/modules/$(KVERS)/kernel/drivers/net
	dh_install ath/ath_pci.o                  lib/modules/$(KVERS)/kernel/drivers/net
	dh_install ath_hal/ath_hal.o              lib/modules/$(KVERS)/kernel/drivers/net
	dh_install net80211/wlan.o                lib/modules/$(KVERS)/kernel/drivers/net
	dh_install net80211/wlan_wep.o            lib/modules/$(KVERS)/kernel/drivers/net
	dh_install net80211/wlan_tkip.o           lib/modules/$(KVERS)/kernel/drivers/net
	dh_install net80211/wlan_ccmp.o           lib/modules/$(KVERS)/kernel/drivers/net
	dh_install net80211/wlan_xauth.o          lib/modules/$(KVERS)/kernel/drivers/net
	dh_install net80211/wlan_acl.o            lib/modules/$(KVERS)/kernel/drivers/net
ifeq ($(ATH_RATE),ath_rate/onoe)
	dh_install ath_rate/onoe/ath_rate_onoe.o  lib/modules/$(KVERS)/kernel/drivers/net
endif
ifeq ($(ATH_RATE),ath_rate/amrr)
	dh_install ath_rate/amrr/ath_rate_amrr.o  lib/modules/$(KVERS)/kernel/drivers/net 
endif
ifeq ($(ATH_RATE),ath_rate/sample)
	dh_install ath_rate/sample/ath_rate_sample.o  lib/modules/$(KVERS)/kernel/drivers/net 
endif

install-2.6:
	dh_installdirs                            lib/modules/$(KVERS)/kernel/drivers/net
	dh_install ath/ath_pci.ko                 lib/modules/$(KVERS)/kernel/drivers/net
	dh_install ath_hal/ath_hal.ko             lib/modules/$(KVERS)/kernel/drivers/net
	dh_install net80211/wlan.ko               lib/modules/$(KVERS)/kernel/drivers/net
	dh_install net80211/wlan_wep.ko           lib/modules/$(KVERS)/kernel/drivers/net 
	dh_install net80211/wlan_tkip.ko          lib/modules/$(KVERS)/kernel/drivers/net 
	dh_install net80211/wlan_ccmp.ko          lib/modules/$(KVERS)/kernel/drivers/net 
	dh_install net80211/wlan_xauth.ko         lib/modules/$(KVERS)/kernel/drivers/net 
	dh_install net80211/wlan_acl.ko           lib/modules/$(KVERS)/kernel/drivers/net  
ifeq ($(ATH_RATE),ath_rate/onoe)
	dh_install ath_rate/onoe/ath_rate_onoe.ko  lib/modules/$(KVERS)/kernel/drivers/net
endif
ifeq ($(ATH_RATE),ath_rate/amrr)
	dh_install ath_rate/amrr/ath_rate_amrr.ko  lib/modules/$(KVERS)/kernel/drivers/net 
endif
ifeq ($(ATH_RATE),ath_rate/sample)
	dh_install ath_rate/sample/ath_rate_sample.ko  lib/modules/$(KVERS)/kernel/drivers/net 
endif

binary_modules: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf	
	dh_installdocs
#	dh_installexamples
#	dh_installmanpages
#	dh_undocumented
	dh_installmodules
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
# You may want to make some executables suid here.
#	dh_suidregister
#	dh_makeshlibs
	dh_installdeb
#	dh_shlibdeps
	dh_gencontrol -- -v$(PKG_VERSION)
	dh_md5sums
	dh_builddeb --destdir=$(CURDIR)/../..


clean:
	dh_testroot
	rm -f configure-stamp
	rm -f config.mk
	$(MAKE) clean TARGET=$(ARCH_TARGET)

	-dh_clean

	-rm $(CURDIR)/debian/control


# Prepares the package for distribution.  Intended for the kernel
# maintainer.
kdist: kdist_clean kdist_config kdist_image

# 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
# seperate binary-modules target to make testing the package building
# easier
kdist_image: binary_modules clean


# 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: clean


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