#!/usr/bin/make -f

#export DH_VERBOSE=1
export DH_COMPAT=2

# Parse DEB_BUILD_OPTIONS as in the policy manual, chapter 11
CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif
export CFLAGS

# Work around outdated configure.guess (still need an up-to-date configure.sub)
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


PWD=$(shell pwd)

build: patch build-stamp
build-stamp:
	dh_testdir

	mkdir build
	cd build && ../configure \
		--build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE) \
		--enable-color-interface \
		--prefix=/usr --mandir=/usr/share/man
	$(MAKE) -C build

	touch build-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	-rm -f build-stamp install-stamp

	-rm -rf build

	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) -C build install DESTDIR=$(PWD)/debian/mikmod

	touch install-stamp

patch: patch-stamp
patch-stamp:
	set -e; \
	if [ -d debian/patches ]; then \
	  for patch in `ls debian/patches/*`; do \
	    echo Patching with $$patch..; \
	    patch -p1 <$$patch; \
	  done; \
	fi

	touch patch-stamp

unpatch:
	set -e; \
	if [ -f patch-stamp ]; then \
	  if [ -d debian/patches ]; then \
	    for patch in `ls -r debian/patches/*`; do \
	      patch -R -p1 <$$patch; \
	    done; \
	  fi; \
	fi
	
	-rm -f patch-stamp

# Build architecture-independent files here.
binary-indep: build install

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installchangelogs
ifneq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip
endif
	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 patch unpatch

