#!/usr/bin/make -f
# -*- makefile -*-
# 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

include /usr/share/dpatch/dpatch.make

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0 -g
else
	CFLAGS += -O3 -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
# Default to g++
CXX ?= g++
ifeq (ccache,$(findstring ccache,$(DEB_BUILD_OPTIONS)))
	CCACHE = /usr/lib/ccache/g++
else
	# Full path to make ccache happy
	CCACHE = $(shell which $(CXX))
endif

# 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


# shared library versions, option 1
# version=2.0.5
# major=2
# option 2, assuming the library is created as src/.libs/libfoo.so.2.0.5 or so
#version=`ls src/.libs/lib*.so.* | \
# awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'`
#major=`ls src/.libs/lib*.so.* | \
# awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'`

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	touch configure-stamp

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

# export COMPILE_INTROSPECTION=ON for the first if statement when that works.
ifeq (i386,$(DEB_BUILD_ARCH))
define INTROSPECTION
        binutils_version=$$(perl -ne 'if((/^Package: binutils$$/ .. /^Version:/) && /^Version: (.*)/) { print "$$1\n"; }' < /var/lib/dpkg/available) ; \
        if dpkg --compare-versions $${binutils_version} '>=' 2.17 ; then \
                echo 'Compiling introspection' ; \
                export COMPILE_INTROSPECTION=ON ; \
        else \
                export COMPILE_INTROSPECTION=OFF ; \
                echo "Not compiling introspection because binutils version $${binutils_version} is below 2.17 which is known to be bugous" ; \
                echo "  BFD 2.15 internal error, aborting at ../../bfd/elf32-i386.c line 2262 in elf_i386_relocate_section" ; \
                echo "  See http://www.mail-archive.com/bug-binutils@gnu.org/msg01883.html for more information" ; \
        fi ;
endef
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
#
OPENTHREADS_SOVERSION=12
OPENSCENEGRAPH_SOVERSION=56

build: patch build-stamp
build-stamp: configure-stamp 
	dh_testdir

	mkdir -p build/osg ; cd build/osg ; ${SETAFFINITY} ${INTROSPECTION} cmake --debug-output \
		-D OPENTHREADS_SOVERSION:STRING=${OPENTHREADS_SOVERSION} \
		-D OPENSCENEGRAPH_SOVERSION:STRING=${OPENSCENEGRAPH_SOVERSION} \
		-D CMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
		-D CMAKE_CXX_FLAGS_RELWITHDEBINFO="${CFLAGS} ${ARCH_CXX_FLAGS}" \
		-D CMAKE_C_FLAGS_RELWITHDEBINFO="${CFLAGS} ${ARCH_C_FLAGS}" \
		-D CMAKE_INSTALL_PREFIX:PATH=/ \
		-D CMAKE_CXX_COMPILER='${CCACHE}' \
		-D BUILD_OSG_WRAPPERS:BOOL=$$COMPILE_INTROSPECTION \
		-D BUILD_OSG_EXAMPLES:BOOL=ON \
		-D LIB_POSTFIX="" \
		-D OPENGL_egl_LIBRARY:STRING=/usr/lib/libEGL.so \
		-D OPENGL_INCLUDE_DIR:PATH=/usr/include/GLES2 \
		-D OPENGL_gl_LIBRARY:FILEPATH=/usr/lib/libGLESv2.so \
		-D OSG_CPP_EXCEPTIONS_AVAILABLE:BOOL=OFF \
		-D OSG_GL1_AVAILABLE:BOOL=OFF \
		-D OSG_GL2_AVAILABLE:BOOL=OFF \
		-D OSG_GL3_AVAILABLE:BOOL=OFF \
		-D OSG_GLES1_AVAILABLE:BOOL=OFF \
		-D OSG_GLES2_AVAILABLE:BOOL=ON \
		-D OSG_GLU_AVAILABLE:BOOL=OFF \
		-D OSG_GL_DISPLAYLISTS_AVAILABLE:BOOL=OFF \
		-D OSG_GL_FIXED_FUNCTION_AVAILABLE:BOOL=OFF \
		-D OSG_GL_MATRICES_AVAILABLE:BOOL=OFF \
		-D OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE:BOOL=OFF \
		-D OSG_GL_VERTEX_FUNCS_AVAILABLE:BOOL=OFF \
		${LINKER_FLAGS} \
		../../${OSG}
	cd build/osg ; ${MAKE} ${PARALLEL_OPTIONS} VERBOSE=1
	touch build-stamp

clean: clean-patched unpatch
clean-patched:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	rm -fr build
	rm -f ${MANPAGES} 
	rm -fr html

	dh_clean 

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

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

MANEXAMPLES = \
	osg2cpp.1 \
	osganimate.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 \
	osggeodemo.1 \
	osggeometry.1 \
	osggeometryshaders.1 \
	osghangglide.1 \
	osghud.1 \
	osgimagesequence.1 \
	osgimpostor.1 \
	osgintersection.1 \
	osgintrospection.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

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp
	mkdir -p $(CURDIR)/debian/tmp/usr
	$(MAKE) -C build/osg DESTDIR=$(CURDIR)/debian/tmp/usr install
	mkdir -p $(CURDIR)/debian/tmp/usr/bin
	-mv $(CURDIR)/debian/tmp/usr/share/OpenSceneGraph/bin/* $(CURDIR)/debian/tmp/usr/bin
	mkdir -p $(CURDIR)/debian/tmp/usr/share/openscenegraph/
	cp -r OpenSceneGraph/examples $(CURDIR)/debian/tmp/usr/share/openscenegraph
	mkdir -p html
	doxygen debian/Doxyfile-openscenegraph
	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: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman -p openscenegraph ${MANPAGES}
	dh_install --sourcedir=debian/tmp --fail-missing
	dh_install -plibopenscenegraph56 debian/lintian/libopenscenegraph56 usr/share/lintian/overrides
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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