#!/usr/bin/make -f
#
# do not edit this file, instead edit rules.in!


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

# needed for quilt:
export QUILT_PATCHES=./debian/patches

# debian/rules can never be in eperl_files, because it may not be removed
EPERL_FILES := debian/config debian/preinst debian/postinst debian/prerm debian/postrm \
   debian/common.functions debian/tetex-bin_install
eperl_sourcefiles=debian/variables debian/COPYRIGHT.scripts \
   debian/common.variables debian/common.functions
eperl_pwd = $(shell pwd)/debian
# generated setup scripts that need to be made executable
setup_scripts=debian/tetex-bin_install debian/checkmd5sums
timepattern=.*[[:alpha:]]*[[:space:]]*[[:digit:]] \([[:digit:].]*\) \.\./.*
tex_common_dir = ../../tex-common/tex-common
real_common_variables=$(tex_common_dir)/debian/common.variables
real_common_functions_in=$(tex_common_dir)/debian/common.functions.in

stampdir=./stampdir

# use the Debian single file patch system
# export DSF-PATCHLIST=debian/patches/patchlist.mk
# include /usr/share/dsfp/dsf-patch.mk

SHELL=/bin/bash

# commented to test whether this is still needed (see bugs #269039 and #180665)
# ifeq ($(DEB_BUILD_ARCH),ia64)
#   CFLAGS=-g
# else
#   CFLAGS =
# endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O0
else
  CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  # this or dh_strip...
  INSTALL_PROGRAM += -s
endif

package=tetex-bin
sublib=libkpathsea4
subdev=libkpathsea-dev

sharedir=debian/$(package)/usr/share
txmfdir=$(sharedir)/texmf-tetex
vardir=/var/lib/texmf
debvar=debian/$(package)$(vardir)
etcdir=/etc/texmf
debetc=debian/$(package)/etc/texmf

###############
# Main targets
###############

# Eperl is simply great: thanks, Davide!
% :: %.in $(eperl_sourcefiles) 
	eperl -k -I $(eperl_pwd) -P -o $@ $<

# Override this rule for debian/rules, which needs an additional command
debian/rules: debian/rules.in $(eperl_sourcefiles)
        # Refresh debian/common.variables and debian/common.functions.in every
        # time the rules file is going to be regenerated
	$(update_commonstuff)
        # Regenerate the rules file
	eperl -k -P -o $@ $<

# Override the general rule for eperl-generated files in order to avoid a
# circular dependency (debian/common.functions is listed in
# $(eperl_sourcefiles)).
debian/common.functions: debian/common.functions.in debian/common.variables
	eperl -k -P -o $@ $<

clean: clean-commonstuff clean-upstream clean-deb clean-patches

configure: configure-stamp

build: build-stamp

install: $(EPERL_FILES) install-bin install-lib 

binary: binary-indep binary-arch

sarge: debian/sarge-stamp

sid: debian/sid-stamp


##############################
# Subtargets that do the work
##############################

# common.variables and common.functions are shared between the
# tetex-base and tetex-bin source packages.  On a normal build, they
# are copied from /usr/share/tex-common/teTeX-build/.  Developers can
# always used the most recent version from tex-commons svn repository.
# If you want to use this mechanism, check out the trunk of the
# tetex-common tree and pass its location relative to the build dir to
# make in the variable $(tex_common_dir).  The default is
# ../../tex-common/tex-common/.
define update_commonstuff
test -e $(real_common_variables) && \
  test $(real_common_variables) -nt debian/common.variables && \
  cp $(real_common_variables) debian/ || true

test -e $(real_common_functions_in) && \
  test $(real_common_functions_in) -nt debian/common.functions.in && \
  cp $(real_common_functions_in) debian/ && \
  eperl -k -P -o debian/common.functions debian/common.functions.in || true
endef

# For command-line use
commonstuff_update:
	$(update_commonstuff)


# this is needed to prevent remaking of configure from configure.in:
configure:
	:

$(stampdir): 
	mkdir $(stampdir) || touch $(stampdir)

clean-patches:
	# unapply patches, not giving an error when quilt says "No patch removed"
	quilt pop -a || [ $$? = 2 ]
	rm -f patch-stamp

patch-stamp:
	quilt push -a
	touch $@

clean-commonstuff:
	$(update_commonstuff)

clean-deb: clean-patches
	$(checkdir)
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	-rmdir $(stampdir)

	# Add here commands to clean up after the build process.
	# Make sure all of our expected symlinks are in place
	sh debian/restore-symlinks
	-rm -f texk/conftest*
# Remove symlinks
# 2.99.1: Don't remove symlinks, they are now in the upstream tarball (but not in CVS)
#	sh debian/restore-symlinks -rm
	rm -f debian/conffiles
	rm -f $(EPERL_FILES)
	rm -f debian/tetex-xwarn
	rm -f $(shell cat debian/tetex-bin.info) debian/dir debian/dir.old

	dh_clean


clean-upstream: 
	-make distclean


configure-stamp: $(stampdir) patch-stamp
# comment out the following line if you want to check the configure target
# in a CVS working copy
	$(check-svn)
	dh_testdir
	# Make sure all of our expected symlinks are in place
	sh debian/restore-symlinks

# 	cp -f /usr/share/misc/config.{guess,sub} ./texk/
# 	cp -f /usr/share/misc/config.{guess,sub} ./utils/texinfo/
# 	cp -f /usr/share/misc/config.{guess,sub} ./config/
# test whether any of these is still needed

	# Add here commands to configure the package.
# explicitly disable subpackages that have separate packages in Debian, even if currently
# not in teTeX (but in ./configure because of TeXLive) or not built by default.
	./configure --prefix=/usr \
	  --enable-ipc --disable-multiplatform --enable-shared \
	  --without-dialog --without-texinfo --without-t1utils --without-dvipng \
	  --without-lcdf-typetools --without-psutils --without-dvi2tty --without-dvidvi \
	  --without-lacheck --without-musixflx --without-texi2html \
	  --with-cjkutils --with-detex --with-dtl --with-dviljk --with-seetexk \
	   --with-x --with-xdvi-x-toolkit=xaw --with-mf-x-toolkit \
	   --with-system-ncurses --with-system-zlib --with-system-pnglib \
	   --with-system-tifflib --with-system-wwwlib --with-system-t1lib \
	   --with-system-gd --mandir='$${prefix}/share/man' --infodir='$${prefix}/share/info' \
	   --without-xdvik

	touch configure-stamp

# debian/po/templates.pot: debian/templates
# 	debconf-updatepo
# 	grep "Last-Translator" debian/po/*.po | \
# 	  sed -e 's/.*\(<.*@.*>\).*/\1, /g' | \
# 	  tr "\n" " " | sed -e 's/,  $$/\n/' \
# 	  > debian/recent-translators

build-stamp: configure-stamp 
# comment out the following line if you want to check the build target
# in a CVS working copy
	$(check-svn)
	dh_testdir

	# just to be sure to not use them
	rm -rf libs/xpdf libs/zlib

	# Obsolete and now fixed pl.l by patch-tmp (for broken flex)
#	cp texk/web2c/omegafonts/lex.yy.c-dist texk/web2c/omegafonts/lex.yy.c
	# Add here commands to compile the package.
	make CFLAGS="${CFLAGS}" #XXCFLAGS='-I/usr/include/poppler' LDFLAGS='-lpoppler'
	gcc -o debian/tetex-xwarn -O2 -W -Wall -ansi -pedantic \
	   -L/usr/X11R6/lib -lXaw -lXt -lX11 -I/usr/X11R6/include \
	   debian/texdoctk-warn.c

	touch build-stamp

install-bin: build-stamp
	dh_testdir
	dh_testroot
	dh_installdirs

	make install prefix=`pwd`/debian/$(package)/usr texmf=`pwd`/$(txmfdir) infodir=`pwd`/debian

	chmod a+x $(setup_scripts)
	debian/tetex-bin_install --move-to-texmfmain
	debian/tetex-bin_install --rm-only
	debian/tetex-bin_install --format-links
	debian/tetex-bin_install --manpage-links
	debian/tetex-bin_install --debianprogs
	debian/tetex-bin_install --conffiles
#	debian/tetex-bin_install --xdvi
	debian/tetex-bin_install --misc
	debian/tetex-bin_install --omfonts


install-lib: build-stamp
# libkpathsea4 and most of the dev-package is installed by dh_movefiles
# libkpathsea-dev installer
	mkdir -p debian/$(subdev)/usr/share/doc
	(cd debian/$(subdev)/usr/share/doc; ln -sf $(sublib) $(subdev))


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	$(check-svn)
	dh_testdir
	dh_testroot
	dh_installdebconf	
	dh_installdocs -p$(package) -p$(sublib) debian/NEWS.Debian debian/README.Developers 
	dh_installexamples
	dh_installinfo
	dh_installchangelogs -p$(package) -p$(sublib) ChangeLog
	cp debian/changelog.Debian.experimental debian/$(package)/usr/share/doc/$(package)/
	dh_installmime
	dh_installmenu
	dh_installcron
	dh_installman new-manpages/[a-z]*
	dh_movefiles --sourcedir=debian/$(package)
	dh_strip
	dh_compress
	dh_fixperms
	dh_perl
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -l $(CURDIR)/debian/$(sublib)/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	echo -e -n "\a"

define checkdir
	test -f debian/rules
endef

define check-svn
	if test -d debian/.svn; then \
		echo "please clean subversion directories"; \
		exit 1; \
	fi
endef

checkroot:
	$(checkdir)
	test root = "`whoami`"

####################################################################
#  targets to compile for sarge
####################################################################

CHANGED_FILES_SARGE = debian/changelog debian/control
EXCHANGED_FILES_SARGE =

debian/sarge-stamp: 
	for file in $(CHANGED_FILES_SARGE); do \
	    cp $${file} debian/sarge/`basename $${file}`.sid; \
	done

	# needs devscripts in etch or from backports.org
	debchange --distribution sarge-backports -b --newversion 3.0-30maemo2+0m5~bpo.1 "Recompiled for sarge."
	debchange --append \
	  "Add a Build-Depends on sarge's xlibs-dev to prevent linking against XOrg"
	debchange --append \
	  "Lower versioned Depends on tex-common from (>= 0.34) to (>= 0.34~bpo.1)"
	sed -f debian/sarge/control.sed debian/sarge/control.sid > debian/control
# 	sed -f debian/sarge/rules.sed debian/sarge/rules.in.sid > debian/rules.in

	for file in $(EXCHANGED_FILES_SARGE); do \
	    mv $${file} debian/sarge/`basename $${file}`.sid; \
	    cp debian/sarge/`basename $${file}`.sarge $${file} ; \
	done

	rm debian/sid-stamp
	echo "dummy content for dpkg-source" > debian/sarge-stamp 

debian/sid-stamp: 
	for file in $(CHANGED_FILES_SARGE); do \
	    mv debian/sarge/`basename $${file}`.sid $${file}; \
	done

	for file in $(EXCHANGED_FILES_SARGE); do \
	    mv debian/sarge/`basename $${file}`.sid  $${file} ; \
	done

	# need to remake rules
	touch debian/rules.in

	rm debian/sarge-stamp
	echo "dummy content for dpkg-source" > debian/sid-stamp 

.PHONY: binary binary-arch binary-indep clean checkroot clean-patches sarge sid


# Local variables:
# mode: Makefile
# end:
