#!/usr/bin/make -f
# # Sample debian/rules that uses debhelper.
# # GNU copyright 1997 to 1999 by Joey Hess.
# #
# # Build comments:
# #  About 3.2 GB is needed on disc.
# #  orig sources are created by
# #    mkdir openscenegraph-xxx.orig
# #    cd openscenegraph-xxx.orig
# #    unzip OpenSceneGraph-xxx.zip
# #    mv OpenSceneGraph-xxx OpenSceneGraph
# #    cd ..
# #    cp -a openscenegraph-xxx.orig openscenegraph-xxx
# #    cd openscenegraph-xxx
# #    cp -a ../openscenegraph-yyy/debian .
# 
# 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  $@

# Use override_dh_* targets to customize this.

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
# Default to g++
CXX ?= g++

# Detect parallel builds.
ifneq (,$(findstring parallel,$(DEB_BUILD_OPTIONS)))
    PARALLEL_JOBS = $(shell echo $(DEB_BUILD_OPTIONS) | \
        sed -e 's/.*parallel=\([0-9]\+\).*/\1/')
    PARALLEL_OPTIONS = -j$(PARALLEL_JOBS)
endif

define SETAFFINITY
	grep 'sched_setaffinity.*cpusetsize' /usr/include/sched.h > /dev/null || export COMPILE_USING_TWO_PARAM_sched_setaffinity=-DCOMPILE_USING_TWO_PARAM_sched_setaffinity ; 
endef

OSG = OpenSceneGraph

ifeq (hppa,$(DEB_BUILD_ARCH))
ARCH_C_FLAGS=-mlong-calls
ARCH_CXX_FLAGS=${ARCH_C_FLAGS}
endif

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

LDFLAGS += -Wl,--as-needed

ifeq (armhf,$(DEB_HOST_ARCH))
ARMHF_DEFINES=-D OSG_GL1_AVAILABLE:BOOL=OFF \
	-D OSG_GL2_AVAILABLE:BOOL=OFF \
	-D OSG_GLES2_AVAILABLE:BOOL=ON \
	-D OPENGL_gl_LIBRARY:STRING=/usr/lib/arm-linux-gnueabihf/libGLESv2.so \
	-D OPENGL_egl_LIBRARY:STRING=/usr/lib/arm-linux-gnueabihf/libEGL.so
endif

#
# Shared libraries version numbers
#
# OpenSceneGraph/OpenThreads/Producer 0.9.8
#
#OPENSCENEGRAPH_SOVERSION=0
#
# OpenSceneGraph/OpenThreads/Producer 0.9.9
#
#OPENSCENEGRAPH_SOVERSION=1
#
# OpenSceneGraph/OpenThreads/Producer 1.0
#
#OPENSCENEGRAPH_SOVERSION=2
#
# OpenSceneGraph/OpenThreads/Producer 1.1
#
#OPENSCENEGRAPH_SOVERSION=3
#
# OpenSceneGraph/OpenThreads/Producer 1.2
#
#OPENSCENEGRAPH_SOVERSION=4
#
# OpenSceneGraph-1.9.1/OpenThreads-1.9.3 overriden with
# OpenSceneGraph-1.9.3, OpenSceneGraph-1.9.4, OpenSceneGraph-1.9.5, OpenSceneGraph-1.9.7 and
# OpenSceneGraph-2.0.0
#
#OPENSCENEGRAPH_SOVERSION=5
#
# OpenSceneGraph 2.2.0
#
#
#OPENSCENEGRAPH_SOVERSION=6
#
# OpenSceneGraph 2.4.0
#
#OPENSCENEGRAPH_SOVERSION=7
#OSG_VERSION=2.4.0
#
# OpenSceneGraph 2.6.1
#
#OPENSCENEGRAPH_SOVERSION=48
#OSG_VERSION=2.6.1
#
# OpenSceneGraph 2.8.0
#
#OPENSCENEGRAPH_SOVERSION=55
#OSG_VERSION=2.8.0
#
# OpenSceneGraph 2.8.1 and .2
#
#OPENTHREADS_SOVERSION=12
#OPENSCENEGRAPH_SOVERSION=56
#
# OpenSceneGraph 2.8.3
# OPENTHREADS_SOVERSION=13
# OPENSCENEGRAPH_SOVERSION=65
#
# OpenSceneGraph 2.9.10
# OPENTHREADS_SOVERSION=14
# OPENSCENEGRAPH_SOVERSION=68
#
# OpenSceneGraph 2.9.11
# OPENTHREADS_SOVERSION=14
# OPENSCENEGRAPH_SOVERSION=71
#
# OpenSceneGraph 2.9.13
# OPENTHREADS_SOVERSION=14
# OPENSCENEGRAPH_SOVERSION=73
#
# OpenSceneGraph 2.9.14
# OPENTHREADS_SOVERSION=14
# OPENSCENEGRAPH_SOVERSION=75
#
# OpenSceneGraph 2.9.16
# OPENTHREADS_SOVERSION=14
# OPENSCENEGRAPH_SOVERSION=77
#
# OpenSceneGraph 3.0.0
# OPENTHREADS_SOVERSION=14
# OPENSCENEGRAPH_SOVERSION=80
#
# OpenSceneGraph 3.2.0
# OPENTHREADS_SOVERSION=14
# OPENSCENEGRAPH_SOVERSION=99
#
# OpenSceneGraph 3.2.1
OPENTHREADS_SOVERSION=20
OPENSCENEGRAPH_SOVERSION=100

GRAB_VERSION_COMMAND := sed -n '1s~[^(]*(\(.*\)-.*).*~\1~p' debian/changelog | sed  's~+dfsg.*~~'
VERSION := $(shell $(GRAB_VERSION_COMMAND))
TMPDIR := $(shell mktemp -d)
TARBALL_NAME := openscenegraph_$(VERSION).orig.tar.gz
UPSTREAM_SVN_TAG_DIR := http://svn.openscenegraph.org/osg/OpenSceneGraph/tags/OpenSceneGraph

get-orig-source:
	dh_testdir
	mkdir -p $(TMPDIR)/openscenegraph-$(VERSION)
	svn export $(UPSTREAM_SVN_TAG_DIR)-$(VERSION) $(TMPDIR)/openscenegraph-$(VERSION)/$(OSG)
	tar czvf ../$(TARBALL_NAME) -C $(TMPDIR)/ openscenegraph-$(VERSION)
	rm -rf $(TMPDIR)

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

	dh_clean 

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

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

MANEXAMPLES = \
	osg2cpp.1 \
	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 \
	osgQtBrowser.1 \
	osgQtWidgets.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 \
	osgviewerQtContext.1 \
	osgviewerSDL.1 \
	osgvirtualprogram.1 \
	present3D.1 \
	osguserdata.1 \
	osgviewerQt.1 \
	osgviewerWX.1 \
	osgatomiccounter.1 \
	osgcomputeshaders.1 \
	osgframerenderer.1 \
	osgkeystone.1 \
	osgmultiplemovies.1 \
	osgmultitouch.1 \
	osgoscdevice.1 \
	osgsimplegl3.1 \
	osgsimpleshaders.1 \
	osgtessellationshaders.1

install-arch: build-arch
	dh_testdir
	dh_testroot

	# Add here commands to install the package into debian/tmp
	mkdir -p $(CURDIR)/debian/tmp/usr
	$(MAKE) -C build/osg DESTDIR=$(CURDIR)/debian/tmp install
	-mv $(CURDIR)/debian/tmp/usr/share/OpenSceneGraph/bin/* $(CURDIR)/debian/tmp/usr/bin
	for man in ${MANEXAMPLES} ; do rm -f $$man ; ln -s debian/osgexamples.1 $$man ; done
	for man in ${MANAPPLICATIONS} ; do rm -f $$man ; ln -s debian/osgapplications.1 $$man ; done

# Build architecture-independent files here.
binary-indep: install-indep
	dh_installdocs -i
	dh_installchangelogs -i
	dh_install -i -XCMakeLists.txt
	dh_lintian -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

install-indep: build-indep
	mkdir -p debian/tmp/usr/share/openscenegraph/
	cp -r OpenSceneGraph/examples debian/tmp/usr/share/openscenegraph

# Build architecture-dependent files here.
binary-arch: install-arch
	dh_install -a
	dh_installdocs -a
	dh_installchangelogs -a
	dh_installexamples -a
	dh_installman -p openscenegraph ${MANPAGES}
	dh_lintian -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_strip -a
	dh_makeshlibs -a
	dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

build: build-indep build-arch

build-indep: doc-stamp
doc-stamp:
	mkdir -p html
	doxygen debian/Doxyfile-openscenegraph
# Use Debian's jquery.js
	rm 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"|'
	touch doc-stamp

build-arch: build-stamp
build-stamp:
	dh_testdir

# Dynamic

	mkdir -p build/osg; cd build/osg ; ${SETAFFINITY} cmake --debug-output \
		-D OPENTHREADS_SOVERSION:STRING=${OPENTHREADS_SOVERSION} \
		-D OPENSCENEGRAPH_SOVERSION:STRING=${OPENSCENEGRAPH_SOVERSION} \
		-D CMAKE_INSTALL_PREFIX:PATH=/usr \
		-D BUILD_OSG_EXAMPLES:BOOL=ON \
		-D LIB_POSTFIX="/${DEB_HOST_MULTIARCH}" \
		${ARMHF_DEFINES} \
		../../${OSG}
	${MAKE} ${PARALLEL_OPTIONS} VERBOSE=1 -C build/osg

	touch build-stamp

.PHONY: build build-indep build-arch clean binary-indep binary-arch binary install-indep \
	install-arch get-orig-source
