#!/usr/bin/make -f

DEB_BUILD_ARCH = $(shell dpkg-architecture -qDEB_BUILD_ARCH)

LIBDIR = usr/lib

p = debian/mozilla-acroread
acrodir = $(LIBDIR)/Adobe/Reader9
rel_acrodir ?= Adobe/Reader9

build: build-stamp
build-stamp:
	dh_testdir

	cp debian/INSTALL_Debian .
	chmod a+x INSTALL_Debian

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	rm -f INSTALL_Debian

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep

	dh_installdirs -pmozilla-acroread \
	usr/lib/mozilla/plugins usr/lib/iceape/plugins \
	usr/lib/iceweasel/plugins

	dh_installdirs -pacroread-data usr/share/applications \
	usr/share/pixmaps usr/share/icons/hicolor

	mkdir -p debian/tmp/usr/lib

	bash ./INSTALL_Debian

#	exit 1

# Files Not needed
	rm debian/tmp/$(acrodir)/Reader/intellinux/bin/xdg*
	rm debian/tmp/$(acrodir)/Browser/install_browser_plugin
	rm debian/tmp/$(acrodir)/Reader/GlobalPrefs/.config
	rm debian/tmp/$(acrodir)/bin/UNINSTALL
	rm debian/tmp/$(acrodir)/Browser/HowTo/ENU/Browser_Plugin_HowTo.txt
	rm debian/tmp/$(acrodir)/Resource/Shell/acroread.1.gz
	rm debian/tmp/$(acrodir)/bin/acroread

# Already in bash
	rm debian/tmp/$(acrodir)/Resource/Shell/acroread_tab

# Sould never be removed otherwise doesn't start (Unable to display the licence file).
#	rm debian/tmp/$(acrodir)/Reader/Legal/en_US/License.txt

	chrpath -d debian/tmp/$(acrodir)/Reader/intellinux/lib/libsccore.so
	chrpath -d debian/tmp/$(acrodir)/Reader/intellinux/lib/libextendscript.so
	execstack -c debian/tmp/$(acrodir)/Reader/intellinux/lib/libcrypto.so.0.9.8

	dh_install --fail-missing -Xnppdf.so

# Install icon in the right place
	@for icon_size in 16 20 22 24 32 36 48 64 96 128 192; do \
	dh_install -pacroread-data debian/acroread-data/$(LIBDIR)/Adobe/Reader9/Resource/Icons/$$icon_size'x'$$icon_size/* \
	usr/share/icons/hicolor/$$icon_size'x'$$icon_size/apps; \
	done
	rm -rf debian/acroread-data/$(LIBDIR)/Adobe/Reader9/Resource/Icons

# Empty directory
	rmdir debian/acroread-data/usr/share/pixmaps

# Install our desktop file in the right place.
	rm  debian/acroread-data/$(acrodir)/Resource/Support/AdobeReader.desktop
	cp debian/AdobeReader.desktop debian/acroread-data/usr/share/applications

	dh_link -pacroread-plugin-speech usr/lib/libORBit-2.so.0 $(LIBDIR)/Adobe/Reader9/Reader/intellinux/lib/libORBit-2.so
	dh_link -pacroread-plugin-speech usr/lib/libbonobo-activation.so.4 $(LIBDIR)/Adobe/Reader9/Reader/intellinux/lib/libbonobo-activation.so
	dh_link -pacroread-plugin-speech usr/lib/libbonobo-2.so.0 $(LIBDIR)/Adobe/Reader9/Reader/intellinux/lib/libbonobo-2.so
	dh_link -pacroread-plugin-speech usr/lib/libgnomespeech.so.7 $(LIBDIR)/Adobe/Reader9/Reader/intellinux/lib/libgnomespeech.so

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installchangelogs -i
	dh_installxfonts -i -pacroread-data
	dh_lintian -i
	dh_link -i
	dh_perl -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 -s
	dh_testroot -s
# Prune empty directories
	find debian -type d -print0 | xargs -0 rmdir -p --ignore-fail-on-non-empty
	dh_installdocs -s
	dh_installexamples -s
	dh_installchangelogs -s
	dh_installman -s
	dh_installmenu -s
	dh_installmime -s
	dh_lintian -s
	dh_link -s
#	dh_strip -s
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -s -n
	dh_installdeb -s
	LD_LIBRARY_PATH=$(CURDIR)/debian/acroread/$(acrodir)/Reader/intellinux/lib:/lib:/usr/lib:$(LD_LIBRARY_PATH) dh_shlibdeps -s -ldebian/acroread/$(acrodir)/Reader/intellinux/lib
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

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