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

DEB_AUTO_UPDATE_AUTOCONF=yes

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

# for building w/ FAPI
#FT_CPPFLAGS = $(shell pkg-config --cflags freetype2)
#FT_LDFLAGS=$(shell pkg-config --libs freetype2)
#FT_OPTS := FT_BRIDGE=1 FT_CFLAGS=$(FT_CPPFLAGS) FT_LIB=$(FT_LDFLAGS)

#ifeq ($(DEB_HOST_ARCH),s390)
#	CC=gcc-3.4
#else
#	CC=gcc
#endif

# Handling DEB_BUILD_OPTIONS
#export DEB_BUILD_OPTIONS=noopt,nostrip
CFLAGS:=$(CFLAGS) -g $(if $(findstring noopt,$(DEB_BUILD_OPTIONS)),-O0,-O2) 

DEB_CONFIGURE_SCRIPT_ENV := CFLAGS="$(CFLAGS) -fPIC" CPPFLAGS="$(FT_CPPFLAGS)" LDFLAGS="-lpaper $(FT_LDFLAGS)"
DEB_CONFIGURE_EXTRA_FLAGS := --prefix=/usr --datarootdir=/opt/usr/share --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info \
	--with-ijs --with-jbig2dec --with-jasper --with-x --disable-gtk \
	--enable-dynamic --with-omni --enable-cups --disable-compile-inits \
	--with-drivers=ALL \
	--with-fontpath=/var/lib/defoma/gs.d/dirs/fonts:/opt/usr/share/cups/fonts:/usr/share/ghostscript/fonts:/usr/local/lib/ghostscript/fonts:/usr/share/fonts:/usr/share/fonts/type1/gsfonts
DEB_MAKE_BUILD_TARGET := STDDIRS $(FT_OPTS)
DEB_MAKE_INSTALL_TARGET := soinstall install-shared install-cups install-contrib-extras \
		DESTDIR=$(CURDIR)/debian/ghostscript \
		prefix=/usr \
		datadir=/opt/usr/share \
		gsdir=/opt/usr/share/ghostscript \
		docdir=/usr/share/doc/ghostscript \
		exdir=/usr/share/doc/ghostscript/examples \
		CUPSSERVERBIN=/opt/usr/lib/cups \
		CUPSSERVER=/opt/usr/lib/cups \
		CUPSSERVERROOT=/etc/cups \
		CUPSDATA=/opt/usr/share/ppd/ghostscript

fail-if-cmaps-exist:
	@test ! -d Resource/CMap || ( \
		echo; \
		echo 'ERROR: Source contains CMaps that are declared non-free.'; \
		echo '       Please strip Resource/CMap from source!'; \
		exit 1)

fail-if-jasper-pdfs-exist:
	@test ! -f jasper/doc/*.pdf || ( \
		echo; \
		echo 'ERROR: Source contains JasPer PDFs that are declared non-free.'; \
		echo '       Please remove PDFs from source!'; \
		exit 1)

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

build/ghostscript::
	#/usr/bin/docbook-to-man debian/ghostscript.sgml > ghostscript.1

build/ghostscript-x::
	$(MAKE) obj/X11.so

build/libgs8::
	$(MAKE) so

install/ghostscript::
	# Move *.types and *.convs files from /etc/cups to
	# /usr/share/cups/mime, so that they are not considered conffiles
	mkdir -p $(CURDIR)/debian/ghostscript/opt/usr/share/cups/mime/
	mv $(CURDIR)/debian/ghostscript/etc/cups/*.types \
	   $(CURDIR)/debian/ghostscript/opt/usr/share/cups/mime/ 2> /dev/null || :
	mv $(CURDIR)/debian/ghostscript/etc/cups/*.convs \
	   $(CURDIR)/debian/ghostscript/opt/usr/share/cups/mime/ 2> /dev/null || :
	rmdir --ignore-fail-on-non-empty $(CURDIR)/debian/ghostscript/etc/cups
	rmdir --ignore-fail-on-non-empty $(CURDIR)/debian/ghostscript/etc

	# Install DeFoMa script
	install -m 644 $(CURDIR)/debian/gs.defoma $(CURDIR)/debian/ghostscript/usr/share/defoma/scripts

	# Remove some of the scripts
	rm $(CURDIR)/debian/ghostscript/usr/bin/unix-lpr.sh $(CURDIR)/debian/ghostscript/usr/bin/lprsetup.sh $(CURDIR)/debian/ghostscript/usr/bin/pv.sh $(CURDIR)/debian/ghostscript/usr/bin/fixmswrd.pl

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

	# Rename /usr/bin/gsc, to not conflict with gambc (bug LP #118785).
	mv $(CURDIR)/debian/ghostscript/usr/bin/gsc $(CURDIR)/debian/ghostscript/usr/bin/gs

	# Some convenience links
	ln -s /usr/bin/gs $(CURDIR)/debian/ghostscript/usr/bin/ghostscript
	ln -s /usr/share/man/man1/gs.1 $(CURDIR)/debian/ghostscript/usr/share/man/man1/ghostscript.1

	# Remove C headers, they go into the libgs-dev package
	rm -rf $(CURDIR)/debian/ghostscript/usr/include

install/ghostscript-x::
	# Separate the dynamic library for X support into the ghostscript-x
	# package
	mkdir -p $(CURDIR)/debian/ghostscript-x/usr/lib/
	mv $(CURDIR)/debian/ghostscript/usr/lib/ghostscript $(CURDIR)/debian/ghostscript-x/usr/lib/

install/ghostscript-doc::
	# Move documentation into the ghostscript-doc package
	mkdir -p $(CURDIR)/debian/ghostscript-doc/usr/share/
	mv $(CURDIR)/debian/ghostscript/usr/share/doc $(CURDIR)/debian/ghostscript-doc/usr/share/
	# Install cjk examples too
	install -d  $(CURDIR)/debian/ghostscript-doc/usr/share/doc/ghostscript/examples/cjk
	install -m 644 $(CURDIR)/examples/cjk/* $(CURDIR)/debian/ghostscript-doc/usr/share/doc/ghostscript/examples/cjk

binary-post-install/ghostscript::
	# for non-arch-all builds, we need to remove the
	# documentation manually, since install/ghostscript-doc is not called
	# for them
	rm -f $(CURDIR)/debian/ghostscript/usr/share/doc/ghostscript/*.htm
	rm -f $(CURDIR)/debian/ghostscript/usr/share/doc/ghostscript/*.html
	rm -f $(CURDIR)/debian/ghostscript/usr/share/doc/ghostscript/*.css
	rm -f $(CURDIR)/debian/ghostscript/usr/share/doc/ghostscript/README
	rm -rf $(CURDIR)/debian/ghostscript/usr/share/doc/ghostscript/examples

install/libgs8::
	# Move libgs into the libgs8 package
	mkdir -p $(CURDIR)/debian/libgs8/usr/lib/
	rm -f $(CURDIR)/debian/ghostscript/usr/lib/libgs*
	cp sobin/libgs.so.8.* $(CURDIR)/debian/libgs8/usr/lib/
	cp -P sobin/libgs.so.8 $(CURDIR)/debian/libgs8/usr/lib/

	# Move libgs.so link into the libgs-dev package
	# and install also the C headers there
	mkdir -p $(CURDIR)/debian/libgs-dev/usr/lib/
	cp -P sobin/libgs.so $(CURDIR)/debian/libgs-dev/usr/lib/
	mkdir -p $(CURDIR)/debian/libgs-dev/usr/include/ghostscript
	install -m 644 $(CURDIR)/src/iapi.h $(CURDIR)/debian/libgs-dev/usr/include/ghostscript
	install -m 644 $(CURDIR)/src/ierrors.h $(CURDIR)/debian/libgs-dev/usr/include/ghostscript
	install -m 644 $(CURDIR)/src/gdevdsp.h $(CURDIR)/debian/libgs-dev/usr/include/ghostscript
