#!/usr/bin/make -f
# -*- makefile -*-

# This has to be exported to make some magic below work.
export DH_OPTIONS

PKG_DIR=base64

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

CFLAGS = -g

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

# Use hardware floating point
ifneq (,$(findstring vfp,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mfpu=vfp -mfloat-abi=softfp
else
# Use soft-float and thumb mode if no hard float enabled
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mthumb
endif
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	rm -f config.cache
	CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --libexecdir=/usr/bin --disable-profiling --disable-static
	touch configure-stamp

build: configure build-stamp
build-stamp:
	dh_testdir
	$(MAKE)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	-rm -f configure-stamp build-stamp 
	-$(MAKE) distclean
	find -type d -name autom4te.cache -print0 | xargs -0 -r rm -rf \;
	-rm -f platform.h auto-include.h
	find -type d -name build -print0 | xargs -0 -r rm -rf \;
	find \( -name config.sub -o -name config.guess \) -print0 | xargs -0 -r rm -f \;

# 	Fix permissions
	if [ -r debian/executable.files ] ; then \
	        find -type d -print0 | xargs -0 -r chmod 755 ;\
	        find -type f ! -name 'rules' -print0 | xargs -0 -r chmod 644 ;\
	        xargs -t -r chmod +x < debian/executable.files ;\
	fi

# 	Final static cleanups
	if [ -r debian/deletable.files ] ; then \
	        xargs -t -r rm -f < debian/deletable.files ;\
	fi

	rm -rf debian/base64
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
#	The program makefile does not know to create these, wierd
	mkdir -p $(CURDIR)/debian/tmp/usr/bin
	mkdir -p $(CURDIR)/debian/tmp/usr/man/man1
#	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp prefix=$(CURDIR)/debian/tmp
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp


binary-indep: build install
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a
	dh_install --sourcedir=debian/tmp -v
#	dh_install -v
	dh_link -a
	dh_strip -a --dbg-package=base64
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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