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

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

# Enable verbose builds to detect missing (hardening) flags.
#export verbose=1

# This has to be exported to make some magic below work.
export DH_OPTIONS
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk

# Set CC for crossbuild
export CC_FOR_BUILD ?= cc
export PKG_CONFIG ?= $(DEB_HOST_GNU_TYPE)-pkg-config

# The build system doesn't use CPPFLAGS, pass them to CFLAGS to enable the
# missing (hardening) flags.
CFLAGS += $(CPPFLAGS)
# ICC support needs patched thirdparty liblcms2-art
CFLAGS += -DFZ_ENABLE_ICC=0
CFLAGS += -fPIC

BUILD_FLAGS = build=debian OS=Linux XCFLAGS="$(CFLAGS)" XLDFLAGS="$(LDFLAGS)" prefix=/usr
# Force using system library
BUILD_FLAGS += USE_SYSTEM_MUJS=yes
BUILD_FLAGS += USE_SYSTEM_LIBS=yes
# Force using system lcms2 (was not included in default system library set)
BUILD_FLAGS += USE_SYSTEM_LCMS2=yes
BUILD_FLAGS += LD=$(DEB_HOST_GNU_TYPE)-ld


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

ifeq (,$(filter terse,$(DEB_BUILD_OPTIONS)))
	BUILD_FLAGS += verbose=yes
endif

ifeq ($(DEB_TARGET_ARCH),mips64el)
	BUILD_FLAGS += HAVE_OBJCOPY=no
endif

override_dh_auto_build:
	-mkdir source/pdf/cmaps
	sh scripts/runcmapdump.sh
	dh_auto_build -- $(BUILD_FLAGS)

override_dh_auto_install:
	dh_auto_install -- $(BUILD_FLAGS)
	install -m744 -T $(CURDIR)/debian/mupdf.sh $(CURDIR)/debian/tmp/usr/bin/mupdf
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	make -C $(CURDIR)/debian/tests test
endif

override_dh_auto_clean:
	-rm -rf build
	-rm -rf generated
	-rm -rf source/pdf/cmaps

%:
	dh $@
