#!/usr/bin/make -f

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

export CFLAGS=-Wall -g

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

SHELL=/bin/bash

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

configure: configure.stamp
configure.stamp:
	dh_testdir
	dpatch apply-all
	./configure --prefix=/usr --mandir=/usr/share/man \
	--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
	--disable-mount-locking
	touch $@

clean:
	dh_testdir
	rm -f *.stamp
	$(MAKE) clean
	rm -f config.log config.status Makefile.conf include/config.h
	dpatch deapply-all
	dh_clean debian/init

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

	dh_installdirs

	$(MAKE) install INSTALLROOT=$(CURDIR)/debian/autofs
	
	dh_installdirs etc/apm/event.d
	cp -a debian/event.d debian/autofs/etc/apm/event.d/autofs
	chmod +x debian/autofs/etc/apm/event.d/autofs
	
	# Do the final stuff
	dh_movefiles --sourcedir=debian/autofs
	dh_installdirs --package=autofs-ldap etc/ldap/schema
	cp -a samples/autofs.schema debian/autofs-ldap/etc/ldap/schema
	cp -a samples/rc.autofs debian/init
	dh_installdocs -X README.options -X README.patches
	dh_installman
	dh_installchangelogs CHANGELOG
	dh_installexamples
	dh_installmodules
	dh_installinit --noscripts -- defaults 20

	# Move all files that will be managed by ucf
	dh_installdirs usr/share/autofs/conffiles
	mv debian/autofs/etc/auto.{master,misc,net,smb} debian/autofs/usr/share/autofs/conffiles/
	mv debian/autofs/etc/default/autofs debian/autofs/usr/share/autofs/conffiles/autofs.default
	cp -a debian/auto.{master,misc,net,smb}.md5sum debian/autofs/usr/share/autofs/conffiles/
	cp -a debian/autofs.default.md5sum debian/autofs/usr/share/autofs/conffiles/

	dh_link
	dh_strip $(STRIPARGS)
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb
