#!/usr/bin/make -f

export DH_VERBOSE=1

CFLAGS += -Wall -g

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

ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH),m68k))
	CFLAGS = -Wall -g -O0
endif

ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH),alpha))
       ARCH_CONF = --with-stack-direction=-1
endif

ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH),ia64))
       ARCH_CONF = --with-stack-direction=-1
endif

ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH),mips))
       ARCH_CONF = --with-stack-direction=-1
endif

ifeq ($(DEB_HOST_ARCH),$(findstring $(DEB_HOST_ARCH),mipsel))
       ARCH_CONF = --with-stack-direction=-1
endif

DEB_CONFIGURE_USER_FLAGS += --with-readline $(ARCH_CONF)

dev_extras = debian/tmp -name install-aliases -o -name rep_config.h -o -name *.a -o -name *.jl

rep_extras = debian/tmp/usr/lib/rep debian/tmp/usr/share/rep -name *.jlc -o -name *.so

dh_install_auto = xargs dh_install --autodest

sm := $(shell grep '^Package: librep[0-9]' debian/control | sed -e 's@^Package: librep\([0-9][0-9]*\).*@\1@' )

%:
	dh $@ --with autotools-dev --with autoreconf

override_dh_auto_clean:
	-$(MAKE) debclean

override_dh_auto_configure:
	env CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" dh_auto_configure -- $(DEB_CONFIGURE_USER_FLAGS)

override_dh_auto_build:
	env LC_ALL=C dh_auto_build

override_dh_installdocs:
	dh_installdocs --link-doc=librep$(sm)

override_dh_installchangelogs:
	dh_installchangelogs -plibrep$(sm) NEWS

override_dh_auto_install:
	dh_auto_install
	dh_install
	find $(dev_extras) | $(dh_install_auto) -plibrep-dev
	find $(rep_extras) | $(dh_install_auto) -prep

override_dh_strip:
	dh_strip -a --dbg-package=librep-dbg

override_dh_makeshlibs:
	dh_makeshlibs -Xusr/lib/rep/

override_dh_shlibdeps:
	dh_shlibdeps -- --warnings=2
