#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


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

CONFIG_GUESS=`support/config.guess`

configure: configure-stamp
configure-stamp:
	
	dh_testdir
	-test -r /usr/share/misc/config.sub && \
	  cp -f /usr/share/misc/config.sub support/config.sub
	-test -r /usr/share/misc/config.guess && \
	  cp -f /usr/share/misc/config.guess support/config.guess
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr \
	--mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --bindir=/usr/bin \
	--datadir=/usr/share --sbindir=/usr/sbin --localstatedir=/var --sysconfdir=/etc/ircd \
	--with-logdir=/var/log/ircd --with-zlib

	touch configure-stamp
	
build: configure-stamp build-stamp
build-stamp: 

	cp debian/maint/config.h $(CONFIG_GUESS)

	cat $(CONFIG_GUESS)/Makefile | sed 's/\/var\/run/&\/ircd/g' >> $(CONFIG_GUESS)/Makefile.new
	mv $(CONFIG_GUESS)/Makefile.new $(CONFIG_GUESS)/Makefile

	cd $(CONFIG_GUESS) && $(MAKE) all
	touch build-stamp

clean:
	
	-rm -rf $(CONFIG_GUESS)
	-rm support/config.sub
	-rm support/config.guess

	-rm -rf configure-stamp
	-rm -rf build-stamp
	-rm debian/maint/ircd-irc2.info
	
	dh_clean
	
install:
	
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	
	# ircd-package
	cp $(CONFIG_GUESS)/chkconf debian/ircd-irc2/usr/sbin
	cp $(CONFIG_GUESS)/iauth debian/ircd-irc2/usr/sbin
	cp $(CONFIG_GUESS)/ircd debian/ircd-irc2/usr/sbin
	cp $(CONFIG_GUESS)/ircd-mkpasswd debian/ircd-irc2/usr/sbin 
	cp $(CONFIG_GUESS)/ircdwatch debian/ircd-irc2/usr/sbin
	
	cp debian/maint/iauth.conf debian/ircd-irc2/etc/ircd
	
# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install

	dh_testdir
	dh_testroot

	# copy overrides file
	install -D `pwd`/debian/lintian.overrides `pwd`/debian/ircd-irc2/usr/share/lintian/overrides/ircd-irc2
	chmod -x `pwd`/debian/ircd-irc2/usr/share/lintian/overrides/ircd-irc2	

	# install config...
	cp debian/maint/ircd.conf debian/ircd-irc2/etc/ircd/ircd.conf
	
	# ... and motd-file.
	cp debian/maint/ircd.motd debian/ircd-irc2/etc/ircd/ircd.motd
	
	dh_installdocs -A

	cp doc/INSTALL.info debian/maint/ircd-irc2.info
	dh_installinfo -A
	
	dh_installman -pircd-irc2 doc/iauth.8 doc/iauth.conf.5 \
	                          doc/ircd.8 \
				  debian/maint/chkconf.8 \
				  debian/maint/ircd-mkpasswd.8 \
    		                  debian/maint/ircdwatch.8
	
	dh_installchangelogs -a 

	dh_installinit --update-rcd-params="start 20 2 3 4 5 ."

	dh_strip
	dh_compress
	dh_fixperms
	
	# fix permissions of var/log/ircd
	chown root:irc debian/ircd-irc2/var/log/ircd
	chmod -R ug+rw debian/ircd-irc2/var/log/ircd

	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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