#!/usr/bin/make -f

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

PACKAGE=brltty
VERSION=$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
DEB_HOST_ARCH=$(shell dpkg-architecture -qDEB_HOST_ARCH)
# java is not supported by m68k (200712)
ifeq ($(DEB_HOST_ARCH),m68k)
export DH_OPTIONS=-Nlibbrlapi-jni -Nlibbrlapi-java
DISABLE_JAVA_SUPPORT=1
endif

CONFIGURE_DISABLE_NONFREE = --without-viavoice --without-theta --without-swift

UDEB_CFLAGS=-Os -fomit-frame-pointer
UDEB_DISABLE=speech-support pcm-support midi-support fm-support \
	contracted-braille api gpm iconv icu x
UDEB_CONFIGURE_OPTIONS=--with-braille-driver=-vr,all \
	--with-braille-device=usb: --with-screen-driver=-as,-a2,-sc,all \
	--without-init-path --without-curses $(CONFIGURE_DISABLE_NONFREE) \
	$(patsubst %,--disable-%,$(UDEB_DISABLE))
UDEB_FILENAME=$(PACKAGE)-udeb_$(VERSION)_$(DEB_HOST_ARCH).udeb
PRIORITY=$(shell grep '^Package: brltty-udeb' debian/control -A 10 | grep ^Priority: | cut -d ' ' -f 2)

export JAVA_HOME=/usr/lib/jvm/default-java

binary: binary-indep binary-arch

clean:
	dh_testdir
	dh_testroot
	rm -f build-*stamp
	rm -rf build-brltty*
	rm -f Bindings/Python/bindings.o
	-test -r /usr/share/misc/config.sub && \
	  cp -f /usr/share/misc/config.sub acdir/config.sub
	-test -r /usr/share/misc/config.guess && \
	  cp -f /usr/share/misc/config.guess acdir/config.guess

	dh_clean

configure: build-brltty/config.status
build-brltty/config.status:
	mkdir build-brltty
	cd build-brltty && ../configure --bindir=/sbin --mandir=/usr/share/man \
		--with-curses=ncursesw --disable-stripping \
		--without-espeak $(CONFIGURE_DISABLE_NONFREE)

configure-udeb: build-brltty-udeb/config.status
build-brltty-udeb/config.status:
	mkdir build-brltty-udeb
	cd build-brltty-udeb && \
	CFLAGS="$(UDEB_CFLAGS)" ../configure --bindir=/sbin \
		$(UDEB_CONFIGURE_OPTIONS)

build: build-brltty build-udeb

build-brltty: build-stamp
build-stamp: configure
	dh_testdir

	$(MAKE) -C build-brltty
	$(MAKE) -C build-brltty/Documents

	touch $@

build-udeb: build-udeb-stamp
build-udeb-stamp: configure-udeb
	dh_testdir

	$(MAKE) -C build-brltty-udeb

	touch $@

# Build architecture-independent files here.
binary-indep: ;
# We have nothing to do here.

# Build architecture-dependent files here.
binary-arch: brltty brltty-udeb

brltty: build-brltty
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) -C build-brltty/Documents
	$(MAKE) -C build-brltty install INSTALL_ROOT=`pwd`/debian/tmp
	dh_install	
	chmod +x debian/$@/usr/share/initramfs-tools/hooks/brltty
	dh_installdocs
	dh_installexamples -p$@
	for file in Drivers/Braille/*/README*; \
		do cp $$file `pwd`/debian/$(PACKAGE)/usr/share/doc/brltty/`basename $$(dirname $$file)`.`basename $$file`; \
		done
	# -r because we don't want to lose the console on an upgrade
	dh_installinit -p$@ -r -- start 25 S .
	dh_installchangelogs Documents/ChangeLog
	dh_installman
	dh_lintian
	dh_pysupport -ppython-brlapi
	mv debian/brltty/lib/brltty/libbrlttysfl.so \
		debian/brltty-flite/lib/brltty
	mv debian/brltty/lib/brltty/libbrlttyssd.so \
		debian/brltty-speechd/lib/brltty
	mv debian/brltty/lib/brltty/libbrlttybxw.so \
		debian/brltty-x11/lib/brltty
	mv debian/brltty/etc/brltty/brltty-xw-all.hlp \
		debian/brltty-x11/etc/brltty
	mv debian/brltty/usr/share/doc/brltty/XWindow.README \
		debian/brltty-x11/usr/share/doc/brltty-x11/
	mv debian/brltty/lib/brltty/libbrlttyxas.so \
		debian/brltty-x11/lib/brltty
	mv debian/brltty/lib/brltty/libbrlttyxa2.so \
		debian/brltty-x11/lib/brltty
ifneq ($(DISABLE_JAVA_SUPPORT),1)
	mv debian/tmp/usr/share/java/brlapi.jar \
		debian/libbrlapi-java/usr/share/java
	chmod 0644 debian/libbrlapi-java/usr/share/java/brlapi.jar
endif
	cp build-brltty/Documents/brltty.conf debian/brltty/etc/brltty.conf
	dh_lisp -pcl-brlapi
	dh_link -p$@ -plibbrlapi0.5 -plibbrlapi-dev
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -n -p$@
	dh_makeshlibs -a -Nbrltty-udeb
	dh_installdeb -a -i -Nbrltty-udeb
	dh_shlibdeps -a -Nbrltty-udeb
	dh_gencontrol -a -i -Nbrltty-udeb
	dh_md5sums -a -i -Nbrltty-udeb
	dh_builddeb -a -i -Nbrltty-udeb

brltty-udeb: build-udeb
	dh_testdir
	dh_testroot
	dh_installdirs -pbrltty-udeb

	$(MAKE) -C build-brltty-udeb install INSTALL_ROOT=`pwd`/debian/brltty-udeb

	# temporary fix
	rm -Rf debian/brltty-udeb/share

	cp debian/brltty-udeb.prebaseconfig debian/brltty-udeb/usr/lib/finish-install.d/07brltty
	chmod 0755 debian/brltty-udeb/usr/lib/finish-install.d/07brltty
	rm -f debian/brltty-udeb/sbin/brltty-*
	rm -Rf debian/brltty-udeb/usr/share
	# Strip comments and whitespaces from translation tables
	cd debian/brltty-udeb/etc/brltty && \
		for file in *.tt? *.at? *.ktb; do \
			sed -e 's/ *#.*//' -e 's/[	 ]\+/ /g' $$file | sed -n '/./p' > $$file.tmp && \
			mv $$file.tmp $$file; \
		done
	# It is sad, but help files are not really essential, but they are
	# fairly large.
	rm debian/brltty-udeb/etc/brltty/*.hlp
	#rm -Rf debian/brltty-udeb/lib
	cp debian/brltty-udeb.init debian/$@/lib/debian-installer-startup.d/S37brltty
	chmod +x debian/$@/lib/debian-installer-startup.d/S37brltty
	cp debian/brltty-udeb.udev.rules debian/$@/lib/udev/rules.d/brltty.rules
	cp debian/brltty-udeb.udev.sh debian/$@/lib/udev/brltty.sh
	chmod +x debian/$@/lib/udev/brltty.sh
	cp debian/brltty-udeb.sh debian/$@/lib/brltty/brltty.sh
	chmod +x debian/$@/lib/brltty/brltty.sh
	dh_strip -p$@
	dh_fixperms -p$@
	dh_installdeb -p$@
	dh_shlibdeps -p$@
	dh_gencontrol -p$@
	# Check that we didn't accidentally link against something outside of
	# d-i world
	grep Depends: debian/brltty-udeb/DEBIAN/control | perl -ne 'map {unless (/-udeb/) {print $$_; exit 1}} split /,/'
	dh_builddeb -p$@

.PHONY: brltty brltty-udeb build build-brltty build-udeb clean binary-indep binary-arch binary
