#!/usr/bin/make -f
BUILD_ARCH = $(shell dpkg --print-installation-architecture)

#CFLAGS= -g
#ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
#     CFLAGS += -O0
#        else
#     CFLAGS += -O2
#endif

CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

MAKE:=$(MAKE) PLATFORM=linux
PREFIX=/usr
BINARYVERSION=6

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp:
	dh_testdir
	$(MAKE) PREFIX=$(PREFIX) C_COMPILER_OPTIMIZATION_OPTIONS="$(CFLAGS) $(CPPFLAGS)" VARDIR=/var/lib/
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	$(MAKE) clean
	$(MAKE) distclean
	rm -f c_defaults.h chicken_defaults.h lib*.so chicken.info-*		
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
# The directories here have to be prefixed by $(CURDIR)/debian/tmp
# because that is the location where the binary package files are
# placed by dpkg-buildpackage. If absolute files are specified,
# i.e. /var/lib/chicken, then those files will be installed on the
# build system, but will not end up in the package.
	$(MAKE) PREFIX="$(CURDIR)/debian/tmp/usr" LINKER_LINK_SHARED_DLOADABLE_OPTIONS='-L. -shared $(LDFLAGS),-R"."' LINKER_LINK_SHARED_PROGRAM_OPTIONS='$(LDFLAGS),-R"."' LIBCHICKEN_SO_LINKER_OPTIONS='$(LDFLAGS),-soname,libchicken.so.6' VARDIR="$(CURDIR)/debian/tmp/var/lib" install
	chrpath -d $(CURDIR)/debian/tmp/usr/bin/*
	chrpath -d $(CURDIR)/debian/tmp/var/lib/chicken/6/*.so

binary-common:
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman
# The asterisk in chicken.info* is necessary, because Debian makeinfo
# is very different from the standard makeinfo, and it is incredibly
# difficult to convince it to produce standalone Texinfo files. 
#	dh_installinfo chicken.info*
	dh_installchangelogs
	dh_install --sourcedir=debian/tmp
	dh_link
ifeq "$(findstring nostrip,$(DEB_BUILD_OPTIONS))" ""
	dh_strip
endif	
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps -l debian/libchicken6/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep: build install

binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

binary-%: build install
	make -f debian/rules binary-common DH_OPTIONS=-p$*

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