#!/usr/bin/make -f

#export DH_VERBOSE=1
export MONO_SHARED_DIR=$(CURDIR)

MONO_ABI_COMP_VERSION = 2.6

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

MAKEFILE = $(firstword $(MAKEFILE_LIST))
DEBIAN_DIR = $(dir $(MAKEFILE))
SOURCE_DIR = $(DEBIAN_DIR)/..

DEB_VERSION = $(shell dpkg-parsechangelog -l$(DEBIAN_DIR)/changelog | grep ^Version | cut -d" " -f2)
DEB_SOURCE_NAME = $(shell dpkg-parsechangelog -l$(DEBIAN_DIR)/changelog | grep ^Source | cut -d" " -f2)
VERSION = $(shell echo $(DEB_VERSION) | cut -d"-" -f1 | sed 's/+dfsg.*//')

# Perl code to figure out the current version and the next "potential" version
DPKG_VERSION ?= $(shell head -n 1 debian/changelog  | cut -f 2 -d '(' | cut -f 1 -d ')')
PKG_VERSION ?= $(shell echo $(DPKG_VERSION) | cut -f 1 -d '-' | cut -f 1-3 -d '.')
NEXT_PKG_VERSION ?= $(shell echo $(PKG_VERSION) | perl -e '$$_=<>;/\.(\d+)$$/;$$a=$$1+1;s/\.(\d+)$$/.$$a/;print;')

configure: configure-stamp templates.pot
configure-stamp: 
	dh_testdir
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
	  --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	# Add here commands to compile the package.
	$(MAKE)
	#/usr/bin/docbook-to-man debian/xsp.sgml > xsp.1
	pod2man --section=8 \
		$(CURDIR)/debian/mono-server-admin.conf \
		> $(CURDIR)/debian/mono-server-admin.8
	pod2man --section=8 $(CURDIR)/debian/mono-server-update.conf \
		> $(CURDIR)/debian/mono-server-update.8
	pod2man --section=8 \
		$(CURDIR)/debian/mono-server2-admin.conf \
		> $(CURDIR)/debian/mono-server2-admin.8
	pod2man --section=8 $(CURDIR)/debian/mono-server2-update.conf \
		> $(CURDIR)/debian/mono-server2-update.8
	pod2man --section=8 $(CURDIR)/debian/mono-xsp-admin.conf \
		> $(CURDIR)/debian/mono-xsp-admin.8
	pod2man --section=8 $(CURDIR)/debian/mono-xsp-update.conf \
		> $(CURDIR)/debian/mono-xsp-update.8
	pod2man --section=8 $(CURDIR)/debian/mono-xsp2-admin.conf \
		> $(CURDIR)/debian/mono-xsp2-admin.8
	pod2man --section=8 $(CURDIR)/debian/mono-xsp2-update.conf \
		> $(CURDIR)/debian/mono-xsp2-update.8

	cp $(CURDIR)/debian/mono-server-update.8 \
		$(CURDIR)/debian/update-mono-server.8
	cp $(CURDIR)/debian/mono-server2-update.8 \
		$(CURDIR)/debian/update-mono-server2.8
	cp $(CURDIR)/debian/mono-xsp-update.8 \
		$(CURDIR)/debian/update-mono-xsp.8
	cp $(CURDIR)/debian/mono-xsp2-update.8 \
		$(CURDIR)/debian/update-mono-xsp.8

	touch build-stamp

templates.pot: $(wildcard debian/*.templates)

clean: clean-patched
clean-patched:
	dh_testdir
	dh_testroot
	rm -f config.log
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf server
	# moved below the clean so the configured-stuff cleans properly
	rm -rf $$MONO_SHARED_DIR/.wapi
	rm -f $(CURDIR)/debian/*.8 $(CURDIR)/debian/*.1
	rm -f build-stamp configure-stamp config.status
	find . -name "*.exe" -exec rm {} \;
	find . -name "*.exe.mdb" -exec rm {} \;
	find . -name "*.dll" -exec rm {} \;
	find . -name "*.dll.mdb" -exec rm {} \;
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

#	Fix permissions
	find debian/tmp -name "*.config" -or -name "*.gif" \
		| xargs chmod a-x

#	Move things into Debian-specific places
	cp -r debian/tmp/usr/lib/xsp/test \
		debian/tmp/usr/share/asp.net2-demos
	mv debian/tmp/usr/lib/xsp/test \
		debian/tmp/usr/share/asp.net-demos
	rm -rf debian/tmp/usr/share/asp.net-demos/2.0

	# Make version two variants on the manpages
	cat debian/tmp/usr/share/man/man1/xsp.1 \
		| sed 's/xsp/xsp2/g' \
		> debian/tmp/usr/share/man/man1/xsp2.1
	cp debian/tmp/usr/share/man/man1/xsp.1 debian/tmp/usr/share/man/man1/xsp1.1
	cat debian/tmp/usr/share/man/man1/dbsessmgr.1 \
		| sed 's/dbsessmgr/dbsessmgr2/g' \
		> debian/tmp/usr/share/man/man1/dbsessmgr2.1
	cp debian/tmp/usr/share/man/man1/dbsessmgr.1 debian/tmp/usr/share/man/man1/dbsessmgr1.1
	cat debian/tmp/usr/share/man/man1/asp-state.1 \
		| sed 's/asp-state/asp-state2/g' \
		> debian/tmp/usr/share/man/man1/asp-state2.1
	cp debian/tmp/usr/share/man/man1/asp-state.1 debian/tmp/usr/share/man/man1/asp-state1.1
	cat debian/tmp/usr/share/man/man1/mod-mono-server.1 \
		| sed 's/mono-server/mono-server2/g' \
		> debian/tmp/usr/share/man/man1/mod-mono-server2.1
	cp debian/tmp/usr/share/man/man1/mod-mono-server.1 debian/tmp/usr/share/man/man1/mod-mono-server1.1
	cat debian/tmp/usr/share/man/man1/fastcgi-mono-server.1 \
		| sed 's/mono-server/mono-server2/g' \
		> debian/tmp/usr/share/man/man1/fastcgi-mono-server2.1
	cp debian/tmp/usr/share/man/man1/fastcgi-mono-server.1 debian/tmp/usr/share/man/man1/fastcgi-mono-server1.1

#	Make dh_installxsp man pages

#	Debian control files
	mkdir -p debian/tmp/usr/sbin
	for f in mono-xsp-update mono-xsp-admin \
		 mono-xsp2-update mono-xsp2-admin \
		 mono-server-admin mono-server-update \
	         mono-server2-admin mono-server2-update; \
	do \
		install -m 755 debian/$$f.conf \
			debian/tmp/usr/sbin/$$f; \
	done

binary: binary-arch binary-indep

binary-arch:

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i
	dh_installexamples -i
	dh_installdirs -i
	dh_install -i
	dh_installdebconf -i
	# auto-start we handle ourself using debconf
	dh_installinit -i --noscripts
	# We are using our local version along with our local copies of the
	# file to make sure asp.net gets the proper version.
	dh_installman -i
	dh_link -i
	dh_strip -i
	dh_clistrip -i
	dh_compress -i
	dh_fixperms -i
	dh_clifixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_makeclilibs -i -V
	dh_clideps -i -d
	dh_gencontrol -i -- -Vmono-abi-comp-version=$(MONO_ABI_COMP_VERSION)
	dh_md5sums -i
	dh_builddeb -i

get-orig-source:
	uscan \
		--package $(DEB_SOURCE_NAME) \
		--watchfile $(DEBIAN_DIR)/watch \
		--upstream-version $(VERSION) \
		--download-version $(VERSION) \
		--destdir . \
		--force-download \
		--rename
	bzcat ./$(DEB_SOURCE_NAME)_$(VERSION).orig.tar.bz2 | \
		gzip -9fn -c - > ./$(DEB_SOURCE_NAME)_$(VERSION).orig.tar.gz
	rm ./$(DEB_SOURCE_NAME)_$(VERSION).orig.tar.bz2


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