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

# Don't load the system makefile.fpc
export FPCMAKE=
export FPCDIR=

# 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
NEWPP=$(PWD)/compiler/ppc386

export DH_COMPAT=2
#export DH_VERBOSE=1


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

clean:
	@echo "--- Cleaning"
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp install-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
	
	$(MAKE) compiler_clean
	$(MAKE) rtl_clean
	$(MAKE) utils_clean
	$(MAKE) fcl_clean
	$(MAKE) gtk_clean
	$(MAKE) api_clean
	$(MAKE) fv_clean
	$(MAKE) -C packages clean
	$(MAKE) -C docs clean

	dh_clean

	rm -f debian/control


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

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

	touch debian-files-stamp


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

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

# First make a new Compiler and RTL using a make cycle
	$(MAKE) compiler_cycle
	$(MAKE) utils_all PP=$(NEWPP)

	$(MAKE) fcl_all PP=$(NEWPP)
	$(MAKE) gtk_all PP=$(NEWPP)
	$(MAKE) api_all PP=$(NEWPP)
	$(MAKE) fv_all PP=$(NEWPP)
	$(MAKE) -C packages all PP=$(NEWPP) RELEASE=1

	touch build-arch-stamp


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

# Specify the compiler to use so installing will do correctly
	$(MAKE) compiler_install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr
	$(MAKE) rtl_install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr
	$(MAKE) utils_install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr

	$(MAKE) base_install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr
	$(MAKE) man_install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr/share
	$(MAKE) demo_install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr

	$(MAKE) fcl_install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr
	$(MAKE) gtk_install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr
	$(MAKE) api_install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr
	$(MAKE) fv_install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr
	$(MAKE) -C packages install PP=$(NEWPP) PREFIXINSTALLDIR=$(INSTALL_DIR)/usr

	touch install-arch-stamp

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

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

	$(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

	$(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_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 install-arch debian-files
	@echo "--- Building: arch packages"
	dh_testversion 2
	dh_testdir
	dh_testroot
	dh_installdocs -a
	dh_installchangelogs -a
	dh_undocumented -a
	dh_installexamples -a
	dh_movefiles -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
#	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a


binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary
