#!/usr/bin/make -f
# -*- makefile -*-

# get Octave paths
include /usr/share/octave/debian/defs.make
# get Matlab paths
include /usr/share/matlab/debian/defs.make

srcpkg = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Source:' | cut -d ' ' -f 2,2)
debver = $(shell LC_ALL=C dpkg-parsechangelog | grep '^Version:' | cut -d ' ' -f 2,2 )
uver = $(shell echo $(debver) | cut -d '-' -f 1,1 )

# Directories which are created/populated/cleanedup
BDIRS=PsychSourceGL/Projects/Linux/build/ Psychtoolbox/PsychBasic/Octave3LinuxFiles Psychtoolbox/PsychBasic/PsychPlugins
OCT_SHAREDDIR=debian/psychtoolbox-3-common/usr/share/psychtoolbox-3
OCT_LIBDIR=debian/octave-psychtoolbox-3/usr/lib/psychtoolbox-3

export LDFLAGS=-Wl,--as-needed

# Critical for building on machines with multiarch support
# (e.g. ubuntu 11.04)
GLIB_INCLUDES=$(shell pkg-config --cflags glib-2.0)

# Upstream advises to build without optimization at the moment
export CXXFLAGS=-Wall -g -O0
export CFLAGS=-Wall -g -O0 -std=gnu99 $(GLIB_INCLUDES)

%:
	dh $@

override_dh_auto_build: build-stamp
build-stamp:
	mkdir -p $(BDIRS)
	: # Build patched version of portaudio
	: # dependent on patch deb_portaudio_Makefile
	$(MAKE) -C PsychSourceGL/Cohorts/PortAudio

	: # Build all extensions, but:
	: #  3  dependent on portaudio above
	: #  4  libeyelink -- non-distributable binary blob with open-sourced API
	cd PsychSourceGL/Source/; \
		for mode in 0 1 2 3   5 6 7 8 9; do \
		  octave -p $(CURDIR)/Psychtoolbox/PsychOneliners -q --eval "linuxmakeitoctave3_ubuntugutsy($$mode)" || exit 1; \
	done

	: # Manually build OpenGL text renderer extension
	cd PsychSourceGL/Cohorts/FTGLTextRenderer; \
	$(CXX) $(CXXFLAGS) -I. -I/usr/include/ -I/usr/include/freetype2/ \
		-L/usr/lib -pie -shared -fPIC \
		-o libptbdrawtext_ftgl.so.1 \
		libptbdrawtext_ftgl.cpp qstringqcharemulation.cpp OGLFT.cpp \
		-lGL -lGLU -lfontconfig -lfreetype

	: # Manually build tcp_udp_ip toolbox's extension
	cd ./Psychtoolbox/PsychHardware/iViewXToolbox/tcp_udp_ip; \
		octave -q --eval "mex pnet.c"; [ -f pnet.mex ]

	install --mode=0644 PsychSourceGL/Cohorts/FTGLTextRenderer/libptbdrawtext_ftgl.so.1 \
	   Psychtoolbox/PsychBasic/PsychPlugins
	touch $@

override_dh_auto_install:
	dh_auto_install
	: # to guarantee -common existance for arch-dep builds only:
	mkdir -p $(OCT_SHAREDDIR)
	rsync -a --exclude=*.o ./Psychtoolbox/ $(OCT_SHAREDDIR)

	: # Move .mex to reside alongside with .m to gain easy help in octave
	mv $(OCT_SHAREDDIR)/PsychBasic/Octave3LinuxFiles/* $(OCT_SHAREDDIR)/PsychBasic
	: # But PsychHID must go into PsychHardware alongside with .m
	mv $(OCT_SHAREDDIR)/PsychBasic/PsychHID.mex $(OCT_SHAREDDIR)/PsychHardware
	rmdir $(OCT_SHAREDDIR)/PsychBasic/Octave3LinuxFiles

	: # Move .mex binaries under /usr/lib and create symlinks file to point back
	mkdir -p $(OCT_LIBDIR)
	cp debian/octave-psychtoolbox-3.links.in  debian/octave-psychtoolbox-3.links
	find $(OCT_SHAREDDIR)/ -iname \*.mex | while read f; do \
		fn=$$(basename $$f); ds=$$(dirname $$f); \
		dl=$$(echo $$ds | sed -e 's,psychtoolbox-3-common,octave-psychtoolbox-3,g' -e 's,/share/,/lib/,g'); \
		echo "I: Moving $$f under $$dl"; \
		mkdir -p $$dl; \
		mv $$f $$dl; \
		echo "/usr/lib$${f#*usr/share}	/usr$${f#*usr}" >> debian/octave-psychtoolbox-3.links; \
	done

	cp debian/psychtoolbox-3-lib.links.in  debian/psychtoolbox-3-lib.links
	find $(OCT_SHAREDDIR)/ -iname \*.so* -o -iname \*.a | while read f; do \
		fn=$$(basename $$f); ds=$$(dirname $$f); \
		dl=$$(echo $$ds | sed -e 's,psychtoolbox-3-common,psychtoolbox-3-lib,g' -e 's,/share/,/lib/,g'); \
		echo "I: Moving $$f under $$dl"; \
		mkdir -p $$dl; \
		mv $$f $$dl; \
		echo "/usr/lib$${f#*usr/share}	/usr$${f#*usr}" >> debian/psychtoolbox-3-lib.links; \
	done

	: # Filter out duplicate licenses etc
	find debian/ -iname 3Dlabs-License.txt -o \
				 -iname COPYING -o \
				 -iname *license.txt | xargs -r rm

	: # Filter out some scripts which aren't pertitent to Debian systems
	find debian/ -iname simplepsychtoolboxsetup.sh -o -iname setupdate.sh | xargs -r rm


override_dh_makeshlibs:
	dh_makeshlibs
	: # Provide Octave:Depends
	[ -e /usr/bin/octave-depends ] \
	&& octave-depends \
	|| echo 'octave:Depends=octave (>= 3.4.3-1~)' >> debian/octave-psychtoolbox-3.substvars

#override_dh_fixperms:
#	dh_fixperms

override_dh_strip:
	dh_strip --dbg-package=psychtoolbox-3-dbg

override_dh_shlibdeps:
	@: # octave will take care of proper deps for extensions -- no need to scan
	@: # them and warn all the time
	dh_shlibdeps -l$(shell octave-config --print OCTLIBDIR)

	: # Manually remove executable bit for everything not to be executed (e.g. .mex .m .so.*)
	@: # needs to be done post dh_strip,dh_shlibdeps so that .mex are found by dh_strip, dh_shlibdeps
#	find debian/ -regextype posix-egrep \
#	-regex '.*\.(c|jpg|vert|frag|xml|html|hiro|kanji|mat|m|txt|mex|so(|\..*))' \
#	-perm /+x -print0 \
	@: # strip from all files since we do not have any real executable there
	find debian/ -ipath '*psych*/usr/*' -perm /+x -type f ! -iname ATIRadeonperf_Linux -print0 \
	| xargs -0 --no-run-if-empty chmod a-x

override_dh_installchangelogs:
	dh_installchangelogs debian/changelog.upstream

override_dh_clean:
	dh_clean
	rm -f build-stamp
	: # Manually remove  additional trash
	find Psych* -regextype posix-egrep -regex '.*\.(o|mex|so(|\..*))' -delete
	-rmdir $(BDIRS)
	: # Prune some Debian generated files
	-rm debian/octave-psychtoolbox-3.links debian/psychtoolbox-3-lib.links
	$(MAKE) -C PsychSourceGL/Cohorts/PortAudio clean

debian/changelog.upstream:
	git log remotes/branches/beta >| $@

get-orig-source: debian/changelog.upstream
	git archive --format=tar --prefix=$(srcpkg)-$(gitver)/ dfsg | \
        gzip -9 > $(srcpkg)_$(uver).orig.tar.gz
