#!/usr/bin/make -f
# debian/rules for FPTC

DEB_PACKAGE_NAME = view3dscene
export DH_ALWAYS_EXCLUDE := COPYING:LICENSE
export LANG:=C

# Define FPC
ifndef FPC
FPC=/usr/bin/fpc
endif
# Set FPCVER
FPCVER=$(shell ${FPC} -iV)

FPCTARGET=$(CPU_TARGET)-linux
# Get version information from changelog file
DEB_VERSION:=$(shell dpkg-parsechangelog | egrep '^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
#export PACKAGESUFFIX=-${DEB_UPSTREAM_MAIN_VERSION}
endif
# Get directories
CURDIR:=$(shell pwd)
INSTALL_DIR=$(CURDIR)/debian/tmp
BIN_DIR=${INSTALL_DIR}/usr/bin
DOC_DIR=${INSTALL_DIR}/usr/share/doc/${DEB_PACKAGE_NAME}
LIB_DIR=${INSTALL_DIR}/usr/lib/${DEB_PACKAGE_NAME}/${DEB_UPSTREAM_MAIN_VERSION}
MAN_DIR=${INSTALL_DIR}/usr/share/man
# 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
# Get current upstream CGE version as that is used in the path of it's files
CGEVERSION := $(shell dpkg-query -f '$${Version}'  -W 'fp-units-castle-game-engine' | sed -e 's/-[^-]*$$//g')
# Set default compilation options
DEB_HOST_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LDFLAGS=$(shell dpkg-buildflags --get LDFLAGS | sed -e 's/-Wl,//g' -e 's/,/ /g')
BUILDOPTS=-k"${LDFLAGS}"\
	-dRELEASE \
	-Mobjfpc \
	-Sh \
	-Ci \
	-Sm \
	-Sc \
	-Sg \
	-Si \
	-O2 \
	-Xs \
	-FU${LIB_DIR} \
	-FE${BIN_DIR} \
	-Fu/usr/lib/${DEB_HOST_MULTIARCH}/fp-units-${FPCVER}/castle-game-engine-${CGEVERSION}/

#export DH_VERBOSE=1

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

clean: clean-patched
	# 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 version file.
#	${RM} ide/version.inc
#	# Add version.inc:
#	echo \'$(DEB_VERSION)\' > version.inc
	if [ ! -f castleconf.inc ] ; then \
		ln -s `dpkg-query -L castle-game-engine-src | grep castleconf.inc` ./ ;\
	fi
	touch configure-stamp

build-arch: configure build-arch-stamp build-doc
build-arch-stamp:
	@echo "--- Building"
	dh_testdir
	${MKDIR} $(BIN_DIR)
	${MKDIR} $(LIB_DIR)
	fpc $(BUILDOPTS) view3dscene.lpr
	fpc $(BUILDOPTS) tovrmlx3d.lpr
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# Code to run the package test suite.
	# We need the directories where the files to test are to be
	# writeable by the current (non-root) user. Copying them in
	# the current tree is the easiest way to accomplish that.
	mkdir -p debian/tmp/kicad
	mkdir -p debian/tmp/whitedune
#	cp -R /usr/share/kicad/modules/ debian/tmp/kicad
	cp -R /usr/share/doc/whitedune-docs/ debian/tmp/whitedune
	PATH=$$PATH:debian/tmp/usr/bin/ ./run_tests.sh debian/tmp/run_tests_errors.txt debian/tmp/run_tests_output.txt
	cat debian/tmp/run_tests_errors.txt
	${RM} debian/tmp/kicad debian/tmp/whitedune
endif
	touch build-arch-stamp

install-arch: build-arch install-arch-stamp install-doc
install-arch-stamp:
	@echo "--- Installing"
	dh_testdir
	dh_testroot
	dh_installdirs
	# copy images, sounds, icons and menu entries for the GNOME menu
	${MKDIR} $(INSTALL_DIR)/usr/share/pixmaps
	convert -geometry 32x32 desktop/view3dscene.svg $(INSTALL_DIR)/usr/share/pixmaps/view3dscene.xpm
	${MKDIR} $(INSTALL_DIR)/usr/share/icons/hicolor/48x48/apps
	convert -geometry 48x48 desktop/view3dscene.svg $(INSTALL_DIR)/usr/share/icons/hicolor/48x48/apps/view3dscene.png
	#  build/share/images
	#  build/share/locale
	#  build/share/sounds
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
	find ${INSTALL_DIR} -empty -delete
	touch install-arch-stamp

build-indep: configure
install-indep:

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

build-doc: build-doc-stamp
build-doc-stamp:
	@echo "--- Building Documentation"
	dh_testdir
	# Need to activate the next line when pasdoc is packaged for Debian
#	${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
	# No need to do anything now as we take specific docs straight
	# from the source dir with dh_installdocs in the binary-indep target
	${MKDIR} ${DOC_DIR}
	#${CP} -t ${DOC_DIR} doc/*.txt doc/pasdoc/*
	touch install-doc-stamp

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

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


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

binary-indep: install-indep
	# No arch-indep package

binary-arch: install-arch
	@echo "--- Building: arch packages"
	dh_testdir
	dh_testroot
	dh_link
	dh_installdocs -a -X.in
	dh_installchangelogs -a
	dh_installexamples -a
	dh_install -a --list-missing
	dh_installmime -a
	dh_lintian
	dh_installmenu
	fpc-depends
	dh_installman -s
	dh_strip -s
	dh_compress -a
	dh_fixperms -a
	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 build-arch \
	install install-indep install-arch \
	configure

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