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

DEB_PACKAGE_NAME=castle-game-engine
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 -)
# Used in dh-exec enhanced files
export CGE_VERSION=${DEB_UPSTREAM_VERSION}
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)
INSTALL_DIR=$(CURDIR)/debian/tmp
LIB_DIR=${INSTALL_DIR}/usr/lib/${DEB_PACKAGE_NAME}/${DEB_UPSTREAM_MAIN_VERSION}
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}
TMP_DIR=${INSTALL_DIR}/tmp
# Get utils
ifndef MKDIR
MKDIR=mkdir -p
endif
ifndef CP
CP=cp -Rfpl
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=

# Until we switch to shorthand dh, we need to set this ourselves for
# reproducible builds
export SOURCE_DATE_EPOCH = $(shell date -d "$$(dpkg-parsechangelog -SDate)" +%s)

#export DH_VERBOSE=1

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

clean: configure clean-patched
	${MAKE} cleanall
	# dh_clean gets a list of files from the debian/clean file
	dh_clean
clean-patched:
	@echo "--- Cleaning"
	dh_testdir
	$(MAKE) clean
	dh_prep

###################
# 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: configure build-arch-stamp
build-arch-stamp:
	@echo "--- Building"
	dh_testdir
	# Building Pascal style font file first while making sure that
	# no unit or object files polute the source tree
	${MKDIR} ${TMP_DIR}
	${FPC} -FE${TMP_DIR}  @castle-fpc.cfg examples/fonts/texturefont2pascal.lpr
	cd src/fonts ; PATH=${PATH}:${TMP_DIR} ./mk_fonts.sh

	# cge embeds some images from xcf via png into Pascal source files
	# First we need to create the pngs
	for pngfile in $$(grep \\\.png\$$ debian/clean) ; do xcf2png $${pngfile%.png}.xcf -o $${pngfile} ; done
	# Then we build the helper function
	${FPC} -FE${TMP_DIR} -dRELEASE @castle-fpc.cfg examples/images_videos/image_to_pas.lpr
	# To finally convert png files to pas file
	PATH=${TMP_DIR} ${MAKE} -C src/ui/opengl/gui-images all

	# For reproducible builds we need to make sure the timestamp of the just
	# created files is fixed. Let's use SOURCE_DATE_EPOCH for that.
	touch --no-dereference --date="@$${SOURCE_DATE_EPOCH}" \
		src/fonts/data/* \
		src/ui/opengl/castlecontrolsimages.*

	# Build the real engine
	${MAKE} all

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# Code to run the package test suite.
	# Command basically copied from tests/compile_console.sh while
	# making sure that no unit or object files polute the source tree
	${FPC}  -gh -dDEBUG -dTEXT_RUNNER -dNO_WINDOW_SYSTEM "$@" \
		-Fuexamples/shadow_fields/ -FE${TMP_DIR} \
		@castle-fpc.cfg tests/test_castle_game_engine.lpr
	# cd is needed to find the data files
	cd tests ; ${TMP_DIR}/test_castle_game_engine -a
endif

	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
	find ${INSTALL_DIR} -empty -delete
	touch install-arch-stamp

build-indep: configure build-doc
install-indep: install-doc install-source

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

build-doc: build-doc-stamp
build-doc-stamp:
	@echo "--- Building Documentation"
	dh_testdir
	${MAKE} -C doc/pasdoc ${DOCTYPE}
	touch build-doc-stamp

install-doc: build-doc install-doc-stamp
install-doc-stamp:
	@echo "--- Installing Documentation"
	dh_testdir
	dh_testroot
	# No need to do anything now as we take specific docs straight
	# from the source dir with dh_installdocs in the binary-indep target
	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: install-indep
	@echo "--- Building: arch-indep packages"
	dh_testdir
	dh_testroot
	dh_installdocs -i -X.in
	dh_installchangelogs -i
	dh_install -i -Xtmp/tmp/ --list-missing
	dh_link -i
	dh_lintian

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 -- -Vdejavu:version=`dpkg-query --show fonts-dejavu | awk '{print $$2}'`
	dh_md5sums -i
	dh_builddeb -i $(DEB_DH_BUILDDEB_ARGS)

binary-arch: install-arch
	@echo "--- Building: arch packages"
	dh_testdir
	dh_testroot
	dh_link -a
	dh_installdocs -a -X.in
	dh_installchangelogs -a
	dh_installexamples -a
	dh_install -a -Xtmp/tmp/ --list-missing
	dh_lintian
	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 -- -Vdejavu:version=`dpkg-query --show fonts-dejavu | awk '{print $$2}'`
	dh_md5sums -s
	dh_builddeb -s $(DEB_DH_BUILDDEB_ARGS)

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

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