#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright 2004-2009, Masayuki Hatta (mhatta) <mhatta@debian.org>
# Copyright 2009-2019, Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for GhostPDL
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

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)))

# These are needed ahead of including CDBS
DEB_AUTO_UPDATE_AUTOCONF = 2.67

include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/utils.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

# put aside upstream-shipped temp files during build
DEB_UPSTREAM_CRUFT_MOVE = configure

# 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

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

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
binary-post-install/$(pkg-lib):: debian/stamp-local-shlibs-$(libname)
debian/stamp-local-shlibs-$(libname): binary-install/$(pkg-lib)
	d-shlibmove --commit \
		--devunversioned --ignorelibdep --multiarch \
		--exclude-a \
		--movedev "debian/tmp/usr/include/*" usr/include/ \
		debian/tmp/usr/lib/$(libname).so

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 )

# 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
