#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1


# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# Include dpatch stuff.
include /usr/share/dpatch/dpatch.make

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
ifeq ($(DEB_HOST_ARCH),s390)
	CC=gcc-3.4
else
	CC=gcc
endif

config.status: configure
	dh_testdir
	# Add here commands to configure the package.
	CC=$(CC) LDFLAGS=-lpaper CFLAGS="$(CFLAGS)" ./autogen.sh --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info \
	--with-ijs --with-jbig2dec --with-jasper --with-x --with-gs=gs-gpl

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)

build: patch build-stamp

build-stamp:  config.status
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)
	#/usr/bin/docbook-to-man debian/gs-gpl.sgml > gs-gpl.1

	touch build-stamp

clean: unpatch fail-if-cmaps-exist
	dh_testdir
	dh_testroot
	rm -f build-stamp 

	# Add here commands to clean up after the build process.
	-$(MAKE) distclean
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	# Add here commands to install the package into debian/gs-gpl.
	$(MAKE) install prefix=$(CURDIR)/debian/gs-gpl/usr

	# We only need gs-gpl binary (others are in gs-common)
	rm -f $(CURDIR)/debian/gs-gpl/usr/bin/*
	install -m 755 $(CURDIR)/bin/gs-gpl $(CURDIR)/debian/gs-gpl/usr/bin

	# We only need gs-gpl.1.gz (others are in gs-common)
	rm -rf $(CURDIR)/debian/gs-gpl/usr/share/man/de
	rm -rf $(CURDIR)/debian/gs-gpl/usr/share/man/man1/*
	install -m 644 $(CURDIR)/man/gs.1 $(CURDIR)/debian/gs-gpl/usr/share/man/man1/gs-gpl.1

	# don't install cidfmap, FAPIcidfmap, FAPIfontmap, Fontmap and Fontmap.GS so that it will be provided by gs-common
	rm -f $(CURDIR)/debian/gs-gpl/usr/share/gs-gpl/8.54/lib/cidfmap
	rm -f $(CURDIR)/debian/gs-gpl/usr/share/gs-gpl/8.54/lib/{FAPIcidfmap,FAPIfontmap}
	rm -f $(CURDIR)/debian/gs-gpl/usr/share/gs-gpl/8.54/lib/{Fontmap,Fontmap.GS}

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
#	dh_install -i
#	dh_installmenu -i
#	dh_installdebconf -i	
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installpam -i
#	dh_installmime -i
#	dh_installinit -i
#	dh_installcron -i
#	dh_installinfo -i
	dh_installman -i
	dh_link -i
	dh_strip -i
	dh_compress -i
	dh_fixperms -i
#	dh_perl -i
#	dh_python -i
#	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
#	dh_install -a
#	dh_installmenu -a
#	dh_installdebconf -a	
#	dh_installlogrotate -a
#	dh_installemacsen -a
#	dh_installpam -a
#	dh_installmime -a
#	dh_installinit -a
#	dh_installcron -a
#	dh_installinfo -a
	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
#	dh_perl -a
#	dh_python -a
#	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install 
