#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2004-2009, Masayuki Hatta (mhatta) <mhatta@debian.org>
# Copyright © 2009-2011, Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for GPL Ghostscript
#
# 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 2, 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/>.

# Use xz compression:
DEB_DH_BUILDDEB_ARGS ?= -u-Zxz

# This needs to run before cdbs auto-update
debian/control:: debian/control.in
DEB_PHONY_RULES += 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/rules/upstream-tarball.mk
include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/utils.mk

# ABI by default follows upstream version (without repackaging suffix)
abi = $(DEB_UPSTREAM_TARBALL_VERSION)
major := $(shell echo $(DEB_UPSTREAM_VERSION) | sed 's/\..*$$//')
libname = libgs
libpkgname = $(libname)$(major)
datapkgname = $(libname)$(major)-common

DEB_UPSTREAM_URL = http://downloads.ghostscript.com/public
DEB_UPSTREAM_TARBALL_MD5 = f7c6f0431ca8d44ee132a55d583212c1

# Non-DFSG license limited to verbatim copying
DEB_UPSTREAM_REPACKAGE_EXCLUDES += \
 ./Resource/CMap/90ms-RKSJ-UCS2 \
 ./Resource/CMap/90pv-RKSJ-UCS2 \
 ./Resource/CMap/90pv-RKSJ-UCS2C \
 ./Resource/CMap/Adobe-CNS1-B5pc \
 ./Resource/CMap/Adobe-CNS1-ETenms-B5 \
 ./Resource/CMap/Adobe-CNS1-H-CID \
 ./Resource/CMap/Adobe-CNS1-H-Host \
 ./Resource/CMap/Adobe-CNS1-H-Mac \
 ./Resource/CMap/Adobe-CNS1-UCS2 \
 ./Resource/CMap/Adobe-GB1-GBK-EUC \
 ./Resource/CMap/Adobe-GB1-GBpc-EUC \
 ./Resource/CMap/Adobe-GB1-H-CID \
 ./Resource/CMap/Adobe-GB1-H-Host \
 ./Resource/CMap/Adobe-GB1-H-Mac \
 ./Resource/CMap/Adobe-GB1-UCS2 \
 ./Resource/CMap/Adobe-Japan1-90ms-RKSJ \
 ./Resource/CMap/Adobe-Japan1-90pv-RKSJ \
 ./Resource/CMap/Adobe-Japan1-H-CID \
 ./Resource/CMap/Adobe-Japan1-H-Host \
 ./Resource/CMap/Adobe-Japan1-H-Mac \
 ./Resource/CMap/Adobe-Japan1-PS-H \
 ./Resource/CMap/Adobe-Japan1-PS-V \
 ./Resource/CMap/Adobe-Japan1-UCS2 \
 ./Resource/CMap/Adobe-Korea1-H-CID \
 ./Resource/CMap/Adobe-Korea1-H-Host \
 ./Resource/CMap/Adobe-Korea1-H-Mac \
 ./Resource/CMap/Adobe-Korea1-KSCms-UHC \
 ./Resource/CMap/Adobe-Korea1-KSCpc-EUC \
 ./Resource/CMap/Adobe-Korea1-UCS2 \
 ./Resource/CMap/B5pc-UCS2 \
 ./Resource/CMap/B5pc-UCS2C \
 ./Resource/CMap/CNS01-RKSJ-H \
 ./Resource/CMap/CNS02-RKSJ-H \
 ./Resource/CMap/CNS03-RKSJ-H \
 ./Resource/CMap/CNS04-RKSJ-H \
 ./Resource/CMap/CNS05-RKSJ-H \
 ./Resource/CMap/CNS06-RKSJ-H \
 ./Resource/CMap/CNS07-RKSJ-H \
 ./Resource/CMap/CNS15-RKSJ-H \
 ./Resource/CMap/ETen-B5-UCS2 \
 ./Resource/CMap/GB-RKSJ-H \
 ./Resource/CMap/GBK-EUC-UCS2 \
 ./Resource/CMap/GBT-RKSJ-H \
 ./Resource/CMap/GBpc-EUC-UCS2 \
 ./Resource/CMap/GBpc-EUC-UCS2C \
 ./Resource/CMap/HK-RKSJ-H \
 ./Resource/CMap/Hojo-RKSJ-H \
 ./Resource/CMap/KSC-RKSJ-H \
 ./Resource/CMap/KSC2-RKSJ-H \
 ./Resource/CMap/KSCms-UHC-UCS2 \
 ./Resource/CMap/KSCpc-EUC-UCS2 \
 ./Resource/CMap/KSCpc-EUC-UCS2C \
 ./Resource/CMap/TCVN-RKSJ-H \
 ./Resource/CMap/UCS2-90ms-RKSJ \
 ./Resource/CMap/UCS2-90pv-RKSJ \
 ./Resource/CMap/UCS2-B5pc \
 ./Resource/CMap/UCS2-ETen-B5 \
 ./Resource/CMap/UCS2-GBK-EUC \
 ./Resource/CMap/UCS2-GBpc-EUC \
 ./Resource/CMap/UCS2-KSCms-UHC \
 ./Resource/CMap/UCS2-KSCpc-EUC
DEB_UPSTREAM_REPACKAGE_EXCLUDES += \
 ./Resource/CMap/Identity-UTF16-H \
 ./Resource/CMap/Identity-UTF16-V
# Sources unavailable
# (PDFs kept commented out as reminder, in case jasper is included)
#DEB_UPSTREAM_REPACKAGE_EXCLUDES += ./jasper/doc/*.pdf
# convenience libraries cluttering authorship/licensing tracking
#  * jpeg would enable old non-standard option (see bug#582521)
#  * jpegxr would enable patented controversial JPEG XR format
DEB_UPSTREAM_REPACKAGE_EXCLUDES += \
 ./cups/libs/ \
 ./expat/ \
 ./freetype/ \
 ./ijs/ \
 ./jasper/ \
 ./jbig2dec/ \
 ./jpeg/ \
 ./jpegxr/ \
 ./lcms/ \
 ./lcms2/ \
 ./libpng/ \
 ./openjpeg/ \
 ./tiff/ \
 ./zlib/

DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^(examples/.*\.pdf|iccprofiles/.*\.icc|toolbin/.*/.*\.icc|Resource/Font/.*|debian/(changelog|copyright(|_hints|_newhints)))$

# put aside upstream-shipped temp files during build but after copyright-check
upstreamtmpfiles = configure
pre-build:: debian/stamp-upstreamtmpstuff
debian/stamp-upstreamtmpstuff: debian/stamp-copyright-check
	for file in $(upstreamtmpfiles); do \
		[ ! -e $$file ] || [ -e $$file.upstream ] || cp -a $$file $$file.upstream; \
	done
	touch $@
clean::
	for file in $(upstreamtmpfiles); do \
		[ ! -e $$file.upstream ] || mv -f $$file.upstream $$file; \
	done
	rm -f debian/stamp-upstreamtmpstuff

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

# Use system libraries
# (listed in order of appearance)
gs_opts = \
 SHARE_JPEG=1 \
 SHARE_LIBPNG=1 \
 SHARE_LIBTIFF=1 \
 SHARE_ZLIB=1 \
 SHARE_JBIG2=1 \
 SHARE_IJS=1 \
 SHARE_EXPAT=1

DEB_CONFIGURE_EXTRA_FLAGS = \
 --with-ijs --with-jbig2dec --with-system-libtiff \
 --with-x --disable-gtk \
 --enable-dynamic --with-omni --disable-compile-inits \
 --with-drivers=ALL \
 --with-fontpath=/var/lib/ghostscript/fonts:/usr/share/cups/fonts:/usr/share/ghostscript/fonts:/usr/local/lib/ghostscript/fonts:/usr/share/fonts

# liblcms1-dev needed for icc34.h - See doc/GS9_Color_Management.pdf
gs_opts += WHICH_CMS=lcms2 SHARE_LCMS=1 LCMS2DIR=/usr
CDBS_BUILD_DEPENDS += , liblcms2-dev, liblcms1-dev

# openjpeg requires patching for ICC and CMYK support
gs_opts += SHARE_JPX=1
DEB_CONFIGURE_EXTRA_FLAGS += --disable-openjpeg --with-jasper
CDBS_BUILD_DEPENDS += , libjasper-dev

gs_opts += SHARE_FT=1
DEB_CONFIGURE_EXTRA_FLAGS += --enable-freetype
CDBS_BUILD_DEPENDS += , libfreetype6-dev

gs_opts += SHARE_LCUPS=1 SHARE_LCUPSI=1
gs_opts += CUPSDATA=/usr/share/ppd/ghostscript
DEB_CONFIGURE_EXTRA_FLAGS += --enable-cups --enable-dbus --with-install-cups
CDBS_BUILD_DEPENDS += , libcups2-dev (>= 1.3.7), libcupsimage2-dev (>= 1.1.20final+rc1-4)
CDBS_BUILD_DEPENDS += , libdbus-1-dev
CDBS_DEPENDS_ghostscript-cups += , ghostscript (>= 8.64~dfsg-8)
CDBS_RECOMMENDS_ghostscript-cups += , cups, cups-client, colord
CDBS_SUGGESTS_ghostscript += , ghostscript-cups

DEB_MAKE_BUILD_TARGET = so gs.a $(gs_opts)
DEB_MAKE_INSTALL_TARGET = soinstall install-cups DESTDIR=$(DEB_DESTDIR) $(gs_opts)

DEB_DH_MAKESHLIBS_ARGS_ghostscript-x = -n

SEDRULE_CONTENT = 's/__ABI__/$(abi)/g;s/__VER__/$(major)/g'

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__,$(libpkgname),$(basename $(infiles)))
pre-build:: $(outfiles)
$(outfiles): update-config debian/stamp-copyright-check
	sed $(SEDRULE_CONTENT) <$(subst $(libpkgname),$(libname)__VER__,$@).in >$@
clean::
	rm -f $(outfiles)

pre-build clean:: fail-source-not-repackaged

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

install/ghostscript::
	# Remove some of the scripts
	rm -f $(DEB_DESTDIR)/usr/bin/unix-lpr.sh $(DEB_DESTDIR)/usr/bin/lprsetup.sh $(DEB_DESTDIR)/usr/bin/pv.sh $(DEB_DESTDIR)/usr/bin/fixmswrd.pl

	# Do not include the Ghostscript loader executable with GTK support
	rm -f $(DEB_DESTDIR)/usr/bin/gsx

	# Rename /usr/bin/gsc, to not conflict with gambc
	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

install/$(datapkgname)::
	# Strip CMap files (shipped separately, registered with DeFoMa)
	rm -rf $(DEB_DESTDIR)/usr/share/ghostscript/$(abi)/Resource/CMap

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

install/libgs-dev::
	mkdir -p $(DEB_DESTDIR)/usr/lib/
	install -m 644 gs.a \
		$(DEB_DESTDIR)/usr/lib/libgs.a

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
binary-post-install/$(libpkgname):: debian/stamp-local-shlibs-$(libname)
debian/stamp-local-shlibs-$(libname): binary-install/$(libpkgname)
	d-shlibmove --commit \
		--override s/liblcms2-2-dev/liblcms2-dev/ \
		--override s/ld1-dev// \
		--devunversioned --ignorelibdep \
		--movedev "debian/tmp/usr/include/*" usr/include/ \
		debian/tmp/usr/lib/$(libname).so
clean::
	rm -f debian/stamp-local-shlibs-$(libname)

binary-post-install/$(datapkgname)::
	file='debian/$(datapkgname)/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
# (separated in core, X11 and autotools dependencies)
CDBS_BUILD_DEPENDS += , libjpeg-dev, libpaper-dev, libpng-dev, libz-dev, libfontconfig1-dev, libtiff-dev, libexpat-dev, libjbig2dec-dev, libidn11-dev, libijs-dev
CDBS_BUILD_DEPENDS += , freeglut3-dev | libglut-dev, libxt-dev, libxext-dev, libx11-dev, libice-dev, libsm-dev
CDBS_BUILD_DEPENDS += , pkg-config

# Needed (always, often, sometimes) at runtime
# TODO: drop gs-cjk-resource after Wheezy+1 (obsoleted before Wheezy)
CDBS_DEPENDS_ghostscript += , gsfonts (>= 6.0-1), debconf | debconf-2.0, debianutils (>= 1.6), $(libpkgname) (= $(DEB_VERSION))
CDBS_DEPENDS_ghostscript-x += , ghostscript (= $(DEB_VERSION))
CDBS_DEPENDS_$(libpkgname) += , poppler-data (>= 0.4.5-3~) | gs-cjk-resource
CDBS_DEPENDS_libgs-dev += , $(libpkgname) (= $(DEB_VERSION))
CDBS_SUGGESTS_ghostscript += , ghostscript-x, hpijs
CDBS_SUGGESTS_ghostscript-doc += , ghostscript

# Public virtual packages
CDBS_PROVIDES_ghostscript += , postscript-viewer

# Transitional quirk: parts of ghostscript split into ghostscript-x
# TODO: drop after Squeeze+1 (was introduced before Squeeze)
CDBS_BREAKS_ghostscript += , ghostscript-x (<< 8.64~dfsg-8)

# Transition: parts of ghostscript split into ghostscript-cups
# TODO: drop after Squeeze+1 (was introduced before Squeeze)
CDBS_CONFLICTS_ghostscript-cups += , ghostscript (<< 8.64~dfsg-8)
CDBS_REPLACES_ghostscript-cups += , ghostscript (<< 8.64~dfsg-8)

# Needed for our packaging
# * recent d-shlibs needed to handle unversioned -dev package and
#   suppress library dependency here declared indirectly using CDBS
CDBS_BUILD_DEPENDS += , d-shlibs (>= 0.45~)
