#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DEB_HOST_MULTIARCH

SUBDIRS_MAKE = clisp mboxfs memfs socketio notice run smbfs tarfs xmlfs
# gopherfs
SUBDIRS_AUTORECONF = cvsfs filter httpfs nsmux pptop
# jfs

SUBDIRS = $(SUBDIRS_MAKE) \
          $(SUBDIRS_AUTORECONF) \
	  pith

READMEs = $(wildcard */README)
READMEs_TOINSTALL = $(patsubst %/README,%-README,$(READMEs))

CFLAGS = -Wall -g
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

CONFIGURE_FLAGS = --prefix=/usr --mandir=\$${prefix}/share/man \
	--infodir=\$${prefix}/share/info CFLAGS="$(CFLAGS)"

STAMPS = $(addprefix build-make-,$(SUBDIRS_MAKE)) \
         $(addprefix build-autoreconf-,$(SUBDIRS_AUTORECONF)) \
	 build-pith

build: build-indep build-arch
build-indep:
build-arch: build-stamp

build-stamp: $(STAMPS) $(READMEs_TOINSTALL)
	> $@

build-make-%:
	cd $* && $(MAKE)
	> $@

pptop/config.rpath:
	cp /usr/share/gettext/config.rpath pptop/

build-autoreconf-pptop: pptop/config.rpath
build-autoreconf-%:
	cd $* && autoreconf -i && ./configure $(CONFIGURE_FLAGS) && $(MAKE)
	> $@

build-pith:
	cd pith && perl Makefile.PL && make
	# TODO: install

%-README: %/README
	ln -f $< $@

clean:
	dh_testdir
	rm -f pptop/config.rpath
	rm -f $(STAMPS) $(READMEs_TOINSTALL)
	for i in $(SUBDIRS) ; \
	do \
		( cd $$i ; make distclean || make clean || true ) ; \
	done
	-rm -f */INSTALL
	-rm -f */Makefile.in
	-rm -f */aclocal.m4
	-rm -f */config.h.in
	-rm -f */configure
	-rm -f */depcomp
	-rm -f */install-sh
	-rm -f */mdate-sh
	-rm -f */missing
	-rm -f */stamp-vti
	-rm -f */texinfo.tex
	-rm -f */version.texi
	-rm -f */compile
	-rm -fr */build-aux
	-rm -f cvsfs/cvsfs.info
	-rm -f memfs/bak memfs/core
	-rm -f pptop/po/*.gmo
	-rm -f socketio/socketio
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs

binary: binary-indep binary-arch
binary-indep:
binary-arch: build install
	dh_testdir
	dh_testroot

	dh_installdocs
	dh_install
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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