#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1


TARGETS = console
CFLAGS = -Wall

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

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -g
endif
ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
    INSTALL_PROGRAM += -s
endif


build-arch: build
build-indep: build

build: build-stamp
build-stamp: $(TARGETS:%=build-%)
	dh_testdir
	touch $@

# Unlike many other debian/rules files, this `configure' target refers to the
# actual file, not a phony target for running configure.
configure: configure.in
	autoconf

build-console/config.status: configure
	dh_testdir
	cp -vf /usr/share/misc/config.sub /usr/share/misc/config.guess .
	[ -d build-console ] || mkdir build-console
	cd build-console && \
	../configure $(confflags) CFLAGS="$(CFLAGS)" \
				  --prefix=/usr \
				  --mandir=\$${prefix}/share/man \
				  --with-libxml2 \
				  --with-expat \
				  --with-included-expat=no \
				  --with-neon \
				  --with-included-neon=no \
				  --with-ssl=gnutls

build-console/Makefile: build-console/config.status Makefile.in
	dh_testdir
	cd build-console && CONFIG_FILES=Makefile CONFIG_HEADERS=./config.status

build-console: build-console-stamp
build-console-stamp: build-console/Makefile
	dh_testdir
	$(MAKE) -C build-console
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp build-console-stamp \
	      install-stamp install-console-stamp

	[ ! -f Makefile ] || $(MAKE) distclean

	rm -rf build-console
	rm -f config.guess config.sub po/*.gmo configure
	# Restore *.po files; delete "Language" line added by build process
	sed -i '/Language:/d' po/*.po
	dh_clean

install: install-stamp
install-stamp: $(TARGETS:%=install-%)
	dh_testdir
	dh_testroot
	touch $@

install-console: install-console-stamp
install-console-stamp: build-console-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) -C build-console install prefix=$(CURDIR)/debian/sitecopy/usr
	rm -rf $(CURDIR)/debian/sitecopy/usr/doc

binary-indep: build install

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples doc/examplerc doc/changes.awk
	dh_installman doc/sitecopy.1
	dh_installinfo
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

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