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

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)

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/')
# Get directories
CURDIR:=$(shell pwd)
BUILD_DIR=$(CURDIR)/debian/build
INSTALL_DIR=$(CURDIR)/debian/tmp
DOC_DIR=${INSTALL_DIR}/usr/share/doc/lazarus/${DEB_UPSTREAM_MAIN_VERSION}
SRC_DIR=$(INSTALL_DIR)/usr/lib/lazarus/${DEB_UPSTREAM_MAIN_VERSION}
# Get utils
ifndef MKDIR
MKDIR=mkdir -p
endif
ifndef CP
CP=cp -Rfpl
endif
RM:=rm -rf
ifndef LAZBUILD
LAZBUILD=${CURDIR}/lazbuild
endif
# Set default compilation options
BUILDOPTS=
INSTALLOPTS=INSTALL_PREFIX=$(INSTALL_DIR)/usr
# 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/share/fpcsrc/${FPCVER}
endif
# 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 
clean-patched:
	@echo "--- Cleaning"
	dh_testdir
	dh_testroot
	${RM} lazarus startlazarus lazbuild
	$(MAKE) distclean ${BUILDOPTS}
	$(MAKE) -C docs/html clean
	find -regex '.*\.\(a\|o\|so\.*\|ppu\)' -delete


###################
# 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)\' > ide/version.inc
	touch configure-stamp

build-arch: debian-files configure build-arch-stamp
build-arch-stamp:
	@echo "--- Building"
	dh_testdir
	$(MAKE) lcl packager/registration ideintf bigidecomponents examples $(BUILDOPTS)
	$(MAKE) bigide USESVN2REVISIONINC=0 $(BUILDOPTS)
	mv $(CURDIR)/lazarus $(CURDIR)/lazarus-gtk2
	$(MAKE) lazbuilder $(BUILDOPTS)
	$(MAKE) tools $(BUILDOPTS)
	touch build-arch-stamp

install-arch: build-arch install-arch-stamp
install-arch-stamp:
	@echo "--- Installing"
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	# create all necessary directories
	install -d $(INSTALL_DIR)/usr/bin
	install -d $(INSTALL_DIR)/usr/lib/lazarus/${DEB_UPSTREAM_MAIN_VERSION}/tools
	# Install the IDE & its accessories
	${CP} -t $(INSTALL_DIR)/usr/lib/lazarus/${DEB_UPSTREAM_MAIN_VERSION} \
		$(CURDIR)/lazarus-gtk2 \
		$(CURDIR)/lazbuild \
		$(CURDIR)/startlazarus \
		$(CURDIR)/languages
	${CP} -t $(INSTALL_DIR)/usr/lib/lazarus/${DEB_UPSTREAM_MAIN_VERSION}/tools \
		$(CURDIR)/tools/lazres \
		$(CURDIR)/tools/lrstolfm \
		$(CURDIR)/tools/svn2revisioninc \
		$(CURDIR)/tools/updatepofiles
	# Install the library files
	find -regex '.*\.\(a\|o\|so\.*\|ppu\)' | xargs ${CP} --parents -t $(INSTALL_DIR)/usr/lib/lazarus/${DEB_UPSTREAM_MAIN_VERSION}
	# copy icons and menu entries for the GNOME menu
	install -d $(INSTALL_DIR)/usr/share/pixmaps
	install -d $(INSTALL_DIR)/usr/share/applications
	convert -geometry 32x32 $(CURDIR)/images/ide_icon48x48.png $(INSTALL_DIR)/usr/share/pixmaps/lazarus.xpm
	chmod 644 $(INSTALL_DIR)/usr/share/pixmaps/*.xpm
	install -m 644 $(CURDIR)/install/lazarus.desktop $(INSTALL_DIR)/usr/share/applications/lazarus.desktop
	# Install man pages
	$(MAKE) -C install/man installman $(INSTALLOPTS) INSTALL_PREFIX=$(INSTALL_DIR)/usr/share
	# Install configuration files
	${MKDIR} $(INSTALL_DIR)/etc/lazarus
	fpcsubst -d LazarusVersion=${DEB_UPSTREAM_MAIN_VERSION} -i ${CURDIR}/tools/install/linux/environmentoptions.xml -o $(INSTALL_DIR)/etc/lazarus/environmentoptions.xml
	fpcsubst -d LazarusVersion=${DEB_UPSTREAM_MAIN_VERSION} -i ${CURDIR}/tools/install/linux/helpoptions.xml -o $(INSTALL_DIR)/etc/lazarus/helpoptions.xml
	# Install contributer list
	install -d $(INSTALL_DIR)/usr/lib/lazarus/${DEB_UPSTREAM_MAIN_VERSION}/docs
	${CP} -t $(INSTALL_DIR)/usr/lib/lazarus/${DEB_UPSTREAM_MAIN_VERSION}/docs \
		$(CURDIR)/docs/Contributors.txt \
		$(CURDIR)/docs/contributors.utf8 \
		$(CURDIR)/docs/acknowledgements.txt
	# Install lintian override files
	set -x; cd debian/overrides ; for PACKAGE in * ; do \
	  if [ -d ../$${PACKAGE} ] ; \
	  then \
	    install -m644 -D $${PACKAGE} \
	      ../$${PACKAGE}/usr/share/lintian/overrides/$${PACKAGE} ; \
	  fi ; \
	done
	find ${INSTALL_DIR} -empty -delete
	touch install-arch-stamp

arrange-arch: install-arch arrange-arch-stamp
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

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

build-doc: patch debian-files build-doc-stamp
build-doc-stamp:
	@echo "--- Building Documentation"
	dh_testdir
	$(MAKE) -C docs/html ${CONVERTER}
	touch build-doc-stamp

install-doc: build-doc install-doc-stamp
install-doc-stamp:
	@echo "--- Installing Documentation"
	dh_testdir
	dh_testroot
	dh_prep
	${MAKE} -C docs/html ${DOCTYPE}install INSTALL_PREFIX=${INSTALL_DIR}/usr INSTALL_DOCDIR=${DOC_DIR}
	cd docs ; ${CP} --parents -t ${DOC_DIR} \
	  index.html \
	  images/laztitle.png
	find ${DOC_DIR} -empty -delete
	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)/Makefile* \
		$(CURDIR)/components \
		$(CURDIR)/converter \
		$(CURDIR)/debugger \
		$(CURDIR)/designer \
		$(CURDIR)/doceditor \
		$(CURDIR)/examples \
		$(CURDIR)/ide \
		$(CURDIR)/ideintf \
		$(CURDIR)/images \
		$(CURDIR)/lcl \
		$(CURDIR)/packager \
		$(CURDIR)/test \
		$(CURDIR)/tools
	find ${SRC_DIR} -regex '.*\.\(bmp\|jpg\|png\|xpm\)' -delete
	$(RM) ${SRC_DIR}/components/lazreport/license*.txt
	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; cd debian/overrides ; for PACKAGE in * ; do \
	  if [ -d ../$${PACKAGE} ] ; \
	  then \
	    install -m644 -D $${PACKAGE} \
	      ../$${PACKAGE}/usr/share/lintian/overrides/$${PACKAGE} ; \
	  fi ; \
	done
	dh_compress -i -X.pdf
	dh_fixperms -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
	dh_installchangelogs -a
	dh_installexamples -a
	dh_installmenu
	fpc-depends
	dh_installman -s
	dh_strip -s
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -s
	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
