#!/usr/bin/make -f

package=userv

t=debian/tmp

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTOPTS= LDFLAGS=-s INSTALL_PROGRAM='install -c -s'
else
	INSTOPTS= INSTALL_PROGRAM='install -c'
endif

build:
	$(checkdir)
	./configure --prefix=/usr
	$(MAKE) all docs
	touch build

clean:
	$(checkdir)
	-rm -f build
	-$(MAKE) -i distclean || $(MAKE) -f Makefile.in distclean
	-rm -rf *~ $t debian/*~ debian/files* debian/substvars*
	-rm -rf spec.html

binary-indep:	checkroot build
	$(checkdir)
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch:	checkroot build
	$(checkdir)
	-rm -rf $t
	install -d $t/{DEBIAN,etc/init.d} $t/usr/{sbin,bin}
	install -d $t/etc/userv/{override,default}.d
	install -d $t/usr/share/doc/$(package)/examples/
	install -d $t/var/run/$(package)/
	install -d $t/usr/share/lintian/overrides/
	cp debian/{postinst,prerm,postrm,conffiles} $t/DEBIAN/.
	cp debian/initd $t/etc/init.d/userv
	chmod 755 $t/DEBIAN/{postinst,prerm,postrm}
	chmod 755 $t/etc/init.d/userv
	$(MAKE) $(INSTOPTS) \
		prefix=$t/usr etcdir=$t/etc \
		docdir=$t/usr/share/doc/userv \
		mandir=$t/usr/share/man \
		install install-doc
	cp debian/copyright $t/usr/share/doc/$(package)/.
	cp debian/changelog $t/usr/share/doc/$(package)/changelog
	cp README $t/usr/share/doc/$(package)/README.upstream
	cp system.default system.override $t/usr/share/doc/$(package)/examples
	ln -s changelog.gz $t/usr/share/doc/$(package)/changelog.Debian.gz
	gzip -9v $t/usr/share/doc/$(package)/changelog \
		$t/usr/share/man/man1/*.1 $t/usr/share/doc/userv/*.ps
	cp debian/lintian $t/usr/share/lintian/overrides/userv
	dpkg-shlibdeps daemon client
	dpkg-gencontrol -isp
	chown -R root.root $t
	chmod -R og=rX $t
	chmod 700 $t/var/run/userv
	dpkg --build $t ..

define checkdir
	test -f overlord.c -a -f lexer.l.m4 -a -f debian/rules
endef

# Below here is fairly generic really

binary:		binary-indep binary-arch

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

checkroot:
	$(checkdir)
	test root = "`whoami`"

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