#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

clean:
	dh_testdir
	dh_testroot
	# Add here commands to clean up after the build process.
	rm -f man/therion-viewer.1
	$(MAKE) -C samples clean
	dh_auto_clean
	dh_clean

build: build-arch build-indep

build-indep:
	# just build the -doc package 
	dh_testdir
        

ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	# We need therion itself to build the samples
	$(MAKE) therion 
	# create HTML documentation for samples
	$(MAKE) samples
endif
	$(MAKE) thbook
	touch build-indep-stamp

build-arch:
	dh_testdir
	# Compile the binaries
	$(MAKE) therion xtherion/xtherion loch/loch
	ln -sf loch.1 man/therion-viewer.1
	# create therion.ini file:
	#$(MAKE) init
	touch build-arch-stamp

binary: binary-arch binary-indep

# Package architecture-independent packages here.
binary-indep: DH_OPTIONS=-i
binary-indep: build-indep-stamp
	# just build the -doc package 
	dh_testdir
	dh_testroot

ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
	#if cross-building these won't have been generated, so skip
	dh_installdocs -ptherion-doc samples.doc
endif
	dh_install -Ntherion-viewer -Ntherion
	dh_installchangelogs CHANGES
	#we should generate html of thbook here too if possible
	dh_compress -ptherion-doc -X.pdf -X.th -X.th2 -Xthconfig
	dh_fixperms -Ntherion-viewer -Ntherion
	dh_installdeb -Ntherion-viewer -Ntherion
	dh_gencontrol -Ntherion-viewer -Ntherion
	dh_md5sums -Ntherion-viewer -Ntherion
	dh_builddeb -Ntherion-viewer -Ntherion

# Package architecture-dependent files here.
binary-arch: DH_OPTIONS=-a
binary-arch: build-arch-stamp 
	dh_testdir
	dh_testroot
	dh_installdirs

	# Add here commands to install the package into debian/therion.
#this stuff no longer needed from 0.2.14
#but it would be nice to put it back
#	install -d -o root -g root -m 755 debian/therion/usr/share/texmf/metapost/therion
#	install -d -o root -g root -m 755 debian/therion/usr/share/texmf/tex/therion
#	install -p -o root -g root -m 644 mpost/*.mp debian/therion/usr/share/texmf/metapost/therion/
#	install -p -o root -g root -m 644 tex/*.tex debian/therion/usr/share/texmf/tex/therion/
	dh_install -Ntherion-doc
	cd loch/locale && for lang in * ; do \
	  install -D "$$lang/loch.mo" "debian/tmp/therion-viewer/usr/share/locale/$$lang/LC_MESSAGES/loch.mo" ; \
	done
	dh_installdocs README
	dh_installdocs -ptherion-viewer loch/help/en/loch.htm
	# we need to clear out large generated files from samples before putting just source files in main package
	$(MAKE) -C samples clean
	dh_installexamples -ptherion  --exclude=.tcl --exclude=Makefile --exclude=thTMPDIR samples thconfig therion.ini
	dh_installmenu
	dh_installmime
	dh_icons
	dh_installman -ptherion man/therion.1 man/xtherion.1
	dh_installman -ptherion-viewer man/loch.1 man/therion-viewer.1
	dh_installchangelogs CHANGES
	dh_strip
	dh_compress -X.pdf -X.th -X.th2 -Xthconfig
	dh_fixperms -Ntherion-doc
	dh_installdeb -Ntherion-doc
	dh_shlibdeps -Ntherion-doc
	dh_gencontrol -Ntherion-doc
	dh_md5sums -Ntherion-doc
	dh_builddeb -Ntherion-doc

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