#!/usr/bin/make -f

include $(CURDIR)/debian/update-patches.mk

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

CFLAGS += -Wall -g -O$(if $(findstring noopt,$(DEB_BUILD_OPTIONS)),0,2)

configure:
	autoreconf -fi
	rm -rf autom4te.cache config.h.in~

confargs += \
	--host=$(DEB_HOST_GNU_TYPE) \
	--prefix=/usr \
	--build=$(DEB_BUILD_GNU_TYPE)

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
confargs += --host=$(DEB_HOST_GNU_TYPE)
endif

ifeq ($(DEB_HOST_ARCH_OS),linux)
confargs += --enable-abstract-sockets
endif

build: build-stamp
build-stamp: configure
	dh_testdir
	./configure $(confargs)
	$(MAKE)
	touch $@

clean:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_clean build-stamp install-stamp INSTALL aclocal.m4 config.guess config.h.in config.sub configure depcomp install-sh ltmain.sh missing
	find -name Makefile.in -delete

install: install-stamp
install-stamp: build
	dh_testdir
	dh_testroot
	dh_clean
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	install -d -m755 debian/tmp/etc/osso-af-init
	install -m755 debian/sapwood-server.sh debian/tmp/etc/osso-af-init
	touch $@

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdirs -s
	dh_install -s --sourcedir=debian/tmp
	dh_installdocs -s
	dh_installchangelogs -s ChangeLog
	dh_strip -s --dbg-package=gtk2-engines-sapwood-dbg
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -s
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

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