#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

# detect if build targets experimental suite (or is a draft)
DEB_SUITE_EXP = $(filter experimental% UNRELEASED,$(DEB_DISTRIBUTION))

# ABI by default follows upstream version (without repackaging suffix)
abi = $(firstword $(subst ~,$() ,$(DEB_UPSTREAM_VERSION)))
major = $(firstword $(subst .,$() ,$(DEB_UPSTREAM_VERSION)))

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifneq (,$(filter $(DEB_HOST_ARCH),ppc64el))
    export DEB_CFLAGS_MAINT_STRIP=-O3
    export DEB_CFLAGS_MAINT_PREPEND=-O2
endif

include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/autoreconf.mk

libname = libgs
pkg = ghostscript
pkg-x11 = ghostscript-x
pkg-doc = ghostscript-doc
pkg-lib = $(libname)$(major)
pkg-dev = $(libname)-dev
pkg-data = $(libname)$(major)-common

# TODO: enable when supporting system-shared libjpeg
# http://bugs.ghostscript.com/show_bug.cgi?id=696654
DEB_CONFIGURE_EXTRA_FLAGS += --without-pcl

# workaround for autotools-resolved paths ignored in base/unix-auth.mak
export DEB_HOST_MULTIARCH

# Support parallel building if enabled in DEB_BUILD_OPTIONS
# FIXME: Enable when not causing spurious errors like this
# https://buildd.debian.org/status/fetch.php?pkg=ghostscript&arch=amd64&ver=9.25~dfsg-6&stamp=1541195477&raw=0
#DEB_BUILD_PARALLEL = 1

# install library into multiarch paths
DEB_DH_INSTALL_ARGS_$(pkg-lib) = \
 usr/lib/$(libname).so.* usr/lib/$(DEB_HOST_MULTIARCH)
DEB_DH_INSTALL_ARGS_$(pkg-dev) = \
 usr/lib/$(libname).so usr/lib/$(DEB_HOST_MULTIARCH)

gs_opts += SHARE_JPEG=1

gs_opts += SHARE_LIBPNG=1

gs_opts += SHARE_LIBTIFF=1
DEB_CONFIGURE_EXTRA_FLAGS += --with-system-libtiff

gs_opts += SHARE_ZLIB=1

gs_opts += SHARE_JBIG2=1
DEB_CONFIGURE_EXTRA_FLAGS += --with-jbig2dec

gs_opts += SHARE_IJS=1
DEB_CONFIGURE_EXTRA_FLAGS += --with-ijs

gs_opts += SHARE_EXPAT=1

DEB_CONFIGURE_EXTRA_FLAGS += --with-x --disable-gtk

DEB_CONFIGURE_EXTRA_FLAGS += --enable-dynamic
DEB_CONFIGURE_EXTRA_FLAGS += --disable-compile-inits

DEB_CONFIGURE_EXTRA_FLAGS += --with-drivers=ALL

gs_opts += WHICH_CMS=lcms2 SHARE_LCMS=1 LCMS2DIR=/usr

# openjpeg requires patching for ICC and CMYK support
gs_opts += SHARE_JPX=1
#DEB_CONFIGURE_EXTRA_FLAGS += --disable-openjpeg

fontdirs = /var/lib/ghostscript/fonts /usr/share/cups/fonts \
 /usr/share/ghostscript/fonts /usr/local/lib/ghostscript/fonts \
 /usr/share/fonts
gs_opts += SHARE_FT=1
DEB_CONFIGURE_EXTRA_FLAGS += --enable-freetype \
 --with-fontpath=$(subst $() ,:,$(strip $(fontdirs)))

# avoid building CUPS driver when bootstrapping an architecture
ifeq (,$(findstring stage1,$(DEB_BUILD_OPTIONS)))
gs_opts += SHARE_LCUPS=1 SHARE_LCUPSI=1
DEB_CONFIGURE_EXTRA_FLAGS += --enable-cups
endif

DEB_MAKE_BUILD_TARGET = so $(gs_opts)
DEB_MAKE_INSTALL_TARGET = soinstall DESTDIR=$(DEB_DESTDIR) $(gs_opts)

DEB_DH_MAKESHLIBS_ARGS_$(pkg-x11) = -n

# relax symbols check when targeting experimental
# and when bootstrapping an architecture
STAGE1 = $(findstring stage1,$(DEB_BUILD_PROFILES))
export DPKG_GENSYMBOLS_CHECK_LEVEL=$(if $(DEB_SUITE_EXP)$(STAGE1),0,1)

DEB_COMPRESS_EXCLUDE_ALL = .pdf

# install ABI-specific subdir explicitly, to fail if resolved wrong
DEB_DH_INSTALL_ARGS_$(pkg-data) = usr/share/ghostscript/$(abi)/

cleanbuilddir::
	[ ! -f Makefile ] || $(MAKE) soclean
	[ ! -f Makefile ] || $(MAKE) distclean

# Remove some of the scripts
DEB_DH_INSTALL_ARGS_ALL += $(patsubst %,-Xbin/%,\
 unix-lpr.sh lprsetup.sh pv.sh fixmswrd.pl)

# Do not include the Ghostscript loader executable with GTK support
install/$(pkg)::
DEB_DH_INSTALL_ARGS_ALL += -Xbin/gsx

# Rename /usr/bin/gsc, to not conflict with gambc
install/$(pkg)::
	mv $(DEB_DESTDIR)/usr/bin/gsc $(DEB_DESTDIR)/usr/bin/gs
	mkdir -p $(DEB_DESTDIR)/usr/sbin/
	install -m 755 debian/update-gsfontmap $(DEB_DESTDIR)/usr/sbin

# Move ICC profiles to shared dir, to allow reuse by others.
install/$(pkg-data)::
	mkdir -p $(DEB_DESTDIR)/usr/share/color/icc
	mv $(DEB_DESTDIR)/usr/share/ghostscript/$(abi)/iccprofiles \
		$(DEB_DESTDIR)/usr/share/color/icc/ghostscript
DEB_DH_LINK_$(pkg-data) += \
 /usr/share/color/icc/ghostscript usr/share/ghostscript/$(abi)/iccprofiles

# Strip CMap files (separately packaged in poppler-data)
DEB_DH_INSTALL_ARGS_$(pkg-data) += -X/Resource/CMap
DEB_DH_LINK_$(pkg-data) += \
 var/lib/ghostscript/CMap usr/share/ghostscript/$(abi)/Resource/CMap

# Recommend fonts-droid (not ship DroidSansFallback.ttf)
DEB_DH_INSTALL_ARGS_ALL += -XDroidSansFallback.ttf
DEB_DH_LINK_$(pkg-data) += \
 /usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf \
 /usr/share/ghostscript/$(abi)/Resource/CIDFSubst/DroidSansFallback.ttf

binary-post-install/$(pkg-data)::
	file='debian/$(cdbs_curpkg)/usr/share/ghostscript/$(abi)/Resource/Init/cidfmap'; \
	  ! egrep -v '^(%([^%].*)?)?$$' "$$file" && rm "$$file" || ( \
		echo; \
		echo 'ERROR: cidfmap not virtually empty as expected,'; \
		echo '       so some alternative to just dropping that file is required.'; \
		echo '       More info at bug#531182.'; \
		exit 1 )
	rename 's/$$/.t1/' \
		debian/$(cdbs_curpkg)/usr/share/ghostscript/$(abi)/Resource/Font/*
	dh_linktree -p$(cdbs_curpkg) -- \
		deduplicate usr/share/fonts/type1/urw-base35 \
		usr/share/ghostscript/$(abi)/Resource/Font
	rename 's/\.t1$$//' \
		debian/$(cdbs_curpkg)/usr/share/ghostscript/$(abi)/Resource/Font/*

# check that tracked issues are solved
debian/stamp-makefile-check: export GS = LD_LIBRARY_PATH=sobin GS_LIB=Resource/Init sobin/gsc
debian/stamp-makefile-check:
		$(MAKE) --file debian/tests/smoke.mk
		$(MAKE) --file debian/tests/issues/bug_700317/check.mk
