#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
-include /usr/share/dpkg/buildtools.mk

binaries := $(shell dh_listpackages)

CC := $(CC) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64

CFLAGS := -g -Wall -W
INSTALL_PROGRAM := install

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O2
endif

build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp:
	./configure --exec-prefix=`pwd`/debian/mbr/ --prefix=`pwd`/debian/mbr/usr --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
	$(MAKE) CC="$(CC)" LD="$(LD)" CFLAGS="$(CFLAGS)"
	touch build

clean:
	dh_testdir
	dh_testroot
	dh_clean
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f build

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -p mbr-udeb sbin
	dh_installdocs -p mbr debian/README-1st.Debian NEWS README AUTHORS
	dh_installchangelogs
	$(MAKE) install INSTALL_PROGRAM="$(INSTALL_PROGRAM)"
ifneq ($(filter mbr-udeb,$(binaries)),)
	cp `pwd`/debian/mbr/sbin/install-mbr `pwd`/debian/mbr-udeb/sbin/
endif

binary-indep: build
	dh_testdir

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_strip
	dh_compress
	dh_fixperms
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: binary binary-arch binary-indep clean
