#!/usr/bin/make -f

# 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 := /lib/modules/$(shell uname -r)/build
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

# This setup must be done before running any other target if it hasn't
# already been done.
setup:
	@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)
	touch $@

%: setup
	dh $@

override_dh_auto_configure:
	sh configure --prefix=$(CURDIR)/debian/tmp \
	    --with-afs-sysname=$(SYS_NAME) \
	    --disable-linux-syscall-probing \
	    --with-linux-kernel-headers=$(KSRC) \
	    --with-linux-kernel-packaging \
	    --enable-debug --enable-debug-lwp --enable-debug-kernel \
	    $(DEBIAN_OPT_FLAGS) $(DEBIAN_KERN_FLAGS)

override_dh_auto_install:
	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)/

override_dh_clean:
	rm -rf src/libafs/MODLOAD-* *-stamp
	dh_clean Makefile config.log config.status setup 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

override_dh_gencontrol:
	dh_gencontrol -- -v`cat debian/VERSION`

override_dh_builddeb:
	dh_builddeb --destdir="$(KPKG_DEST_DIR)"

kdist_configure: override_dh_auto_configure

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

kdist:
	$(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: kdist kdist_clean kdist_configure kdist_image
