#!/usr/bin/make -f

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

PACKAGE=aolserver4
VERSION=$(shell grep NS_PATCH_LEVEL $(CURDIR)/include/ns.h | cut -d\" -f2)
ABI=1
ASPREFIX=/usr/lib/$(PACKAGE)
DOCDIR=$(PACKAGE)-doc
DEVDIR=$(PACKAGE)-dev
COREDIR=$(PACKAGE)-core
ASSRC=$(CURDIR)
ASDOC=$(CURDIR)/docs
ASTARGET=$(CURDIR)/debian/$(PACKAGE)-daemon
ASDOCTARGET=$(CURDIR)/debian/$(DOCDIR)
ASDEVTARGET=$(CURDIR)/debian/$(DEVDIR)
ASCORETARGET=$(CURDIR)/debian/$(COREDIR)

TARGETDIR=$(CURDIR)/debian/tmp

MAINTFILES=debian/control debian/debian.substvars

maint: $(MAINTFILES)

debian/control: $(CURDIR)/debian/control.in 
	sed -e "s/@VERSION@/$(VERSION)/g" -e "s/@ABI@/$(ABI)/g" $< >$@

debian/debian.substvars: $(CURDIR)/debian/debian.substvars.in 
	sed -e "s/@VERSION@/$(VERSION)/g" -e "s/@ABI@/$(ABI)/g" $< >$@

configure: maint configure-stamp
configure-stamp: 
	dh_testdir
	
	# TCLSH is currently undefined in tclConfig.sh, but required.
	# Note that Aolserver requires 8.4+ with threads enabled to work.
	( cd $(ASSRC); TCLSH=/usr/bin/tclsh ./configure --prefix=$(ASPREFIX) --with-tcl=/usr/lib --mandir=/usr/share/man )
	
	touch $@

build: configure build-stamp
build-stamp:
	dh_testdir
	
	#
	# syntax checking
	# 
	bash -n $(CURDIR)/debian/aolserver4-daemon.postinst
	bash -n $(CURDIR)/debian/aolserver4-daemon.postrm
	bash -n $(CURDIR)/debian/aolserver4-daemon.preinst
	bash -n $(CURDIR)/debian/aolserver4-daemon.prerm
	
	( cd $(ASSRC); $(MAKE) )
	
	touch $@

clean: 
	dh_testdir
	dh_testroot
	
	debconf-updatepo || true
	[ ! -f $(ASSRC)/include/ns.mak ] || $(MAKE) -C $(ASSRC)  distclean 
	rm -f $(ASSRC)/ns_*.3
	rm -rf $(TARGETDIR)
	rm -f config.log 
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	
	# Install all stuff in a temp dir... 
	( cd $(ASSRC); $(MAKE) AOLSERVER=$(TARGETDIR) install )
	
	# Minor changes
	mv $(TARGETDIR)/bin/nsd $(TARGETDIR)/bin/$(PACKAGE)-nsd
	chmod a-x $(TARGETDIR)/lib/*.so* $(TARGETDIR)/bin/*.so*
	mv $(TARGETDIR)/servers/server1 $(TARGETDIR)/servers/main
	-rmdir $(TARGETDIR)/servers/main/pages
	#
	# This installs all man pages. 
	# There are wrong sections here, which need to be fixed one day.
	# Moreover, man pages are essentially empty and use many symlinks. 
	# Currently a patch to upstream nsinstall-man.sh needs to be
	# applied in order the following code works.
	#
	( cd doc; $(MAKE) AOLSERVER=$(TARGETDIR) UTILS=$(CURDIR)/util install )
	mkdir -p $(TARGETDIR)/man/man8 $(TARGETDIR)/doc/examples 
	zcat $(TARGETDIR)/man/man1/nsd.1aolserver.gz|sed 's/\(\.TH\) \(\w\+\) 1 \(.*\)/\1 \2 8 \3/'|gzip -9 \
		>$(TARGETDIR)/man/man8/$(PACKAGE)-nsd.8.gz
	# 
	# Example applications
	#
	cp $(CURDIR)/docs/admin/sample-config.tcl.txt $(TARGETDIR)/doc/examples/.
	cp $(CURDIR)/docs/admin/sample-db-config.tcl.txt $(TARGETDIR)/doc/examples/.
	cp -r $(ASSRC)/tests/* $(TARGETDIR)/doc/examples/.
	cp $(CURDIR)/debian/app3.tcl $(TARGETDIR)/modules/tcl/.
	-find $(TARGETDIR)/doc/examples -name CVS -exec rm -rf {} \;
	-chmod a-x $(TARGETDIR)/doc/examples/tests.html
	# 
	# Install default home page
	#
	cp $(CURDIR)/debian/index.adp $(TARGETDIR)/doc/examples/.
	cp $(CURDIR)/debian/debian.substvars $(TARGETDIR)/.
	
	touch $@

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot -i
	
	( cd $(ASDOC); cp -r . $(ASDOCTARGET)/usr/share/doc/$(DOCDIR)/html/. )
	cp -r $(TARGETDIR)/html/* $(ASDOCTARGET)/usr/share/doc/$(DOCDIR)/reference/.
	-rm -f $(ASDOCTARGET)/usr/share/doc/$(DOCDIR)/html/ChangeLog 
	-rm -f $(ASDOCTARGET)/usr/share/doc/$(DOCDIR)/html/README.txt
	-find $(ASDOCTARGET)/usr/share/doc/$(DOCDIR)/html -name CVS -exec rm -rf {} \;
	
	dh_installdocs -i
	dh_installchangelogs -i ChangeLog
	dh_installexamples -i $(CURDIR)/debian/examples/aolconf-1.01 $(CURDIR)/examples/*
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i 
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot -a
	dh_installdocs -a
	dh_installexamples -a $(TARGETDIR)/doc/examples/*
	dh_install -a 
	dh_installdebconf -a
	dh_installchangelogs -a ChangeLog
	dh_installlogrotate -a
	dh_link -a 
	dh_lintian -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_shlibdeps -a -l$(ASCORETARGET)$(ASPREFIX)/lib
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a 
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure debian/debian.substvars maint debian/control
