#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2004-2009, Masayuki Hatta (mhatta) <mhatta@debian.org>
# Copyright © 2009-2017, 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/>.

# These are needed ahead of including CDBS
# ABI by default follows upstream version (without repackaging suffix)
abi = $(firstword $(subst ~,$(space),$(DEB_UPSTREAM_VERSION)))
major = $(firstword $(subst .,$(space),$(DEB_UPSTREAM_VERSION)))
SEDRULE_CONTENT = 's/__ABI__/$(abi)/g;s/__VER__/$(major)/g'
DEB_PHONY_RULES += debian/control.in
debian/control:: debian/control.in
debian/control.in::
	sed $(SEDRULE_CONTENT) <debian/control.in.in >debian/control.in
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

# suppress checking license during build
DEB_COPYRIGHT_CHECK_LICENSECHECK =
CDBS_BUILD_DEPENDS_rules_utils_copyright-check =

# put aside upstream-shipped temp files during build but after copyright-check
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
DEB_BUILD_PARALLEL = 1

gs_opts += SHARE_JPEG=1
dev-deps += jpeg

gs_opts += SHARE_LIBPNG=1
dev-deps += png

gs_opts += SHARE_TRIO=1
dev-deps += trio

gs_opts += SHARE_LIBTIFF=1
DEB_CONFIGURE_EXTRA_FLAGS += --with-system-libtiff
dev-deps += tiff

gs_opts += SHARE_ZLIB=1
dev-deps += z

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

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

gs_opts += SHARE_EXPAT=1
dev-deps += expat

DEB_CONFIGURE_EXTRA_FLAGS += --with-x --disable-gtk
dev-deps += xt xext x11 ice sm

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
dev-deps += lcms2

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

# In contrary to Debian we use the openjpeg convenience library, as the Ubuntu
# security team does not accept the upstream openjpeg library into Ubuntu
# Main (https://bugs.launchpad.net/bugs/711061)
gs_opts += SHARE_JPX=0
DEB_CONFIGURE_EXTRA_FLAGS += --enable-openjpeg --without-jasper

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 $(space),:,$(strip $(fontdirs)))
dev-deps += freetype6 fontconfig1

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

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
EXP_RELEASE = $(filter experimental% UNRELEASED,\
 $(shell dpkg-parsechangelog | grep -Po '^Distribution: \K.*'))
STAGE1 = $(findstring stage1,$(DEB_BUILD_PROFILES))
export DPKG_GENSYMBOLS_CHECK_LEVEL=$(if $(EXP_RELEASE)$(STAGE1),0,1)

DEB_COMPRESS_EXCLUDE_ALL = .pdf

pre-build:: debian/control.in.in
	echo "sed $(SEDRULE_CONTENT) <$< | diff -u debian/control.in -"
	@sed $(SEDRULE_CONTENT) <$< | diff -u debian/control.in - || ( \
		echo; \
		echo "Upstream version has changed (or debian/control.in.in is out of sync)"; \
		echo "Edit debian/control manually, or use the following command:"; \
		echo "  DEB_AUTO_UPDATE_DEBIAN_CONTROL=yes fakeroot debian/rules clean"; \
		exit 1)

# Generate (and cleanup) files containing variables static per build
infiles = $(filter-out debian/control.in debian/control.in.in, $(wildcard debian/*.in))
outfiles = $(subst $(libname)__VER__,$(pkg-lib),$(basename $(infiles)))
pre-build:: $(outfiles)
$(outfiles): update-config debian/stamp-copyright-check
	sed $(SEDRULE_CONTENT) <$(subst $(pkg-lib),$(libname)__VER__,$@).in >$@
clean::
	rm -f $(outfiles)

clean::
	[ ! -f Makefile ] || $(MAKE) soclean
	[ ! -f Makefile ] || $(MAKE) distclean
# These are somehow left...
	rm -rf sobin soobj

# Remove some of the scripts
bin-avoid = unix-lpr.sh lprsetup.sh pv.sh fixmswrd.pl
install/$(pkg)::
	rm -f $(patsubst %,$(DEB_DESTDIR)/usr/bin/%,$(bin-avoid))

# Do not include the Ghostscript loader executable with GTK support
install/$(pkg)::
	rm -f $(DEB_DESTDIR)/usr/bin/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

# Strip CMap files (separately packaged in poppler-data)
install/$(pkg-data)::
	rm -rf $(DEB_DESTDIR)/usr/share/ghostscript/$(abi)/Resource/CMap

# Recommend fonts-droid (not ship DroidSansFallback.ttf)
CDBS_RECOMMENDS_$(pkg-data) +=, fonts-droid-fallback
install/$(pkg-data)::
	rm -f $(DEB_DESTDIR)/usr/share/ghostscript/$(abi)/Resource/CIDFSubst/DroidSansFallback.ttf

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
CDBS_BUILD_DEPENDS +=, d-shlibs
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
clean::
	rm -f debian/stamp-local-shlibs-$(libname)

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 )

# Needed at build time
dev-deps += paper idn11
CDBS_BUILD_DEPENDS +=, $(patsubst %,$(comma) lib%-dev,$(dev-deps))
CDBS_BUILD_DEPENDS +=, freeglut3-dev, pkg-config

# Needed (always, often, sometimes) at runtime
CDBS_DEPENDS_$(pkg) +=, debconf | debconf-2.0
CDBS_DEPENDS_$(pkg) +=, $(pkg-lib) (= $(DEB_VERSION))
CDBS_DEPENDS_$(pkg-x11) +=, $(pkg) (= $(DEB_VERSION))
CDBS_DEPENDS_$(pkg-lib) +=, poppler-data
CDBS_DEPENDS_$(pkg-dev) +=, $(pkg-lib) (= $(DEB_VERSION))
CDBS_RECOMMENDS_$(pkg) +=, gsfonts
CDBS_SUGGESTS_$(pkg) +=, $(pkg-x11)
CDBS_SUGGESTS_$(pkg-doc) +=, $(pkg)

# Public virtual packages
CDBS_PROVIDES_$(pkg) +=, postscript-viewer
