#!/usr/bin/make -f
#export DH_VERBOSE=1

INSTALLROOT := `pwd`/debian/mona

# workaround for gcc-4.0 bug on m68k
ifeq ($(DEB_HOST_ARCH),m68k)
  OPTFLAGS = -O2
else			
  OPTFLAGS = -O3
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
     OPTFLAGS = -O0
endif

build: build-stamp 

build-stamp:
	dh_testdir
	(cd config;\
	 [ -h config.guess ] || { mv config.guess config.guess.upstream; \
                ln -s /usr/share/misc/config.guess; };\
         [ -h config.sub ] || { mv config.sub config.sub.upstream; \
                ln -s /usr/share/misc/config.sub; })
	dh_autoreconf
	./configure --prefix=/usr CFLAGS=$(OPTFLAGS) CXXFLAGS=$(OPTFLAGS)
	$(MAKE) CFLAGS=$(OPTFLAGS) CXXFLAGS=$(OPTFLAGS)
	touch build-stamp

clean: clean1

clean1:
	dh_testdir
	dh_testroot
	(cd config;\
	 [ ! -h  config.guess ] || mv config.guess.upstream config.guess;\
         [ ! -h  config.sub ] || mv config.sub.upstream config.sub)
	if [ -e makefile ]; then $(MAKE) distclean; fi
	-rm build-stamp
	dh_autoreconf_clean
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs 

	# the generated makefile does not honor PREFIX=...
	./configure --prefix=$(INSTALLROOT)/usr \
		--mandir=$(INSTALLROOT)/usr/share/man
	$(MAKE) install

	# get rid of rpaths
	for lib in libmonabdd.so libmonadfa.so libmonagta.so;\
		do chrpath -d $(INSTALLROOT)/usr/lib/$$lib; \
	done
	chrpath -d $(INSTALLROOT)/usr/bin/mona

	# do not install .la files
	rm $(INSTALLROOT)/usr/lib/*.la

	# make install seems to misplace this one
	-rm $(INSTALLROOT)/usr/share/mona-mode.el
	cp mona-mode.el $(INSTALLROOT)/usr/share/emacs/site-lisp/mona

	cp debian/lintian-overrides \
	  $(INSTALLROOT)/usr/share/lintian/overrides/mona


binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs README
	dh_installexamples Examples/*
	-rm $(INSTALLROOT)/usr/share/doc/mona/examples/*.o
	dh_installemacsen
	dh_installman mona.1
	dh_installchangelogs 
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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