#!/usr/bin/make -f

DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)
PERL ?= /usr/bin/perl

# there are some alignment problems on sparc, fixed in perl 5.10.1
# we work around them by using -O1, but only on sparc and only for perls before 5.10.1
PERL_VER=$(shell $(PERL) -e 'my ($$rev, $$ver, $$subver) = $$] =~ /(\d+)\.(\d\d\d)(\d\d\d)/; print join(".", $$rev+0, $$ver+0, $$subver+0)')
GOOD_PERL=$(shell if dpkg --compare-versions $(PERL_VER) -gt 5.10.1~; then echo "yes"; fi)

CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O0
else
    ifeq (sparc,$(DEB_HOST_ARCH))
        ifeq (yes,$(GOOD_PERL))
            $(info Building on sparc, with Perl $(PERL_VER), use -O2)
            CFLAGS+=-O2
        else
            $(info Building on sparc, with Perl $(PERL_VER), use -O1)
            CFLAGS+=-O1
        endif
    else
        $(info Not building on sparc (Perl $(PERL_VER)), use -O2)
        CFLAGS+=-O2
    endif
endif

override_dh_auto_configure:
	dh_auto_configure -- OPTIMIZE="$(CFLAGS)"

# end of sparc alignment workaround

%:
	dh $@
