#!/usr/bin/make -f
# debian/rules for Free Pascal

# Documentation type to use pdf/html
DOCTYPE=pdf

# Detect name of new compiler
CPU_SOURCE=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
CPU_TARGET=$(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq ($(CPU_TARGET),m68k)
PPSUF=68k
endif
ifeq ($(CPU_TARGET),i386)
PPSUF=386
endif
ifeq ($(CPU_TARGET),x86_64)
PPSUF=x64
endif
ifeq ($(CPU_TARGET),powerpc)
PPSUF=ppc
endif
ifeq ($(CPU_TARGET),alpha)
PPSUF=axp
endif
ifeq ($(CPU_TARGET),arm)
PPSUF=arm
endif

ifneq ($(CPU_SOURCE),$(CPU_TARGET))
PPPRE=ppcross
else
PPPRE=ppc
endif

PPNEW=$(PPPRE)$(PPSUF)

# Reset FPC and FPCDIR if it was set
FPC=
FPCDIR=
# Get Package version and FPC version out of changelog file
PACKAGEVERSION:=$(shell head -n 1 debian/changelog | awk '{ print $$2 }' | tr -d '[()]')
FPCVERSION:=$(shell echo $(PACKAGEVERSION) | awk -F '-' '{ print $$1 }')
# Get directories	
PWD:=$(shell pwd)
BUILD_DIR=$(PWD)/debian/build
INSTALL_DIR=$(PWD)/debian/tmp
DOC_DIR=$(INSTALL_DIR)/usr/share/doc
EXAMPLE_TEMP=$(DOC_DIR)/fpc-$(FPCVERSION)
# Get utils
NEWPP=$(PWD)/compiler/$(PPNEW)
NEWFPDOC=$(PWD)/utils/fpdoc/fpdoc
# Create default options
BUILDOPTS=PP=$(NEWPP)
INSTALLOPTS=PP=$(NEWPP) INSTALL_PREFIX=$(INSTALL_DIR)/usr

export DH_COMPAT=2
# export DH_VERBOSE=1

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

clean:
	@echo "--- Cleaning"
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp install-arch-stamp arrange-arch-stamp
	rm -f build-indep-stamp install-indep-stamp

	rm -f debian-files-stamp
	rm -f debian/*.files debian/*.docs debian/*.examples debian/*.postinst debian/*.postrm

	$(MAKE) compiler_distclean
	$(MAKE) rtl_distclean
	$(MAKE) packages_distclean
	$(MAKE) fcl_distclean
	$(MAKE) utils_distclean

	$(MAKE) -C docs clean

	-(find . -name \*.a | xargs rm)

	dh_clean

# Cannot do that because the control-file is often needed after a cleanup
#	rm -f debian/control


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

debian-files: debian-files-stamp
debian-files-stamp:
	@echo "--- Creating debian files"
	bash debian/fixdeb debian $(PPNEW)

	touch debian-files-stamp


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

build-arch: build-arch-stamp
build-arch-stamp: debian-files-stamp
	@echo "--- Building"
	dh_testdir

# First make a new Compiler and RTL using a make cycle
	$(MAKE) compiler_cycle
	$(MAKE) rtl_clean rtl_smart $(BUILDOPTS)
	$(MAKE) packages_base_smart $(BUILDOPTS)
	$(MAKE) fcl_smart $(BUILDOPTS)
	$(MAKE) packages_extra_smart $(BUILDOPTS)
	$(MAKE) utils_all $(BUILDOPTS)

	touch build-arch-stamp


install-arch: install-arch-stamp
install-arch-stamp: build-arch-stamp debian-files-stamp
	@echo "--- Installing"
	dh_testdir
	dh_testroot
	dh_clean -k

# Specify the compiler to use so installing will use the correct versioned dir
	$(MAKE) compiler_distinstall $(INSTALLOPTS)
	$(MAKE) rtl_distinstall $(INSTALLOPTS)
	$(MAKE) fcl_distinstall $(INSTALLOPTS)
	$(MAKE) packages_distinstall $(INSTALLOPTS)
	$(MAKE) utils_distinstall $(INSTALLOPTS)

# Copy examples to the correct doc dir
	install -d -m755 $(DOC_DIR)/fp-units-fcl/examples
	install -d -m755 $(DOC_DIR)/fp-units-gtk/examples
	install -d -m755 $(DOC_DIR)/fp-units-gnome1/examples
	install -d -m755 $(DOC_DIR)/fp-units-gfx/examples
	install -d -m755 $(DOC_DIR)/fp-units-db/examples
	install -d -m755 $(DOC_DIR)/fp-units-net/examples
	install -d -m755 $(DOC_DIR)/fp-units-misc/examples
	install -d -m755 $(DOC_DIR)/fp-units-base/examples

	bash debian/moveexamples $(EXAMPLE_TEMP) $(DOC_DIR)

# Install man pages and RTL demos and whatsnew and readme
	$(MAKE) man_install $(INSTALLOPTS) INSTALL_PREFIX=$(INSTALL_DIR)/usr/share
	$(MAKE) demo_install $(INSTALLOPTS) INSTALL_SOURCEDIR=$(DOC_DIR)/fp-compiler
	$(MAKE) doc_install $(INSTALLOPTS) INSTALL_DOCDIR=$(DOC_DIR)/fp-compiler

# Create fpc.cfg which is included as conffile
	chmod 755 compiler/utils/samplecfg 
	compiler/utils/samplecfg "/usr/lib/fpc/\$$version" $(INSTALL_DIR)/etc
	touch install-arch-stamp


arrange-arch: arrange-arch-stamp
arrange-arch-stamp: install-arch-stamp debian-files-stamp
	dh_testdir
	dh_testroot
	dh_install -a --sourcedir=debian/tmp --list-missing

	touch arrange-arch-stamp


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

build-indep: build-indep-stamp
# We depend on the new fpdoc
build-indep-stamp: debian-files-stamp build-arch-stamp
	@echo "--- Building Documentation"
	dh_testdir

	$(MAKE) -C docs $(DOCTYPE) FPDOC=$(NEWFPDOC) 

	touch build-indep-stamp


install-indep: install-indep-stamp
install-indep-stamp: debian-files build-indep
	@echo "--- Installing Documentation"
	dh_testdir
	dh_testroot
	dh_clean -k

	$(MAKE) -C docs $(DOCTYPE)install DOCINSTALLDIR=$(INSTALL_DIR)/usr/share/doc/fpc/$(FPCVERSION)/

	touch install-indep-stamp


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

build: build-arch build-indep

install: install-arch install-indep

binary: binary-arch binary-indep 


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

binary-indep: build-indep install-indep debian-files
	@echo "--- Building: arch-indep packages"
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i
	dh_installchangelogs -pfp-docs logs/Changes.docs
	dh_install -i --sourcedir=debian/tmp --list-missing
	dh_compress -i -X.pdf
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build-arch arrange-arch debian-files
	@echo "--- Building: arch packages"
	dh_testdir
	dh_testroot
	dh_installdocs -a
	dh_installchangelogs -a

	dh_installchangelogs -pfp-compiler logs/Changes.compiler
	dh_installchangelogs -pfp-units-rtl logs/Changes.rtl
	dh_installchangelogs -pfp-units-fcl logs/Changes.fcl
	dh_installchangelogs -pfp-utils logs/Changes.utils

	dh_installexamples -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a


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