#!/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)
CONFFLAGS = --enable-scrollbar-code --enable-signal-handler --without-libzio \
             --disable-setenv-code --enable-off_t --enable-largefile
# some flags need to be given to make:
DIRFLAGS = appdefaultsdir=/etc/X11/app-defaults sysdocdir=/usr/share/doc

CFLAGS = -Wall -Wmissing-prototypes -g
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
CPPFLAGS =
ifneq (,$(filter debug,$(DEB_BUILD_OPTIONS)))
	CPPFLAGS = -DMESSAGES
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
        PARFLAGS = -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

clean:
	dh_testdir
	# run distclean of configure was completed or attempted
	if [ -f config.status -o -f config.log ] ; then $(MAKE) distclean ; fi
	test ! -f config.status
	dh_clean
	rm -f build-arch-stamp

config.status: configure
	dh_testdir
	./configure --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE) \
		    $(CONFFLAGS) CFLAGS='$(CFLAGS)' CPPFLAGS='$(CPPFLAGS)' \
		    LDFLAGS="-Wl,-z,defs" \
		    --bindir=/usr/bin --datarootdir=/usr/share \
		    --mandir=/usr/share/man --infodir=/usr/share/info \
	|| ( echo configure failed with $$? ; cat config.log ; exit 1 )

build-arch: build-arch-stamp
build-arch-stamp: config.status
	dh_testdir
	$(MAKE) $(PARFLAGS) $(DIRFLAGS) \
		LIBS="-lXaw3d -lXmu -lXt -lX11 -lm -lXinerama"
	touch $@

binary-arch: build-arch-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	$(MAKE) $(PARFLAGS) $(DIRFLAGS) install DESTDIR=${CURDIR}/debian/gv
	dh_install
	dh_installchangelogs ChangeLog
	dh_installdocs AUTHORS NEWS README
	dh_installinfo doc/gv.info
	dh_installmenu
	dh_installmime
	dh_strip
	dh_compress
	find debian/gv -name "dir.gz" -print -delete
	dh_fixperms
	dh_md5sums
	dh_shlibdeps
	dh_installdeb
	dh_gencontrol
	dh_builddeb

build-indep:
binary-indep:
# nothing to do...

build: build-arch
binary: binary-arch

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