#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independent
# package.

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

# This is the debhelper compatability version to use.
export DH_COMPAT=4
# This has to be exported to make some magic below work.
export DH_OPTIONS

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), ia64))
	CONFFLAG = --without-lib3ds --without-libode
endif
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), hppa))
	CONFFLAG = --without-lib3ds --without-libode
endif
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), s390))
	CONFFLAG = --without-lib3ds --without-libode
endif
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), amd64))
	CONFFLAG = --without-lib3ds --without-libode
endif
NEWRENDERER = --enable-new-renderer
#PYTHON = --without-python
CPUOPTIM = --enable-cpu-specific-optimizations=no
CONFDIR = --prefix=/usr --sysconfdir=/etc

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Autobuild debugging stuff
	dh_buildinfo generate cat
	# Add here commands to configure the package.
	# Activate all plugins
	# @@@ FIXME: This does not handle plugins protected by ifeq/ifneq.
#	perl -pi -e "s/^#PLUGINS/PLUGINS/" $(CURDIR)/CS/mk/user.mak
	# Temp remove of render3d renderers
	#perl -pi -e "s/^PLUGINS(.*render3.*$$)/#PLUGINS\1/" $(CURDIR)/CS/mk/user.mak
##	perl -pi -e "s/^PLUGINS(.*glshader_cg$$)/#PLUGINS\1/" $(CURDIR)/CS/mk/user.mak
	# Temp? remove of cslua
	#perl -pi -e "s/^PLUGINS(.*cslua.*$$)/#PLUGINS\1/" $(CURDIR)/CS/mk/user.mak
	# Temp? remove of openal
	#perl -pi -e "s/^PLUGINS(.*openal.*$$)/#PLUGINS\1/" $(CURDIR)/CS/mk/user.mak
	# Remove broken support of python because of swig or g++ on s390 and powerpc
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), s390))
	perl -pi -e "s/^PLUGINS(.*cspython.*$$)/#PLUGINS\1/" $(CURDIR)/CS/mk/user.mak
endif
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), powerpc))
	perl -pi -e "s/^PLUGINS(.*cspython.*$$)/#PLUGINS\1/" $(CURDIR)/CS/mk/user.mak
endif
	find $(CURDIR)/CS -name .cvsignore | xargs rm -f
	# --disable-qsqrt was added for a gcc3.2 bug
	#  then removed and I had to replace with cs_cv_cxx_qsqrt_ok=no ./configure
	#  then a correct test was added in configure
	#  this is adding #define CS_NO_QSQRT in CS/include/volatile.h
	# Don't support until this is dynamic lib
	# --enable-meta-info-embedding was by default before (enbed .csplugin info)
	perl -pi -e "s:\[crystal\]:\[crystalspace\]:" $(CURDIR)/CS/configure.ac
	cd CS; ./bin/autogen.sh ; cs_cv_prog_cxx_local_include=no ./configure $(CONFDIR) $(CPUOPTIM) $(PYTHON) $(NEWRENDERER) $(CONFFLAG)

	# Install as much as possible
#	perl -pi -e "s/#TO_INSTALL/TO_INSTALL/" $(CURDIR)/CS/cache.mak
	# Activating scripts
	chmod +x $(CURDIR)/debian/createmenus
#	chmod +x $(CURDIR)/debian/addmissing
	chmod +x $(CURDIR)/debian/automan
	# Creating menus
	$(CURDIR)/debian/createmenus $(CURDIR)
	# Some path adjustment
#	perl -pi -e "s:/usr/local/crystal:/usr/lib/crystalspace:" $(CURDIR)/CS/libs/csutil/generic/instpath.cpp
#	perl -pi -e "s:/usr/local/crystal:/usr/lib/crystalspace:" $(CURDIR)/CS/libs/csutil/unix/instpath.cpp

	touch configure-stamp

#Architecture
build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp

	# Add here commands to compile the package.
	# For arm, powerpc, s390
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), arm))
	echo "CFLAGS.SYSTEM += -fsigned-char"  >> $(CURDIR)/CS/config.mak 
endif
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), powerpc))
	echo "CFLAGS.SYSTEM += -fsigned-char"  >> $(CURDIR)/CS/config.mak 
endif
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), s390))
	echo "CFLAGS.SYSTEM += -fsigned-char"  >> $(CURDIR)/CS/config.mak 
endif
ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), hppa))
	echo "CFLAGS.SYSTEM += -fsigned-char"  >> $(CURDIR)/CS/config.mak 
endif

	# Compile static libs without -fPIC (except for ia64, amd64, hppa and alpha)
#ifneq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), amd64))
#ifneq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), ia64))
#ifneq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), hppa))
#ifneq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH), alpha))
#	cd $(CURDIR)/CS; $(MAKE) libs
#endif
#endif
#endif
	# Force -fPIC as compiling/linking option
	echo "CFLAGS.SYSTEM += -fPIC"  >> $(CURDIR)/CS/config.mak 
	echo "LFLAGS.EXE += -fPIC"  >> $(CURDIR)/CS/config.mak 
	echo "LFLAGS.DLL += -fPIC"  >> $(CURDIR)/CS/config.mak 
	echo "LIBS.EXE += -fPIC"  >> $(CURDIR)/CS/config.mak 
	# Make dynamic version of libs
	# not yet usable
	#cd $(CURDIR)/CS; $(MAKE) libs USE_SHARED_LIBS=yes
	# Build the rest
	# Blocks is not anymore in CS CVS
	# Ok this is rather dirty but i didn't find where to put this for blocks (only for dyn lib in fact)
	#echo "LIBS.EXE += -lz"  >> $(CURDIR)/CS/config.mak 
	cd $(CURDIR)/CS; $(MAKE) all
	# should be too something like this $(MAKE) all USE_SHARED_LIBS=yes

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp

	# Add here commands to compile the indep part of the package.
	#$(MAKE) doc

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp

	# Add here commands to clean up after the build process.
	[ -f $(CURDIR)/CS/Makefile ] && (cd $(CURDIR)/CS; $(MAKE) distclean) || true

	dh_clean

DEVBIN=map2cs mdl2spr vsh cs-config 3ds2lev csfgen cslight maya2spr md32spr milk2spr python.cex tbconvert

install: install-indep install-arch
install-indep:

	dh_testdir
	dh_testroot
	dh_clean -k -i
	dh_installdirs -i

	# Add here commands to install the indep part of the package into
	# debian/<package>-doc.
	#INSTALLDOC#
	cd CS; DESTDIR=$(CURDIR)/debian/crystalspace-doc/usr/share $(MAKE) install_doc

install-arch:
	dh_testdir
	dh_testroot
	dh_clean -k -a
	dh_installdirs -a

	# Add here commands to install the package into debian/crystalspace.
	cd CS; DESTDIR=$(CURDIR)/debian/crystalspace $(MAKE) install_bin 
	cd CS; DESTDIR=$(CURDIR)/debian/crystalspace $(MAKE) install_lib 
	cd CS; DESTDIR=$(CURDIR)/debian/crystalspace $(MAKE) install_plugin 
	cd CS; DESTDIR=$(CURDIR)/debian/crystalspace/usr/share $(MAKE) install_data 
	cd CS; DESTDIR=$(CURDIR)/debian/crystalspace $(MAKE) install_config 

	cd CS; DESTDIR=$(CURDIR)/debian/crystalspace-dev $(MAKE) install_include

	# Let's fix some things
	#chmod +x $(CURDIR)/debian/crystalspace/usr/share/crystalspace/bindings/python/tutor*.py
	#rm -f $(CURDIR)/debian/crystalspace/usr/share/doc/crystalspace-0.99/LICENSE.gz
	#rm -f $(CURDIR)/debian/crystalspace-doc/usr/share/doc/crystalspace-0.99/LICENSE.gz
	#chmod +x $(CURDIR)/debian/crystalspace/usr/share/crystalspace/build/autoconf/config.guess
	#chmod +x $(CURDIR)/debian/crystalspace/usr/share/crystalspace/build/autoconf/config.sub
	#chmod +x $(CURDIR)/debian/crystalspace/usr/share/crystalspace/build/autoconf/install-sh
	#chmod +x $(CURDIR)/debian/crystalspace/usr/share/crystalspace/build/jamtemplate/autogen.template
	#[ ! -d $(CURDIR)/debian/crystalspace/usr/share/crystalspace/data/maps/flarge/factories ] && \
	#	cp -r $(CURDIR)/CS/data/flarge/factories \
	#	$(CURDIR)/debian/crystalspace/usr/share/crystalspace/data/maps/flarge 

	# Calculate ligths
	cd $(CURDIR)/CS ; ./cslight -video=null \
		$(CURDIR)/debian/crystalspace/usr/share/crystalspace/data/maps/flarge/

	# Adding home made xpm
	mkdir -p $(CURDIR)/debian/crystalspace/usr/share/pixmaps
	cp $(CURDIR)/xpm/*.xpm \
		$(CURDIR)/debian/crystalspace/usr/share/pixmaps

	# Autogenerate man
	$(CURDIR)/debian/automan $(CURDIR)
	$(CURDIR)/debian/rules man
	rm -f $(CURDIR)/debian/man/*.sgml.?
#	dh_installman -p crystalspace-demos $(CURDIR)/debian/man/*.6
#	dh_installman -p crystalspace-dev $(CURDIR)/debian/man/*.1
	dh_installman -p crystalspace $(CURDIR)/debian/man/*.1

%.xpm: %.jpg
	convert -size 32x32 $< $@
	mogrify -format xpm -geometry 32x32 -map /usr/share/pixmaps/cmap.xpm $@

man: $(subst .sgml,, $(wildcard $(CURDIR)/debian/man/*.sgml*))
%.1: %.sgml.1
	@echo "Creating $(notdir $@)"
	@docbook-to-man $< > $@
%.6: %.sgml.6
	@echo "Creating $(notdir $@)"
	@docbook-to-man $< > $@

# Must not depend on anything. This is to be called by
# binary-arch/binary-multi
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
#	dh_installdebconf	
	dh_installdocs
	# Autobuild debugging stuffs
	dh_buildinfo install
	dh_installexamples
	dh_installmenu
#	dh_installemacsen
#	dh_installpam
#	dh_installinit
	dh_installcron
#	dh_installmanpages
	dh_installinfo
#	dh_undocumented
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
#	dh_perl
	#dh_shlibdeps -l$(CURDIR)/debian/usr/lib/crystalspace/lib
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb
# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

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