#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# This file is public domain software, originally written by Joey Hess. 

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

DEBVER=$(shell dpkg-parsechangelog |sed -n -e '/^Version:/s/^Version: //p')
DEB_SRC_BUILD_PROG:=dpkg-buildpackage -rfakeroot -S -sa

build: build-stamp
build-stamp:
	dh_testdir
	cd localapps; ./configure --prefix=/usr --libexecdir=/usr/lib; cd -
	cd nbd-proxy; ./configure --prefix=/usr --libexecdir=/usr/lib; cd -

	$(MAKE) -C client/getltscfg
	$(MAKE) -C localapps
	$(MAKE) -C nbd-proxy
	$(MAKE) -C po update-po
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

#	$(MAKE) -C client/getltscfg clean
#	$(MAKE) -C localapps clean

	# Using po specific Makefile to clean message catalogs
	$(MAKE) -C po clean

	# Debconf translation update
	debconf-updatepo

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Make the bug reporting script executable
	chmod +x debian/bugs/ltsp-server/script

	# Make the Live LTSP script executable
	chmod +x debian/scripts/ltsp-live

	# Unpack the icon
	base64 -d debian/scripts/ltsp-live.png.b64 > debian/scripts/ltsp-live.png

	# Build and install the locales
	cd debian/scripts/po; DESTDIR=$(CURDIR)/debian/ltsp-livecd/ make install

	# Using po specific Makefile to install message catalogs
	$(MAKE) -C po install DESTDIR=$(CURDIR)/debian/ltsp-server/

	# Copy init scripts from upstream code
	cp $(CURDIR)/client/initscripts/ltsp-core $(CURDIR)/debian/ltsp-client-core.init
	cp $(CURDIR)/client/initscripts/ltsp-core.upstart $(CURDIR)/debian/ltsp-client-core.upstart

	# Manually install the functions in the right packages
	mkdir -p $(CURDIR)/debian/ltsp-client-core/usr/share/ltsp
	mkdir -p $(CURDIR)/debian/ltsp-server/usr/share/ltsp
	cp common/ltsp-common-functions $(CURDIR)/debian/ltsp-client-core/usr/share/ltsp/ltsp-client-common-functions
	cp common/ltsp-common-functions $(CURDIR)/debian/ltsp-server/usr/share/ltsp/ltsp-server-common-functions

	# install 40-ltsp in both ltsp-client-core and ltsp-server
	mkdir -p $(CURDIR)/debian/ltsp-client-core/etc/X11/Xsession.d/
	install -m 644 $(CURDIR)/debian/40-ltsp $(CURDIR)/debian/ltsp-client-core/etc/X11/Xsession.d/40-ltsp-client
	mkdir -p $(CURDIR)/debian/ltsp-server/etc/X11/Xsession.d/
	install -m 644 $(CURDIR)/debian/40-ltsp $(CURDIR)/debian/ltsp-server/etc/X11/Xsession.d/40-ltsp-server

	# FIXME: Temporary until end of transition
	cp common/ltsp-common-functions $(CURDIR)/debian/ltsp-client-core/usr/share/ltsp/ltsp-common-functions

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_install -i -Xarch-ids
	dh_installdebconf -i
	dh_installman -pltsp-server -pltsp-client-core
	dh_link -i
	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 -a
	dh_testroot -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_install -a
	dh_installdebconf -a
	dh_installinit -a --no-start
	dh_link -a
	dh_strip -a

	# FIXME: We should make something clever fixing all the symlinks instead
	rm $(CURDIR)/debian/ltsp-client-core/usr/share/ltsp/ltsp-client-vendor-functions
	cp server/functions/Debian/ltsp-server-vendor-functions $(CURDIR)/debian/ltsp-client-core/usr/share/ltsp/ltsp-client-vendor-functions

	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

bzr-release:
	bzr export ltsp-$(DEB_UPSTREAM_VERSION) 
	rm -rf ltsp-$(DEB_UPSTREAM_VERSION)/debian
	tar czf ../ltsp_$(DEB_UPSTREAM_VERSION).orig.tar.gz ltsp-$(DEB_UPSTREAM_VERSION)
	rm -rf ltsp-$(DEB_UPSTREAM_VERSION)

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