#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

# These are used for cross-compiling and for saving the configure
# script
# from having to guess our platform (since we know it already)
export DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# Autoconf 2,52 and higher goes into crosscompiling mode when --host
# is specified, so only pass --host when it is necessary.
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  CONFIGURE_FLAGS := --build $(DEB_HOST_GNU_TYPE)
else
  CONFIGURE_FLAGS := --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	MAKE_ENVVARS := EXTRA_HC_OPTS=-O0
else
	MAKE_ENVVARS := EXTRA_HC_OPTS=-O2
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	cp /usr/share/misc/config.sub   lvm/src/config/tests/
	cp /usr/share/misc/config.guess lvm/src/config/tests/
	cd lvm/src && ./configure --prefix /usr \
	    --host $(DEB_HOST_GNU_TYPE) 
	cd helium && ./configure --prefix=/usr \
	    --bindir=/usr/lib/helium/bin \
	    --with-ag=/usr/bin/uuagc --with-ghc=/usr/bin/ghc \
	    $(CONFIGURE_FLAGS) 
	touch configure-stamp


build: build-stamp
build-stamp: configure-stamp 
	dh_testdir
	$(MAKE_ENVVARS) $(MAKE) -C lvm/src/runtime depend
	$(MAKE_ENVVARS) $(MAKE) -C helium/src ag
	$(MAKE_ENVVARS) $(MAKE) -C helium/src
	touch $@

clean: clean-patched
clean-patched:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	[ ! -f helium/src/Makefile ] || $(MAKE) -C helium/src clean
	rm -f lvm/src/config/tests/config.sub
	rm -f lvm/src/config/tests/config.guess
	rm -f lvm/src/runtime/.depend
	rm -f helium/config.cache helium/config.log \
	    helium/config.status helium/lib/Makefile \
	    helium/lib/simple/Makefile helium/src/Makefile \
	    helium/src/Makefile.bak \
	    helium/src/utils/OSSpecific.hs lvm/src/config/makefile \
	    lvm/src/config/m.h lvm/src/config/s.h
	rm -f helium/src/codegeneration/CodeGeneration.hs
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) -C helium/src install DESTDIR=$(CURDIR)/debian/helium
	install debian/scripts/helium   $(CURDIR)/debian/helium/usr/bin
	install debian/scripts/lvmrun   $(CURDIR)/debian/helium/usr/bin
	install debian/scripts/texthint $(CURDIR)/debian/helium/usr/bin

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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