#!/usr/bin/make -f

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

VERSION_NUMBER := $(shell grep '^VERSION_NUMBER=' < version.sh | sed s/^VERSION_NUMBER=//)

# check if we need to use DEBUG:
DEBUG_FLAGS:=$(if $(findstring debug,$(DEB_BUILD_OPTIONS)),--with-debug,)

#standard C flags:
CFLAGS:=-falign-functions=4
export CFLAGS

# change the CFLAGS depending on the architecture
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Use --enable-portability on most archs except the best-supported ones (x86, ppc64*)
ifeq (,$(filter amd64 i386 kfreebsd-amd64 kfreebsd-i386 hurd-i386 ppc64 ppc64el,$(DEB_HOST_ARCH)))
PORTABILITY_FLAG := --enable-portability
endif

ifneq (,$(filter armhf ,$(DEB_HOST_ARCH)))
	 CFLAGS:=-marm ${CFLAGS}
endif

MODULES := --with-module=gdbm --with-module=berkeley-db --with-module=pcre --with-module=rawsock --with-module=clx/new-clx

ifneq (,$(filter alpha amd64 armel armhf i386 powerpc kfreebsd-i386 kfreebsd-amd64 hurd-i386 ppc64 ppc64el mips64el arm64,$(DEB_HOST_ARCH)))
	# Enable FFI and extra modules
        FFI:=--with-dynamic-ffi --with-dynamic-modules
        MODULES += --with-module=bindings/glibc --with-module=postgresql --with-module=zlib --with-module=dbus
else
	# Disable FFI
        FFI:=--without-dynamic-ffi
endif

%:
	dh $@

override_dh_auto_configure:
	# on systems without FFI we cannot make multiple packages
	./configure debian/build ${PORTABILITY_FLAG} ${DEBUG_FLAGS} --prefix=/usr --fsstnd=debian ${FFI} CFLAGS="${CFLAGS}" ${MODULES} || { cat debian/build/config.log; touch foo.h; cpp -dM foo.h ; exit 1; }

	# remove the -O2 from the Makefile on mipsel
ifeq ($(DEB_HOST_ARCH),mipsel)
	sed 's/ -O2 / /' < debian/build/Makefile > debian/build/Makefile-new && \
		mv debian/build/Makefile debian/build/Makefile-old && \
		mv debian/build/Makefile-new debian/build/Makefile
endif # filter mipsel ,$(DEB_HOST_ARCH) remove the -O2 from the makefile

override_dh_auto_build:
	# build the package
	$(MAKE) -C debian/build

override_dh_auto_clean:
	rm -rf debian/build 

override_dh_auto_install:
	# install the package
	$(MAKE) -C debian/build DESTDIR=$(shell pwd)/debian/tmp install
	mkdir -p debian/tmp/usr/share/doc debian/tmp/usr/share/man debian/tmp/usr/sbin debian/tmp/usr/bin

	# remove unwanted upstream doc/ files
	rm debian/tmp/usr/share/doc/clisp/doc/clisp.1 \
		debian/tmp/usr/share/doc/clisp/doc/clisp-link.1 \
		debian/tmp/usr/share/doc/clisp/COPYRIGHT \
		debian/tmp/usr/share/doc/clisp/GNU-GPL || true

	# Install CLC files
	install -o root -g root -m 0644 debian/install-clc.lisp \
		debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)

override_dh_install:
	dh_install --sourcedir=debian/tmp

	# remove docs from the clx package
	if [ -d debian/clisp-module-clx/usr/lib/clisp-$(VERSION_NUMBER)/clx/new-clx/demos ] ; then rm -rf debian/clisp-module-clx/usr/lib/clisp-$(VERSION_NUMBER)/clx/new-clx/demos ; fi 

	# handle full image if  it exists, normally we do not build it
	if [ -d debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full ] ; then mkdir -p debian/clisp-dev/usr/lib/clisp-$(VERSION_NUMBER)/full/ ; fi
	if [ -d debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full ] ; then mkdir -p debian/clisp/usr/lib/clisp-$(VERSION_NUMBER)/full/ ; fi
	if [ -d debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full ] ; then install --owner=root --group=root --mode=0644  debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full/*.a debian/clisp-dev/usr/lib/clisp-$(VERSION_NUMBER)/full/ ; fi
	if [ -d debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full ] ; then install --owner=root --group=root --mode=0644  debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full/*.h debian/clisp-dev/usr/lib/clisp-$(VERSION_NUMBER)/full/ ; fi
	if [ -d debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full ] ; then install --owner=root --group=root --mode=0644  debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full/makevars debian/clisp-dev/usr/lib/clisp-$(VERSION_NUMBER)/full/ ; fi
	if [ -d debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full ] ; then install --owner=root --group=root --mode=0644  debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full/lispinit.mem debian/clisp/usr/lib/clisp-$(VERSION_NUMBER)/full/ ; fi
	if [ -d debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full ] ; then install --owner=root --group=root --mode=0755  debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full/lisp.run debian/clisp/usr/lib/clisp-$(VERSION_NUMBER)/full/ ; fi
	if [ -d debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full ] ; then install --owner=root --group=root --mode=0644  debian/tmp/usr/lib/clisp-$(VERSION_NUMBER)/full/*.o debian/clisp/usr/lib/clisp-$(VERSION_NUMBER)/full/ ; fi

override_dh_installchangelogs:
	dh_installchangelogs src/ChangeLog

override_dh_gencontrol-arch:
	# add FAS file format version to substvars
	debian/clisp/usr/bin/clisp -Efile utf-8 -B debian/clisp/usr/lib/clisp-$(VERSION_NUMBER) -M debian/clisp/usr/lib/clisp-$(VERSION_NUMBER)/base/lispinit.mem debian/fasl-version.lisp
	dh_gencontrol -a
