#!/usr/bin/make -f
# debian/rules for Castle Game Engine

DEB_PACKAGE_NAME=castle-game-engine
DEB_DH_BUILDDEB_ARGS := -- -Z bzip2
export DH_ALWAYS_EXCLUDE := COPYING:LICENSE
export LANG:=C

# Documentation type to use chm/html
ifeq (${DEBDOCTYPE},)
DOCTYPE=html
# HTML Converter chmdocs or htmldocs for (default)
CONVERTER=${DOCTYPE}docs
else
DOCTYPE=${DEBDOCTYPE}
CONVERTER=${DOCTYPE}docs
endif
# Define FPC
ifndef FPC
FPC=/usr/bin/fpc
endif
# Set FPCVER
FPCVER=$(shell ${FPC} -iV)

CPU_TARGET?=$(shell ${FPC} -iTP)
OS_TARGET?=$(shell ${FPC} -iTO)
FPCTARGET=${CPU_TARGET}-${OS_TARGET}
# Get version information from changelog file
DEB_VERSION:=$(shell dpkg-parsechangelog | grep '^Version:' | cut -f 2 -d ' ')
DEB_UPSTREAM_VERSION:=$(shell echo $(DEB_VERSION) | cut -f 1 -d -)
DEB_UPSTREAM_MAIN_VERSION:=$(shell echo ${DEB_UPSTREAM_VERSION} | sed -e 's/^\([0-9\.]*\).*/\1/')
DEB_BUILD=$(lastword $(subst -, ,${DEB_VERSION}))
ifndef PACKAGESUFFIX
PACKAGESUFFIX=-${DEB_UPSTREAM_MAIN_VERSION}
endif
DEB_HOST_MULTIARCH=$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
# Get directories
CURDIR:=$(shell pwd)
BUILD_DIR=$(CURDIR)/debian/build
INSTALL_DIR=$(CURDIR)/debian/tmp
BIN_DIR=${INSTALL_DIR}/usr/bin
LIB_DIR=${INSTALL_DIR}/usr/lib/${DEB_PACKAGE_NAME}/${DEB_UPSTREAM_MAIN_VERSION}
DOC_DIR=${INSTALL_DIR}/usr/share/doc/${DEB_PACKAGE_NAME}
SRC_DIR=${INSTALL_DIR}/usr/src/${DEB_PACKAGE_NAME}-${DEB_UPSTREAM_MAIN_VERSION}
UNITS_DIR=${INSTALL_DIR}/usr/lib/${DEB_HOST_MULTIARCH}/fp-units-${FPCVER}/${DEB_PACKAGE_NAME}-${DEB_UPSTREAM_MAIN_VERSION}
# Get utils
ifndef MKDIR
MKDIR=mkdir -p
endif
ifndef CP
CP=cp -Rfpl
endif
ifndef MV
MV=mv
endif
RM:=rm -rf
# Get fpcmake from path if none is specified.
ifndef FPCMAKE
FPCMAKE=fpcmake
endif
# Define FPCDIR if it was not set
ifndef FPCDIR
export FPCDIR=/usr/lib/fpc/${FPCVER}
endif
ifndef LAZBUILD
LAZBUILD=lazbuild
endif
# Set default compilation options
BUILDOPTS=

# Configure patch system
include /usr/share/quilt/quilt.make

#export DH_VERBOSE=1

###################
# Clean
#

clean: patch configure clean-patched unpatch
	${RM} build-arch-stamp install-arch-stamp arrange-arch-stamp configure-stamp
	${RM} build-doc-stamp install-doc-stamp
	${RM} build-indep-stamp install-indep-stamp
	${RM} debian-files-stamp
	${RM} debian/files debian/docs debian/dirs debian/*.files debian/*.docs debian/*.examples debian/*.postinst debian/*.postrm
	${RM} .pc
	# Remove auto-generated version file.
	${RM} ide/version.inc
	# Remove auto-generated make files.
	find -name Makefile.fpc -execdir 'sh' '-c' '${RM} $$(basename {} .fpc)' ';'
	dh_clean -X'examples/3d_sound_game/#lets_take_a_walk.lpr#'
clean-patched:
	@echo "--- Cleaning"
	dh_testdir
	dh_testroot
	$(MAKE) clean
	find -regex '.*\.\(a\|or?\|so\.*\|ppu\)' -delete
	dh_prep

###################
# Debian files
#

debian-files: patch debian-files-stamp
debian-files-stamp:
	@echo "--- Creating/fixing *.install files"
	#/bin/bash debian/fixdeb debian $(FPCTARGET) $(PPNEW)
	touch debian-files-stamp

###################
# Arch packages
#

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Remove auto-generated make files.
	find -name Makefile.fpc -execdir 'sh' '-c' '${RM} $$(basename {} .fpc)' ';'
#	# Regenrate make files using fpcmake.
#	${FPCMAKE} -r -Tall
#	# Remove auto-generated version file.
#	${RM} ide/version.inc
#	# Add version.inc:
#	echo \'$(DEB_VERSION)\' > version.inc
	touch configure-stamp

build-arch: debian-files configure build-arch-stamp
build-arch-stamp:
	@echo "--- Building"
	dh_testdir
	${MAKE} all
	touch build-arch-stamp

install-arch: build-arch install-arch-stamp
install-arch-stamp:
	@echo "--- Installing"
	dh_testdir
	dh_testroot
	dh_installdirs
	# Install units
	${MKDIR} ${UNITS_DIR}
	${CP} -t ${UNITS_DIR} units/${FPCTARGET}/*
	# copy images, sounds, icons and menu entries for the GNOME menu
	#${MKDIR} $(INSTALL_DIR)/usr/share
	#${CP} -t $(INSTALL_DIR)/usr/share \
	#  build/share/applications \
	#  build/share/icons \
	#  build/share/images \
	#  build/share/locale \
	#  build/share/man \
	#  build/share/sounds
	# Mark package as manually compilable and install them
	for package in $$(find * -name '*.lpk') ; \
	do \
		pkgDir=$$(dirname "${LIB_DIR}/$${package}") ; \
		${MKDIR} "$${pkgDir}" ; \
		sed -e 's@\(\W*\)<CompilerOptions>.*@\1<AutoUpdate Value="Manually"/>\n&@' \
		    -e "s@\.\./src/@$$(relpath ${SRC_DIR} $${pkgDir})/@g" \
			-e "s@\(\W*\)<UnitOutputDirectory .*@<UnitOutputDirectory Value=\"$$(relpath ${UNITS_DIR} $${pkgDir})\"/>@" \
		    "$${package}" > "${LIB_DIR}/$${package}" ; \
	done
	# Install lintian override files
	set -x -e ; \
	if [ -d debian/overrides ] ; \
	then \
	 	cd debian/overrides ; \
		for PACKAGE in * ; do \
			if [ -d ../$${PACKAGE}${PACKAGESUFFIX} ] ; then \
		  		${MKDIR} ../$${PACKAGE}${PACKAGESUFFIX}/usr/share/lintian/overrides ; \
				sed -e "s/$${PACKAGE}/&${PACKAGESUFFIX}/g" $${PACKAGE} > ../$${PACKAGE}${PACKAGESUFFIX}/usr/share/lintian/overrides/$${PACKAGE}${PACKAGESUFFIX} ; \
			fi ; \
		done ; \
	fi ;
	find ${INSTALL_DIR} -empty -delete
	touch install-arch-stamp

arrange-arch: install-arch arrange-arch-stamp install-man
arrange-arch-stamp:
	dh_testdir
	dh_testroot
	dh_install -s --sourcedir=debian/tmp --list-missing
	touch arrange-arch-stamp

build-indep: patch
install-indep:
	touch install-indep-stamp

install-man: install-man-stamp
install-man-stamp:
	# Install man pages
	dh_installman -s
	touch install-man-stamp

###################
# Documentation
#

build-doc: patch debian-files build-doc-stamp
build-doc-stamp:
	@echo "--- Building Documentation"
	dh_testdir
	#${MAKE} -C doc/pasdoc ${CONVERTER} ${BUILDOPTS}
	touch build-doc-stamp

install-doc: build-doc install-doc-stamp
install-doc-stamp:
	@echo "--- Installing Documentation"
	dh_testdir
	dh_testroot
	${MKDIR} ${DOC_DIR}
	${CP} -t ${DOC_DIR} \
		doc/kambi_coding_conventions.txt \
		doc/naming_engine_3.0.txt \
		doc/TODO.txt \
		doc/kambi_command_line_params.txt \
		doc/naming_engine_4.0.txt \
		doc/profiling_howto.txt \
		doc/TODO.vrml_x3d_specs.txt \
		doc/pasdoc/auto_link_exclude.txt \
		doc/pasdoc/introduction_removed_outdated_delphi_notes.pasdoc \
		doc/pasdoc/external_class_hierarchy.txt \
		doc/pasdoc/introduction.pasdoc \
		doc/pasdoc/README.txt \
		doc/pasdoc/html
	touch install-doc-stamp

###################
# Source
#

install-source: install-source-stamp
install-source-stamp:
	@echo "--- Cleaning the tree and copying the source code"
	dh_testdir
	dh_testroot
	${MKDIR} ${SRC_DIR}
	${CP} -t ${SRC_DIR} \
		$(CURDIR)/src/*
	find $(INSTALL_DIR) -name '*.bmp' -exec chmod 644 '{}' ';'
	find ${SRC_DIR} -empty -delete
	touch install-source-stamp

###################
# Generic
#

build: build-arch build-indep
install: install-arch install-indep
binary: binary-arch binary-indep


###################
# Deb building
#

binary-indep: clean-patched build-doc install-doc install-source debian-files
	@echo "--- Building: arch-indep packages"
	dh_testdir
	dh_testroot
	dh_installdocs -i -X.in
	dh_installchangelogs -i
	dh_install -i --sourcedir=debian/tmp --list-missing
	# Install lintian override files
	set -x -e ; \
	if [ -d debian/overrides ] ; \
	then \
	 	cd debian/overrides ; \
		for PACKAGE in * ; do \
			if [ -d ../$${PACKAGE}${PACKAGESUFFIX} ] ; then \
		  		${MKDIR} ../$${PACKAGE}${PACKAGESUFFIX}/usr/share/lintian/overrides ; \
				sed -e "s/$${PACKAGE}/&${PACKAGESUFFIX}/g" $${PACKAGE} > ../$${PACKAGE}${PACKAGESUFFIX}/usr/share/lintian/overrides/$${PACKAGE}${PACKAGESUFFIX} ; \
			fi ; \
		done ; \
	fi ;
ifeq (${DEB_BUILD},0)
	# We are building upstream debs, install override files for build = 0
	set -x -e ; cd debian ; for PACKAGE in `sed -n -e 's/Package: \(.*\)/\1/p' control` ; do \
	  if [ -d $${PACKAGE} ] ; \
	  then \
	    ${MKDIR} $${PACKAGE}/usr/share/lintian/overrides ; \
	    echo "$${PACKAGE}: debian-revision-should-not-be-zero" >> $${PACKAGE}/usr/share/lintian/overrides/$${PACKAGE} ; \
	  fi ; \
	done
endif
	dh_compress -i -X.pdf
	dh_fixperms
	dh_installdebconf -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i $(DEB_DH_BUILDDEB_ARGS)

binary-arch: arrange-arch
	@echo "--- Building: arch packages"
	dh_testdir
	dh_testroot
	dh_link
	dh_installdocs -a -X.in
	dh_installchangelogs -a
	dh_installexamples -a
	dh_installmenu
	fpc-depends
	dh_installman -s
	dh_strip -s
	dh_compress -a
	dh_fixperms
	dh_installdebconf -a
	dh_installdeb -a
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s $(DEB_DH_BUILDDEB_ARGS)

.PHONY: build clean binary binary-arch \
	binary-indep debian-files build-arch \
	install install-indep install-arch \
	configure

get-orig-source:
	-uscan --upstream-version=0 --rename
