#!/usr/bin/make -f
# Based on sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# The rules file for the OpenAFS module build.

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

# Tell Autoconf the correct system types.
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
    SYSTEM = --build $(DEB_HOST_GNU_TYPE)
else
    SYSTEM = --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

# Determine whether we're building with optimization.  This doesn't really
# work at the moment due to upstream problems.
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    DEBIAN_OPT_FLAGS = --disable-optimize --disable-optimize-lwp
    DEBIAN_KERN_FLAGS = --disable-optimize-kernel
else
    DEBIAN_OPT_FLAGS =
    DEBIAN_KERN_FLAGS =
endif

# Find kernel build information.
ifndef KSRC
    KSRC = /usr/src/linux
endif
ifndef KPKG_DEST_DIR
    KPKG_DEST_DIR = ..
endif
KVERS := $(shell perl debian/kernel-version $(KSRC))
export KSRC 
export KVERS
export KPKG_DEST_DIR

SYS_NAME  := $(shell KSRC="$(KSRC)" KVERS="$(KVERS)" ARCH="$(ARCH)" \
		sh debian/sysname)

package    = openafs
modulepkg := openafs-modules-$(KVERS)${INT_SUBARCH}
moduledir := debian/$(modulepkg)/lib/modules/$(KVERS)/fs

configure: configure-modules-stamp
configure-stamp:
	@if test x"$(SYS_NAME)" = x"UNKNOWN" ; then exit 1 ; fi
	@if test x"$(KVERS)" = x ; then \
	    echo 'No version in $(KSRC)/include/linux/version.h' >&2 ; \
	    exit 1 ; \
	fi
	sh debian/prep-modules $(KSRC)
	sh configure --with-afs-sysname=$(SYS_NAME) \
	    --with-linux-kernel-headers=$(KSRC) --prefix=`pwd`/debian/tmp \
	    --with-linux-kernel-packaging --enable-disconnected \
	    --enable-debug --enable-debug-lwp --enable-debug-kernel \
	    $(DEBIAN_OPT_FLAGS) $(DEBIAN_KERN_FLAGS)
	touch $@

build: build-arch build-indep
build-arch: build-stamp
build-indep:
build-stamp: configure-stamp
	$(MAKE)
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -rf src/libafs/MODLOAD-* *-stamp
	dh_clean Makefile config.log config.status src/libafs/Makefile \
	    src/libafs/Makefile.common src/libafs/asm src/libafs/asm-generic \
	    src/libafs/h src/libafs/linux src/libafs/net src/libafs/netinet \
	    src/libafs/rpc src/libafs/sys

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	cd src/libafs && make install DESTDIR=$(CURDIR)/debian/tmp
	install -d -g root -o root -m 755 $(moduledir)
	install -g root -o root -m 644 \
	    debian/tmp/lib/modules/*/extra/openafs/*o $(moduledir)/
	touch $@

binary: binary-arch binary-indep
binary-indep:
binary-arch: install-stamp
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installmodules
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v`cat debian/VERSION`
	dh_md5sums
	dh_builddeb --destdir="$(KPKG_DEST_DIR)"

####################  Module package support (make-kpkg)  ####################

kdist_configure: configure-stamp

kdist_image: build-stamp
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary-arch
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

kdist: build-stamp
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary-arch
	KSRC="$(KSRC)" KMAINT="$(KMAINT)" KEMAIL="$(KEMAIL)" \
	    sh -v debian/genchanges
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

kdist_clean:
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

.PHONY: binary binary-arch binary-indep build build-arch build-indep clean
.PHONY: configure install kdist kdist_clean kdist_configure kdist_image
