#!/usr/bin/make -f

CFLAGS = -Wall -g

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

p = debian/pecomato

configure: configure-stamp
configure-stamp:
	dh_testdir
	cp $(CURDIR)/Makefile.linux $(CURDIR)/Makefile
	patch -p0 < $(CURDIR)/debian/Makefile.patch
	touch configure-stamp

build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp: configure-stamp 
	dh_testdir
	$(MAKE) PREFIX=$(CURDIR)/$(p)/usr
	touch $@

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

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	$(MAKE) PREFIX=$(CURDIR)/$(p)/usr install
	# remove duplicated docs
	rm -rf $(CURDIR)/$(p)/usr/share/doc/pecomato/txt/ 
	rm -f \
	  $(CURDIR)/$(p)/usr/share/doc/pecomato/html/LICENSE.* \
	  $(CURDIR)/$(p)/usr/share/doc/pecomato/html/DISCLAIMER.* \
	  $(CURDIR)/$(p)/usr/share/doc/pecomato/html/INSTALL.* \
	  $(CURDIR)/$(p)/usr/share/doc/pecomato/html/TODO.* \
	  $(CURDIR)/$(p)/usr/share/doc/pecomato/html/README.*
	# relocate manpage
	mv $(CURDIR)/$(p)/usr/share/doc/pecomato/man/manpage.man \
	  $(CURDIR)/$(p)/usr/share/man/man1/pecomato.1
	rmdir $(CURDIR)/$(p)/usr/share/doc/pecomato/man

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installman
	dh_link
	dh_strip
	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 configure
