#!/usr/bin/make -f

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
  CC=$(DEB_HOST_GNU_TYPE)-gcc
endif

CFLAGS = -Wall -g

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

build: build-stamp
build-stamp:
	dh_testdir

	[ -f makefile.dos ] || (mv makefile makefile.dos && ln -s grr/Makefile Makefile)
	[ -f machine.h.dos ] || (mv machine.h machine.h.dos && ln -s grr/machine.h machine.h)

	$(MAKE)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	[ ! -f makefile.dos ] || (rm Makefile && mv makefile.dos makefile)
	[ ! -f machine.h.dos ] || (rm machine.h && mv machine.h.dos machine.h)

	rm -f .depend ha *.out *~ *.o

	dh_clean -Xacoder.bak

install: build
	dh_testdir
	dh_testroot
	dh_prep

	mkdir -p $(CURDIR)/debian/ha/usr/bin
	install -m755 ha $(CURDIR)/debian/ha/usr/bin

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installchangelogs
	dh_installman
	dh_install --sourcedir=debian/ha
	dh_strip
	dh_link
	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
