#!/usr/bin/make -f

DEB_HOST_ARCH_OS = $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

LSB_RELEASE_ID ?= $(shell lsb_release --short --id)
ifeq (Ubuntu,$(LSB_RELEASE_ID))
	INSTALLUDEV_OPTS="--priority=80"
endif

%:
	dh $@ --with autoreconf

override_dh_auto_configure:
	cd localapps; ./configure --prefix=/usr --libexecdir=/usr/lib; cd -
ifeq ($(DEB_HOST_ARCH_OS),linux)
	cd nbd-proxy; ./configure --prefix=/usr --libexecdir=/usr/lib; cd -
endif

	$(MAKE) -C client/getltscfg
	$(MAKE) -C localapps
ifeq ($(DEB_HOST_ARCH_OS),linux)
	$(MAKE) -C nbd-proxy 
endif
	$(MAKE) -C po update-po

override_dh_clean:
	[ ! -f client/getltscfg/Makefile ] || $(MAKE) -C client/getltscfg clean

	[ ! -f localapps/Makefile ] || $(MAKE) -C localapps clean

ifeq ($(DEB_HOST_ARCH_OS),linux)
	[ ! -f nbd-proxy/Makefile ] || $(MAKE) -C nbd-proxy clean
endif

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

	# Debconf translation update
	debconf-updatepo

	chmod +x debian/bugs/ltsp-server/script

	dh_clean

override_dh_installinit:
	# Copy default and init scripts from upstream code
	cp $(CURDIR)/client/initscripts/ltsp-core $(CURDIR)/debian/ltsp-client-core.init
	dh_installinit -a --no-start

override_dh_install:
	dh_install
ifeq ($(DEB_HOST_ARCH_OS),linux)
	mkdir -p $(CURDIR)/debian/ltsp-client-core/usr/sbin/
	install -m 755 nbd-proxy/src/nbd-proxy $(CURDIR)/debian/ltsp-client-core/usr/sbin/nbd-proxy
	install -m 755 nbd-proxy/src/nbd-client-proxy $(CURDIR)/debian/ltsp-client-core/usr/sbin/nbd-client-proxy
endif

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

override_dh_installudev:
	cp client/udev/88-ltsp-sound.rules debian/ltsp-client-core.udev
	dh_installudev $(INSTALLUDEV_OPTS)
	rm debian/ltsp-client-core.udev

override_dh_autoreconf:
	dh_autoreconf ./autogen.sh
