#!/usr/bin/make -f
# -*- makefile -*-

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

# Must be exported
export DH_OPTIONS

# 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)


CFLAGS = -Wall -g

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

Makefile: configure
	dh_testdir
	# Add here commands to configure the package.
	USEHOBBITPING=y \
	ENABLESSL=y \
	ENABLELDAP=y \
	ENABLELDAPSSL=y \
	BBUSER=hobbit \
	BBTOPDIR=/usr/lib/hobbit \
	BBVAR=/var/lib/hobbit \
	BBHOSTURL=/hobbit \
	CGIDIR=/usr/lib/hobbit/cgi-bin \
	BBCGIURL=/hobbit-cgi \
	SECURECGIDIR=/usr/lib/hobbit/cgi-secure \
	SECUREBBCGIURL=/hobbit-seccgi \
	HTTPDGID=www-data \
	BBLOGDIR=/var/log/hobbit \
	BBHOSTNAME=localhost \
	BBHOSTIP=127.0.0.1 \
	MANROOT=/usr/share/man \
	INSTALLBINDIR=/usr/lib/hobbit/server/bin \
	INSTALLETCDIR=/etc/hobbit \
	INSTALLWEBDIR=/etc/hobbit/web \
	INSTALLEXTDIR=/usr/lib/hobbit/server/ext \
	INSTALLTMPDIR=/var/lib/hobbit/tmp \
	INSTALLWWWDIR=/var/lib/hobbit/www \
	./configure

build: build-stamp

build-stamp:  Makefile
	dh_testdir

	# Add here commands to compile the package.
	PKGBUILD=1 $(MAKE)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	-$(MAKE) distclean
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	PKGBUILD=1 INSTALLROOT=$(CURDIR)/debian/hobbit/ $(MAKE) install
	cd $(CURDIR)/debian/hobbit/usr/bin && ln -s ../lib/hobbit/server/bin/{bb,bbcmd} .
	cd $(CURDIR)/debian/hobbit/usr/lib/hobbit/client && mv etc/hobbitclient.cfg $(CURDIR)/debian/hobbit/etc/hobbit && rm -rf $(CURDIR)/debian/hobbit/usr/lib/hobbit/client/etc && ln -s ../../../../etc/hobbit etc
	cd $(CURDIR)/debian/hobbit/usr/lib/hobbit/client && rmdir logs && ln -s ../../../../var/log/hobbit logs
	cd $(CURDIR)/debian/hobbit/usr/lib/hobbit/client && rmdir tmp  && ln -s ../../../../var/lib/hobbit/tmp tmp


install-client: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	PKGBUILD=1 INSTALLROOT=$(CURDIR)/debian/hobbit-client/ $(MAKE) install-client
	cp debian/hobbit-client.default $(CURDIR)/debian/hobbit-client/etc/default/hobbit-client
	mkdir -p $(CURDIR)/debian/hobbit-client/var/log/hobbit
	mkdir -p $(CURDIR)/debian/hobbit-client/etc/hobbit
	cd $(CURDIR)/debian/hobbit-client/usr/lib/hobbit/client && mv etc/* $(CURDIR)/debian/hobbit-client/etc/hobbit && rmdir etc && ln -s ../../../../etc/hobbit etc
	cd $(CURDIR)/debian/hobbit-client/usr/lib/hobbit/client && rmdir logs && ln -s ../../../../var/log/hobbit logs
	cd $(CURDIR)/debian/hobbit-client/usr/lib/hobbit/client && rmdir tmp && ln -s ../../../../tmp tmp
	mv $(CURDIR)/debian/hobbit-client/usr/lib/hobbit/client/etc/hobbitclient.cfg /tmp/hobbitclient.cfg.$$
	cat /tmp/hobbitclient.cfg.$$ | sed -e 's!^BBDISP=.*!include /var/run/hobbitclient-runtime.cfg!' | grep -v "^BBDISPLAYS=" >$(CURDIR)/debian/hobbit-client/usr/lib/hobbit/client/etc/hobbitclient.cfg


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

hobbit: DH_OPTIONS=--package=hobbit
hobbit: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs Changes
	dh_installdocs
	dh_installexamples
#	dh_install
#	dh_installmenu
	dh_installdebconf	
	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
#	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

hobbit-client: DH_OPTIONS=--package=hobbit-client
hobbit-client: build install-client
	dh_testdir
	dh_testroot
	dh_installchangelogs Changes
	dh_installdocs
	dh_installexamples
#	dh_install
#	dh_installmenu
	dh_installdebconf	
	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
#	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: hobbit hobbit-client

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