#!/usr/bin/make -f
# -*- makefile -*-

export SHELL=/bin/bash

# Build with gcc-4.9 on Stretch, Buster,Xenial, Yakkety, Zesty, and Horizon.
distrelease := $(shell lsb_release -cs)
ifeq ($(distrelease),$(filter $(distrelease),stretch buster xenial yakkety zesty artful bionic Horizon))
export CC=gcc-4.9
export CXX=g++-4.9
export CPP=cpp-4.9
export LD=gcc-4.9
endif

DEB_BUILD_ARCH = $(shell dpkg-architecture -qDEB_BUILD_ARCH)
OPTI_FLAGS_LIBONLY := -fomit-frame-pointer
### PICCORO optimizer flags for i386 x86 machines
ifeq ($(DEB_HOST_ARCH),i386)
    OPTI_FLAGS_LIBONLY += -march=i686 -mtune=pentiumpro -msse2 
else
ifeq ($(DEB_HOST_ARCH),i486)
    OPTI_FLAGS_LIBONLY += -march=i686 -mtune=pentiumpro -msse2 
endif
endif

ifeq ($(DEB_HOST_ARCH),amd64)
    OPTI_FLAGS_LIBONLY += -msse2
endif

CFLAGS= -O3 -Wl,--no-keep-memory,--reduce-memory-overhead $(OPTI_FLAGS_LIBONLY)
export CFLAGS= -O3 -Wl,--no-keep-memory,--reduce-memory-overhead $(OPTI_FLAGS_LIBONLY)
export CXXFLAGS= -O3 -Wl,--no-keep-memory,--reduce-memory-overhead $(OPTI_FLAGS_LIBONLY)

%:
	dh $@ --parallel

override_dh_auto_clean:
	rm -f mozconfig
	dh_auto_clean

# Use a custom mozconfig for Sid to build with one thread.
# Builds on Debian derivatives may have to add their own distrelease codename.
marchbuild := $(shell dpkg-architecture -qDEB_BUILD_ARCH)
override_dh_auto_configure:
	

ifeq ($(marchbuild),$(filter $(marchbuild),i386))
override_dh_auto_build:
	cp debian/mozconfig-i386 mozconfig
	make -f client.mk build
else
ifeq ($(marchbuild),$(filter $(marchbuild),amd64))
override_dh_auto_build:
	cp debian/mozconfig-amd64 mozconfig
	make -f client.mk build
else
override_dh_auto_build:
	cp debian/mozconfig mozconfig
	make -f client.mk build
endif
endif

override_dh_auto_install:
	make -f client.mk DESTDIR=$$(pwd)/debian/palemoon prefix=/usr \
        installdir=/usr/lib/palemoon \
        sdkdir=/usr/lib/palemoon-devel install
	rm -rf $$(pwd)/debian/palemoon/usr/share/idl
	rm -rf $$(pwd)/debian/palemoon/usr/lib/palemoon-devel
	rm -rf $$(pwd)/debian/palemoon/usr/include
# remove vestigial duplicate file
	rm -rf $$(pwd)/debian/palemoon/usr/lib/palemoon/palemoon-bin

override_dh_shlibdeps:
	dh_shlibdeps -l /usr/lib/palemoon

# For releases that don't use xz compression by default, such as Wheezy.
# It's OK to leave this in for those that do.
override_dh_builddeb:
	dh_builddeb -- -Z xz
