#!/usr/bin/make -f

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

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

CC = gcc
LIBDIR = /usr/lib
ROOTLIBDIR = /lib

ifneq (,$(findstring $(DEB_HOST_ARCH), s390 sparc))
#BUILD_BIARCH = yes
BIARCH_CC = "gcc -m64"
BIARCH_LIBDIR = /usr/lib64
BIARCH_ROOTLIBDIR = /lib64
endif

BUILD_DIR = debian/build
STAMPS_DIR = debian/stamps

$(BUILD_DIR) $(STAMPS_DIR):
	@[ -d $@ ] || mkdir $@

source setup build: $(BUILD_DIR) $(STAMPS_DIR)

srcfiles := $(filter-out debian, $(wildcard * .[^.]*))
source: $(STAMPS_DIR)/source
$(STAMPS_DIR)/source:
	dh_testdir
	@rm -rf $(BUILD_DIR)/source
	mkdir $(BUILD_DIR)/source
	cp -a $(srcfiles) $(BUILD_DIR)/source
	touch $@

setup: $(STAMPS_DIR)/setup
ifdef BUILD_BIARCH
setup: $(STAMPS_DIR)/setup-biarch
endif
setup: $(STAMPS_DIR)/setup-source

$(STAMPS_DIR)/setup: $(STAMPS_DIR)/source
	@rm -rf $(BUILD_DIR)/build
	cp -al $(BUILD_DIR)/source/ $(BUILD_DIR)/build
	cd $(BUILD_DIR)/build && \
	./configure \
		--kernel_src=/usr \
		--libdir=$(LIBDIR)
	touch $@

$(STAMPS_DIR)/setup-biarch: $(STAMPS_DIR)/source
	@rm -rf $(BUILD_DIR)/build
	cp -al $(BUILD_DIR)/source/ $(BUILD_DIR)/build-biarch
	cd $(BUILD_DIR)/build-biarch && \
	./configure \
		--kernel_src=/usr \
		--libdir=$(BIARCH_LIBDIR)
	touch $@

$(STAMPS_DIR)/setup-source: OUT_DIR = $(BUILD_DIR)/build-source/modules/redhat-cluster
$(STAMPS_DIR)/setup-source: $(STAMPS_DIR)/source
	@rm -rf $(BUILD_DIR)/build-source
	mkdir -p $(OUT_DIR)/include/cluster
	cp -al $(BUILD_DIR)/source/cman-kernel/src $(OUT_DIR)/cman
	cp -al $(BUILD_DIR)/source/dlm-kernel/src $(OUT_DIR)/dlm
	cp -al $(BUILD_DIR)/source/gfs-kernel/src $(OUT_DIR)/gfs
	echo "obj-m = cman/ dlm/ gfs/" >> $(OUT_DIR)/Makefile
	echo "obj-m = dlm/ gfs/ gulm/ harness/ nolock/" >> $(OUT_DIR)/gfs/Makefile
	$(foreach name, cman dlm, \
	rm $(OUT_DIR)/$(name)/Makefile; \
	(echo "export USING_KBUILD=1"; cat $(BUILD_DIR)/source/$(name)-kernel/src/Makefile) > $(OUT_DIR)/$(name)/Makefile; \
	ln -s ../include/cluster $(OUT_DIR)/$(name);)
	$(foreach name, dlm gfs gulm harness nolock, \
	rm $(OUT_DIR)/gfs/$(name)/Makefile; \
	(echo "export USING_KBUILD=1"; cat $(BUILD_DIR)/source/gfs-kernel/src/$(name)/Makefile) > $(OUT_DIR)/gfs/$(name)/Makefile; \
	ln -s ../../include/cluster ../../include/linux $(OUT_DIR)/gfs/$(name);)
	cd $(OUT_DIR)/include/cluster; ln -sf ../../*/*.h ../../*/*/*.h .
	ln -s cluster $(OUT_DIR)/include/linux
	touch $@

build: $(STAMPS_DIR)/build
ifdef BUILD_BIARCH
build: $(STAMPS_DIR)/build-biarch
endif
build: $(STAMPS_DIR)/build-source

$(STAMPS_DIR)/build: $(STAMPS_DIR)/setup
	dh_testdir

	$(MAKE) -C $(BUILD_DIR)/build CC=$(CC)

	touch $@

$(STAMPS_DIR)/build-biarch: $(STAMPS_DIR)/setup-biarch
	dh_testdir

	$(MAKE) -C $(BUILD_DIR)/build-biarch CC=$(BIARCH_CC)

	touch $@

$(STAMPS_DIR)/build-source: $(STAMPS_DIR)/setup-source
	dh_testdir
	cd $(BUILD_DIR)/build-source; tar -cjf redhat-cluster.tar.bz2 modules
	touch $@

clean:
	dh_testdir
	rm -rf $(BUILD_DIR) $(STAMPS_DIR)
	dh_clean 

install:
	dh_testdir
	dh_testroot
	dh_clean -k

	$(MAKE) -C $(BUILD_DIR)/build install DESTDIR=$(CURDIR)/$(BUILD_DIR)/install

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install -i --sourcedir=$(BUILD_DIR)
	dh_installchangelogs -i
	dh_installdocs -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install -s --sourcedir=$(BUILD_DIR)
	dh_installchangelogs -s
	dh_installdocs -s
	dh_installinit -p ccs -p ccs -- start 61 S . start 5 0 6 .
	dh_installinit -p cman --no-restart-on-upgrade -- start 62 S . start 4 0 6 .
	dh_installinit -p fence --no-restart-on-upgrade -- start 63 S . start 3 0 6 .
	dh_link -s
	dh_strip -s
	dh_compress -s 
	dh_fixperms -s
	dh_makeshlibs -s -V
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install
