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

# Note: You must unset FPCDIR before using this Makefile

# Documentation type to use pdf/html
DOCTYPE=pdf

# 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
# Get utils
NEWPP=$(PWD)/compiler/ppc386
NEWPPUFILES=$(PWD)/utils/ppufiles
# Create default options
BUILDOPTS=PP=$(NEWPP)
INSTALLOPTS=PP=$(NEWPP) PPUFILES=$(NEWPPUFILES) PREFIXINSTALLDIR=$(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) fcl_distclean
	$(MAKE) api_distclean
	$(MAKE) utils_distclean
	$(MAKE) packages_distclean

	$(MAKE) -C docs clean
	rm -f docs/syntax.sty

	dh_clean

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


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

debian-files: debian-files-stamp
debian-files-stamp:
	@echo "--- Creating debian files"
	bash debian/fixdeb debian

	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) fcl_smart $(BUILDOPTS)
	$(MAKE) api_smart $(BUILDOPTS)
	$(MAKE) packages_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_install $(INSTALLOPTS)
	$(MAKE) rtl_install $(INSTALLOPTS)
	$(MAKE) fcl_install $(INSTALLOPTS)
	$(MAKE) api_install $(INSTALLOPTS)
	$(MAKE) utils_install $(INSTALLOPTS)
	$(MAKE) packages_install $(INSTALLOPTS)

	$(MAKE) fcl_exampleinstall $(INSTALLOPTS) DOCINSTALLDIR=$(DOC_DIR)/fp-units-fcl
	$(MAKE) api_exampleinstall $(INSTALLOPTS) DOCINSTALLDIR=$(DOC_DIR)/fp-units-api
	$(MAKE) -C packages pkggtk_exampleinstall $(INSTALLOPTS) DOCINSTALLDIR=$(DOC_DIR)/fp-units-gtk
	$(MAKE) -C packages pkggfx_exampleinstall $(INSTALLOPTS) DOCINSTALLDIR=$(DOC_DIR)/fp-units-gfx
	$(MAKE) -C packages pkgmisc_exampleinstall $(INSTALLOPTS) DOCINSTALLDIR=$(DOC_DIR)/fp-units-misc
	$(MAKE) -C packages pkgbase_exampleinstall $(INSTALLOPTS) DOCINSTALLDIR=$(DOC_DIR)/fp-units-base
	$(MAKE) -C packages pkgnet_exampleinstall $(INSTALLOPTS) DOCINSTALLDIR=$(DOC_DIR)/fp-units-net
	$(MAKE) -C packages pkgdb_exampleinstall $(INSTALLOPTS) DOCINSTALLDIR=$(DOC_DIR)/fp-units-db

	$(MAKE) man_install $(INSTALLOPTS) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr/share
	$(MAKE) demo_install $(INSTALLOPTS) DOCINSTALLDIR=$(INSTALL_DIR)/usr/share/doc/fp-compiler
	$(MAKE) doc_install $(INSTALLOPTS) DOCINSTALLDIR=$(INSTALL_DIR)/usr/share/doc/fp-compiler

# Create ppc386.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_testversion 2
	dh_testdir
	dh_testroot
	dh_movefiles -a

	touch arrange-arch-stamp


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

build-indep: build-indep-stamp
build-indep-stamp: debian-files-stamp
	@echo "--- Building Documentation"
	dh_testdir

# We need our own syntax.sty (v1.9) which is not in the current tetex and
# on ctan.org (PFV)
	cp debian/syntax.sty docs
	$(MAKE) -C docs $(DOCTYPE)

	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-indep binary-arch


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

binary-indep: build-indep install-indep debian-files
	@echo "--- Building: arch-indep packages"
	dh_testversion 2
	dh_testdir
	dh_testroot
	dh_installdocs -i
	dh_installchangelogs -i
	dh_installchangelogs -pfp-docs logs/Changes.docs
	dh_movefiles -i
	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_testversion 2
	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-utils logs/Changes.utils
	dh_installchangelogs -pfp-units-fcl logs/Changes.fcl
	dh_installchangelogs -pfp-units-api logs/Changes.api

	dh_undocumented -a
	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
