#!/usr/bin/make -f

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

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

# At the moment, CMake discards CPPFLAGS, so we have to add its value
# to C{,XX}FLAGS variables

CFLAGS += $(CPPFLAGS)
CXXFLAGS += $(CPPFLAGS)

%:
	dh $@ --buildsystem cmake --parallel

# Use override_dh_* targets to customize this.

CFLAGS := ${CFLAGS} ${ARCH_C_FLAGS}
CXXFLAGS := ${CXXFLAGS} ${ARCH_CXX_FLAGS}

LDFLAGS += -Wl,--as-needed

GRAB_VERSION_COMMAND := sed -n '1s~[^(]*(\(.*\)-.*).*~\1~p' debian/changelog | sed  's~+dfsg.*~~'
VERSION := $(shell $(GRAB_VERSION_COMMAND))
TARBALL_NAME := OpenSceneGraph-$(VERSION).tar.gz

# Get soname from the source code
SONAME_VERSION:=$(shell perl -wne '/OPENSCENEGRAPH_SOVERSION\s+(\d+)/ and print $$1;' CMakeLists.txt)

debian/libopenscenegraph$(SONAME_VERSION).lintian-overrides:
	sed -e 's/@V@/$(SONAME_VERSION)/g' debian/libopenscenegraph.lintian_overrides.in > $@

get-orig-source:
	dh_testdir
	wget --directory-prefix=.. https://github.com/openscenegraph/OpenSceneGraph/archive/$(TARBALL_NAME)

override_dh_clean:
	dh_testdir
	dh_testroot

	# Add here commands to clean up after the build process.
	rm -fr build
	rm -f ${MANPAGES} 
	rm -fr html
	rm -f doxygen_sqlite3.db
	rm -f debian/libopenscenegraph$(SONAME_VERSION).lintian-overrides

	dh_clean 

MANPAGES = \
	${MANAPPLICATIONS} \
	${MANEXAMPLES}

MANAPPLICATIONS = \
	osgviewer.1 \
	osgconv.1 \
	osgversion.1 \
	osgfilecache.1 \
	osgarchive.1 \
	osg2cpp.1

MANEXAMPLES = \
	osganimate.1 \
	osganimationhardware.1 \
	osganimationmakepath.1 \
	osganimationnode.1 \
	osganimationskinning.1 \
	osganimationsolid.1 \
	osganimationtimeline.1 \
	osganimationviewer.1 \
	osgautocapture.1 \
	osgautotransform.1 \
	osgbillboard.1 \
	osgblendequation.1 \
	osgbrowser.1 \
	osgcallback.1 \
	osgcamera.1 \
	osgcatch.1 \
	osgclip.1 \
	osgcluster.1 \
	osgcompositeviewer.1 \
	osgcopy.1 \
	osgcubemap.1 \
	osgdelaunay.1 \
	osgdepthpartition.1 \
	osgdepthpeeling.1 \
	osgdistortion.1 \
	osgdrawinstanced.1 \
	osgfadetext.1 \
	osgfilecache.1 \
	osgfont.1 \
	osgforest.1 \
	osgfxbrowser.1 \
	osggameoflife.1 \
	osggeometry.1 \
	osggeometryshaders.1 \
	osghangglide.1 \
	osghud.1 \
	osgimagesequence.1 \
	osgimpostor.1 \
	osgintersection.1 \
	osgkdtree.1 \
	osgkeyboard.1 \
	osgkeyboardmouse.1 \
	osglauncher.1 \
	osglight.1 \
	osglightpoint.1 \
	osglogicop.1 \
	osglogo.1 \
	osgmanipulator.1 \
	osgmemorytest.1 \
	osgmotionblur.1 \
	osgmovie.1 \
	osgmultiplerendertargets.1 \
	osgmultitexture.1 \
	osgmultitexturecontrol.1 \
	osgoccluder.1 \
	osgocclusionquery.1 \
	osgpackeddepthstencil.1 \
	osgpagedlod.1 \
	osgparametric.1 \
	osgparticle.1 \
	osgparticleeffects.1 \
	osgpdf.1 \
	osgphotoalbum.1 \
	osgpick.1 \
	osgplanets.1 \
	osgpoints.1 \
	osgpointsprite.1 \
	osgprecipitation.1 \
	osgprerender.1 \
	osgprerendercubemap.1 \
	osgreflect.1 \
	osgrobot.1 \
	osgscalarbar.1 \
	osgscreencapture.1 \
	osgscribe.1 \
	osgsequence.1 \
	osgshaders.1 \
	osgshaderterrain.1 \
	osgshadow.1 \
	osgshape.1 \
	osgsharedarray.1 \
	osgsidebyside.1 \
	osgsimplifier.1 \
	osgsimulation.1 \
	osgslice.1 \
	osgspacewarp.1 \
	osgspheresegment.1 \
	osgspotlight.1 \
	osgstereoimage.1 \
	osgstereomatch.1 \
	osgteapot.1 \
	osgterrain.1 \
	osgtessellate.1 \
	osgtext.1 \
	osgtext3D.1 \
	osgtexture1D.1 \
	osgtexture2D.1 \
	osgtexture3D.1 \
	osgtexturerectangle.1 \
	osgthirdpersonview.1 \
	osgunittests.1 \
	osgvertexprogram.1 \
	osgviewerGLUT.1 \
	osgvolume.1 \
	osgwidgetaddremove.1 \
	osgwidgetbox.1 \
	osgwidgetcanvas.1 \
	osgwidgetframe.1 \
	osgwidgetinput.1 \
	osgwidgetlabel.1 \
	osgwidgetmenu.1 \
	osgwidgetmessagebox.1 \
	osgwidgetnotebook.1 \
	osgwidgetperformance.1 \
	osgwidgetscrolled.1 \
	osgwidgetshader.1 \
	osgwidgetstyled.1 \
	osgwidgettable.1 \
	osgwidgetwindow.1 \
	osgwindows.1 \
	osganalysis.1 \
	osganimationeasemotion.1 \
	osganimationmorph.1 \
	osgdatabaserevisions.1 \
	osgfpdepth.1 \
	osggpx.1 \
	osggraphicscost.1 \
	osgmultiviewpaging.1 \
	osgoit.1 \
	osgoutline.1 \
	osgparticleshader.1 \
	osgposter.1 \
	osgqfont.1 \
	osgshadercomposition.1 \
	osgshadergen.1 \
	osgtexturecompression.1 \
	osgthreadedterrain.1 \
	osguniformbuffer.1 \
	osguserstats.1 \
	osgvertexattributes.1 \
	osgviewerGTK.1 \
	osgviewerSDL.1 \
	osgvirtualprogram.1 \
	present3D.1 \
	osguserdata.1 \
	osgviewerWX.1 \
	osgatomiccounter.1 \
	osgcomputeshaders.1 \
	osgframerenderer.1 \
	osgkeystone.1 \
	osgmultiplemovies.1 \
	osgmultitouch.1 \
	osgoscdevice.1 \
	osgsimplegl3.1 \
	osgsimpleshaders.1 \
	osgtessellationshaders.1 \
	osgSSBO.1 \
	osgblenddrawbuffers.1 \
	osggpucull.1 \
	osgtexture2DArray.1 \
	osgtransferfunction.1 \
	osgtransformfeedback.1 \
	osgbindlesstext.1 \
	osgdeferred.1 \
	osgobjectcache.1 \
	osgsampler.1 \
	osgshadermultiviewport.1 \
	osgshaderpipeline.1 \
	osgsimpleMDI.1

override_dh_install-indep:
	dh_install -i -XCMakeLists.txt

override_dh_installman:
# Provably better done with --builddirectory= in the general options
# than specifying DEFAULT_BUILD_DIRECTORY.  From the sources I can see
# that always starts with "obj-"
	for man in ${MANAPPLICATIONS} ; do if ! [ -e debian/$$man ] ; then help2man -N --version-string="$(VERSION)" -S OpenSceneGraph obj-*/bin/$${man%.1} > $$man ; else cp debian/$$man .; fi; done
	for man in ${MANEXAMPLES} ; do rm -f $$man ; ln -s debian/osgexamples.1 $$man ; done
	dh_installman -p openscenegraph ${MANPAGES}

override_dh_auto_build-indep:
	mkdir -p html
	doxygen debian/Doxyfile-openscenegraph
# Use Debian's jquery.js
	rm -f html/openscenegraph/jquery.js
	find html -name "*.html" -print0 | xargs -0 perl -i -pe 's|src="jquery.js"|src="/usr/share/javascript/jquery/jquery.js"|'


override_dh_auto_configure: debian/libopenscenegraph$(SONAME_VERSION).lintian-overrides
	dh_auto_configure -- --debug-output \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr \
		-DBUILD_OSG_EXAMPLES:BOOL=ON \
		-DLIB_POSTFIX="/${DEB_HOST_MULTIARCH}" \
		-DCMAKE_RELWITHDEBINFO_POSTFIX="" \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DOSG_USE_LOCAL_LUA_SOURCE:BOOL=OFF \
