#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# $Id$
#

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# Define package name variable for a one-stop change.
PACKAGE_NAME = PBPKG

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# Build both architecture dependent and independent
build: build-arch 

# Build architecture dependent
build-arch: build-arch-stamp

build-arch-stamp: 
	dh_testdir
	#for PLAT in amd64 aarch64; do for AFILE in gcc cc; do [ ! -e RULES/$(PLAT)-linux-$(AFILE).rul ] && ln -s i586-linux-$(AFILE).rul RULES/$(PLAT)-linux-$(AFILE).rul; done; done

	# Compile the package.
	make COPTX=-DTRY_EXT2_FS GMAKE_NOWARN=true MANDIR=man CFLAGS="-O2 -g -pipe -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -mtune=generic -fasynchronous-unwind-tables"
	touch build-stamp

# Clean up
clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp #CONFIGURE-STAMP#
	# Clean temporary document directory
	rm -rf debian/doc-temp
	# Clean up.
	-$(MAKE) distclean
	rm -f config.log
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif

	dh_clean

# Install architecture dependent and independent
install: install-arch

# Install architecture dependent
install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_clean -k -s
	dh_installdirs -s

	# Install the package files into build directory:
	dh_install -s
	make GMAKE_NOWARN=true "INS_BASE=$(CURDIR)/debian/$(PACKAGE_NAME)/usr" "INS_RBASE=/$(CURDIR)/debian/$(PACKAGE_NAME)" MANDIR=man install
	rm -rf $(CURDIR)/debian/$(PACKAGE_NAME)

	#mkdir -p $(CURDIR)/debian/$(PACKAGE_NAME)/usr/bin
	#mkdir -p $(CURDIR)/debian/$(PACKAGE_NAME)/usr/share/man/man1

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	#dh_installchangelogs changelog
	dh_installdocs
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

# Build architecture depdendent and independent packages
binary: binary-arch
.PHONY: clean binary

