#!/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-lwp-optimize
    DEBIAN_KERN_FLAGS = --disable-kernel-optimize
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)" 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 \
	    --enable-debug --enable-lwp-debug --enable-kernel-debug \
	    $(DEBIAN_OPT_FLAGS) $(DEBIAN_KERN_FLAGS)
	touch configure-stamp

build: build-arch build-indep
build-arch: build-arch-stamp
build-indep:
build-arch-stamp: configure-stamp
	make only_libafs
	touch build-arch-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp configure-stamp
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_clean

install: build-arch-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	cd src/libafs && make install
	install -d -g root -o root -m 755 $(moduledir)
	install -g root -o root -m 755 debian/tmp/lib/openafs/*o $(moduledir)/
	if test -f $(moduledir)/openafs.o \
	        -a ! -f $(moduledir)/openafs.mp.o ; then \
	    ln $(moduledir)/openafs.o $(moduledir)/openafs.mp.o || exit 1 ; \
	fi

binary: binary-arch binary-indep
binary-indep:
binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs NEWS
	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-arch-stamp
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary-arch
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

kdist: build-arch-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
