#!/usr/bin/make -f
# debian/rules for mce using debhelper.
# Copyright © 2004-2007 Nokia Corporation.  All rights reserved.

# 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

package = mce
pkgdir = $(CURDIR)/debian/tmp

makefileversion := $$(grep -m1 '^VERSION' Makefile | sed -e 's/VERSION *:= *//')
changelogversion := $$(grep -m1 'mce (' debian/changelog | sed -e 's/mce (//;s/.*:\(.*\))/\1/;s/[)-+].*//')

CC = gcc
CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

# Use soft-float and thumb mode if enabled
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mthumb
endif

.PHONY: clean
clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	# Add here commands to clean up after the build process.
	$(MAKE) distclean

	dh_clean

.PHONY: check
check:
	@if [ x"$(makefileversion)" != x"$(changelogversion)" ]; then \
		printf "error: version-number mismatch\n"; \
		printf "Makefile version: $(makefileversion)\n"; \
		printf "changelog version: $(changelogversion)\n"; \
		exit 1; \
	fi
	@make test || echo "Self tests failed; run \"make test\" for more info"

.PHONY: build-stamp
build-stamp:
	dh_testdir

	$(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS)"

	touch build-stamp

.PHONY: build
build: build-stamp check

.PHONY: install
install:
	$(MAKE) install DESTDIR="$(pkgdir)"

.PHONY: binary-indep
binary-indep:
#nothing to see here, move along

.PHONY: binary-arch
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman
	dh_installchangelogs
	dh_install -s
	dh_installinit --name=mce -- defaults 21 79
	dh_installinit -r --no-start --name=zzinitdone -- start 99 2 3 4 5 . stop 00 0 1 2 3 4 5 6 .
	dh_strip --dbg-package=mce --dbg-package=mcetools
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

.PHONY: binary
binary: binary-indep binary-arch
