#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# This is the debhelper compatability version to use.
export DH_COMPAT=2

# Conveniently overwritable from the environment
SRC_DIR:=/usr/src
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)
PKG_VERSION=$(EPOCH)$(VERSION)+$(REV)

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

useragent="Debian GNU/Linux"
ftpusername="anonymous"
ftppassword="Debian_GNU_Linux"

upstreamversion=1.0-2880
dirname=NVIDIA_kernel-$(upstreamversion)

# ARGH NVIDIA keeps changing shit
filename="NVIDIA_kernel-1.0-2880.tar.gz"

nvidiaurl=http://205.158.109.140/XFree86_40/1.0-2880/$(filename)
#nvidiaurl="ftp://$(ftpusername):$(ftppassword)@ftp2.detonator.nvidia.com/pub/drivers/english/XFree86_40/$(upstreamversion)/$(filename)"


.PHONY: configure configure-stamp
configure: configure-stamp
configure-stamp:
	if [ -f $(CURDIR)/debian/control.template ]; then \
	cp -a $(CURDIR)/debian/control.template $(CURDIR)/debian/control; \
	fi

	dh_testdir
	dh_testroot

	if [ ! -f $(SRC_DIR)/$(filename) ]; then \
		echo "NVIDIA_kernel needs to be downloaded."; \
		if which wget > /dev/null; then \
			echo "Now attempting to download NVIDIA_kernel."; \
			if ! `wget --passive-ftp -P /usr/src -U $(useragent) $(nvidiaurl)`; then \
				echo "$(filename) not on web server."; \
				exit 1; \
			fi; \
		else \
			echo "wget not found. (apt-get install wget)"; \
			exit 1; \
		fi; \
	fi

	tar -zxvf $(SRC_DIR)/$(filename)

	touch configure-stamp


.PHONY: build build-stamp
build: configure-stamp build-stamp
build-stamp:
	dh_testdir
	dh_testroot

	$(MAKE) -C $(CURDIR)/$(dirname) SYSINCLUDE=$(KSRC)/include NVdriver

	touch build-stamp


.PHONY: install
install: build
	dh_testdir
	dh_testroot
	dh_clean -k

	sed 's/#KVERS#/$(KVERS)/g' debian/control.template > $(CURDIR)/debian/control
	sed 's/#KVERS#/$(KVERS)/g' debian/dirs.template > $(CURDIR)/debian/dirs
	sed 's/#KVERS#/$(KVERS)/g' debian/override.template > $(CURDIR)/debian/override

	dh_installdirs

#	install -m 0644 $(CURDIR)/debian/devfs.devices $(CURDIR)/debian/nvidia-kernel-$(KVERS)/etc/devfs/devices.d/nvidia-kernel-$(KVERS)
	install -m 0644 $(CURDIR)/debian/override $(CURDIR)/debian/nvidia-kernel-$(KVERS)/usr/share/lintian/overrides/nvidia-kernel-$(KVERS)
	install -m 0644 $(CURDIR)/$(dirname)/NVdriver $(CURDIR)/debian/nvidia-kernel-$(KVERS)/lib/modules/$(KVERS)/kernel/drivers/video
	install -m 0644 $(CURDIR)/debian/devfs.conf $(CURDIR)/debian/nvidia-kernel-$(KVERS)/etc/devfs/conf.d/nvidia-kernel-$(KVERS)




.PHONY: binary_modules
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=$(KSRC)/..


.PHONY: clean
clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

# Add here commands to clean up after the build process.
#       -$(MAKE) clean

	dh_clean

	-rm $(CURDIR)/debian/control
	-rm $(CURDIR)/debian/dirs
	-rm $(CURDIR)/debian/override
	-rm -rf $(CURDIR)/$(dirname)


# 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)
.PHONY: kdist_config
kdist_config: configure

.PHONY: kdist_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
.PHONY: kdist_image
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).
.PHONY: kdist_clean
kdist_clean: clean

kdist: binary_modules
	# Build a .changes file.
	KVERS="$(KVERS)" KSRC="$(KSRC)" KMAINT="$(KMAINT)" \
	REV="$(REV)" KEMAIL="$(KEMAIL)" \
	sh -v debian/genchanges.sh
	# Only now can we clean up.
	debian/rules clean
							
