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

unexport LANG LC_ALL LC_CTYPE LC_COLLATE LC_TIME LC_NUMERIC LC_MESSAGES
unexport CFLAGS CXXFLAGS CPPFLAGS LDFLAGS

export SHELL = /bin/bash

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

vafilt = $(subst $(2)=,,$(filter $(2)=%,$(1)))

DPKG_VARS		:= $(shell dpkg-architecture)
DEB_HOST_ARCH		?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH)
DEB_HOST_ARCH_BITS	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH_BITS)
DEB_HOST_ARCH_OS	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH_OS)

changelog_values := $(shell dpkg-parsechangelog \
			| awk '/^(Version|Source):/ {print $$2}')
PKGSOURCE  := $(word 1, $(changelog_values))
PKGVERSION := $(word 2, $(changelog_values))

on_buildd := $(shell [ -f /CurrentlyBuilding -o "$$LOGNAME" = buildd ] && echo yes)

ifneq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
  WITHOUT_CHECK := yes
endif
WITHOUT_BENCH :=
ifneq (,$(findstring nobench, $(DEB_BUILD_OPTIONS)))
  WITHOUT_BENCH := yes
endif
ifneq (,$(filter $(DEB_HOST_ARCH), hurd-i386))
  WITHOUT_BENCH := disabled on $(DEB_HOST_ARCH)
endif
ifeq ($(on_buildd),yes)
  ifneq (,$(findstring $(DEB_HOST_ARCH), hppa mips mipsel s390))
#    WITHOUT_CHECK := yes
  endif
endif
ifneq (,$(filter $(DEB_HOST_ARCH), kfreebsd-amd64 kfreebsd-i386))
  WITHOUT_CHECK := yes
endif

#Force testings off
WITHOUT_BENCH := yes
WITHOUT_CHECK := yes

COMMA = ,
ifneq (,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
  NJOBS := -j $(subst parallel=,,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
endif

distribution := $(shell lsb_release -is)
#distribution := Ubuntu

VER=2.7
NVER=2.8
PVER=python2.7
PRIORITY=$(shell echo $(VER) | tr -d '.')0

PREVVER	:= $(shell awk '/^python/ && NR > 1 {print substr($$2,2,length($$2)-2); exit}' debian/changelog)

# default versions are built from the python-defaults source package
# keep the definition to adjust package priorities.
DEFAULT_VERSION = no
STATIC_PYTHON=yes

MIN_MODS := $(shell awk '/^  / && $$2 == "module" { print $$1 }' \
		debian/PVER-minimal.README.Debian.in)
MIN_EXTS := $(shell awk '/^  / && $$2 == "extension" { print $$1 }' \
		debian/PVER-minimal.README.Debian.in)
MIN_BUILTINS := $(shell awk '/^  / && $$2 == "builtin" { print $$1 }' \
		debian/PVER-minimal.README.Debian.in)
MIN_PACKAGES := $(shell awk '/^  / && $$2 == "package" { print $$1 }' \
		debian/PVER-minimal.README.Debian.in)
MIN_ENCODINGS := $(foreach i, \
			$(filter-out \
			  big5% bz2% cp932.py cp949.py cp950.py euc_% \
			  gb% iso2022% johab.py shift_jis% , \
			  $(shell cd Lib/encodings && echo *.py)), \
			encodings/$(i)) \
		 codecs.py stringprep.py

with_tk := no
with_gdbm := no
with_interp := static
#with_interp := shared

PY_INTERPRETER = /$(PREFIX)/bin/python$(VER)

ifeq ($(DEFAULT_VERSION),yes)
  PY_PRIO = standard
  #PYSTDDEP = , python (>= $(VER))
  ifeq ($(distribution),Ubuntu)
    PY_MINPRIO = required
  else
    PY_MINPRIO = $(PY_PRIO)
  endif
else
  PY_PRIO = optional
  PY_MINPRIO = $(PY_PRIO)
endif
with_fpectl = yes

CC = gcc

DPKG_CFLAGS  := $(shell dpkg-buildflags --get CFLAGS)
DPKG_LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)
OPT_CFLAGS   := $(filter-out -O%,$(DPKG_CFLAGS)) # default is -O3
DEBUG_CFLAGS := $(patsubst -O%,-O0,$(DPKG_CFLAGS))

# on alpha, use -O2 only, use -mieee
ifeq ($(DEB_HOST_ARCH),alpha)
    OPT_CFLAGS += -mieee
    DEBUG_CFLAGS += -mieee
    EXTRA_OPT_FLAGS += -O2
endif
# issues with ia64 and m68k with -O3
ifeq ($(DEB_HOST_ARCH),m68k)
    EXTRA_OPT_FLAGS += -O2
endif

ifeq ($(DEB_HOST_ARCH_OS),linux)
  ifneq (,$(findstring $(DEB_HOST_ARCH), amd64 armel armhf i386 powerpc ppc64))
    with_pgo := yes
  endif
endif

ifneq (,$(findstring $(DEB_HOST_ARCH), amd64 armel armhf i386))
  with_lto := yes
endif
#Force:
with_lto := no

ifneq (,$(findstring noopt, $(DEB_BUILD_OPTIONS)))
    OPT_CFLAGS := $(filter-out -O%, $(OPT_CFLAGS))
    EXTRA_OPT_CFLAGS = -O0
    with_pgo =
    with_lto =
endif

ifeq ($(with_lto),yes)
    EXTRA_OPT_CFLAGS += -g1 -flto -fuse-linker-plugin
endif

make_build_target = 
#make_build_target = $(if $(with_pgo))

buildd_static	:= $(CURDIR)/build-static
buildd_shared	:= $(CURDIR)/build-shared
buildd_debug	:= $(CURDIR)/build-debug
buildd_shdebug	:= $(CURDIR)/build-shdebug

d		:= debian/tmp
scriptdir	=  $(PREFIX)/share/lib/python$(VER)
scriptdir	=  $(PREFIX)/share/python$(VER)
scriptdir	=  $(PREFIX)/lib/python$(VER)

# package names and directories
p_base	:= $(PVER)
p_min	:= $(PVER)-minimal
p_lib	:= lib$(PVER)
p_tk	:= $(PVER)-tk
p_gdbm	:= $(PVER)-gdbm
p_dev	:= $(PVER)-dev
p_exam	:= $(PVER)-examples
p_idle	:= idle-$(PVER)
p_doc	:= $(PVER)-doc
p_dbg	:= $(PVER)-dbg

d_base	:= debian/$(p_base)
d_min	:= debian/$(p_min)
d_lib	:= debian/$(p_lib)
d_tk	:= debian/$(p_tk)
d_gdbm	:= debian/$(p_gdbm)
d_dev	:= debian/$(p_dev)
d_exam	:= debian/$(p_exam)
d_idle	:= debian/$(p_idle)
d_doc	:= debian/$(p_doc)
d_dbg	:= debian/$(p_dbg)

build-arch: stamps/stamp-build
build-indep: stamps/stamp-build-doc
build: build-arch
stamps/stamp-build: stamps/stamp-build-static stamps/stamp-mincheck \
    stamps/stamp-build-shared stamps/stamp-build-debug \
    stamps/stamp-build-shared-debug \
    stamps/stamp-check stamps/stamp-pystone stamps/stamp-pybench
	touch $@

PROFILE_EXCLUDES = test_compiler test_distutils test_platform test_subprocess \
	test_multiprocessing test_cprofile \
	test_thread test_threaded_import test_threadedtempfile \
	test_socketserver \
	test_threading test_threading_local test_threadsignals \
	test_dbm_dumb test_dbm_ndbm test_pydoc test_sundry test_gdb \

ifneq (,$(filter $(DEB_HOST_ARCH), arm armel))
  PROFILE_EXCLUDES += test_float
endif
ifneq (,$(filter $(DEB_HOST_ARCH), kfreebsd-amd64 kfreebsd-i386))
  PROFILE_EXCLUDES += test_io
endif
PROFILE_EXCLUDES += test_zipfile
PROFILE_EXCLUDES += test_xmlrpc
PROFILE_EXCLUDES += test_bsddb3

PROFILE_TASK = ../Lib/test/regrtest.py \
	-x $(sort $(TEST_EXCLUDES) $(PROFILE_EXCLUDES))

stamps/stamp-build-static: stamps/stamp-configure-static
	dh_testdir
	$(MAKE) $(NJOBS) -C $(buildd_static) \
		EXTRA_CFLAGS="$(EXTRA_OPT_CFLAGS)" \
		PROFILE_TASK='$(PROFILE_TASK)' $(make_build_target)
	touch stamps/stamp-build-static

stamps/stamp-build-shared: stamps/stamp-configure-shared
	dh_testdir
	$(MAKE) $(NJOBS) -C $(buildd_shared) \
		EXTRA_CFLAGS="$(EXTRA_OPT_CFLAGS)"
#	: # build the shared library
#	$(MAKE) $(NJOBS) -C $(buildd_shared) \
#		libpython$(VER).so
	: # build a static library with PIC objects
	$(MAKE) $(NJOBS) -C $(buildd_shared) \
		EXTRA_CFLAGS="$(EXTRA_OPT_CFLAGS)" \
		LIBRARY=libpython$(VER)-pic.a libpython$(VER)-pic.a
	touch stamps/stamp-build-shared

stamps/stamp-build-debug: stamps/stamp-configure-debug
	dh_testdir
	$(MAKE) $(NJOBS) -C $(buildd_debug)
		EXTRA_CFLAGS="$(DEBUG_CFLAGS)"
	touch stamps/stamp-build-debug

stamps/stamp-build-shared-debug: stamps/stamp-configure-shared-debug
	dh_testdir
	: # build the shared debug library
	$(MAKE) $(NJOBS) -C $(buildd_shdebug) \
		libpython$(VER)_d.so
	touch stamps/stamp-build-shared-debug

PREFIX=opt/python27
common_configure_args = \
		--prefix=/$(PREFIX) \
		--enable-ipv6 \
		--enable-unicode=ucs4 \
		--with-dbmliborder=bdb:gdbm \
		--with-system-expat

ifeq ($(DEB_HOST_ARCH), avr32)
  common_configure_args += --without-ffi
else
  common_configure_args += --with-system-ffi
endif

ifeq ($(with_fpectl),yes)
  common_configure_args += \
		--with-fpectl
endif

stamps/stamp-configure-shared: stamps/stamp-patch
	rm -rf $(buildd_shared)
	mkdir -p $(buildd_shared)
	cd $(buildd_shared) && \
	  CC="$(CC)" CFLAGS="$(OPT_CFLAGS)" LDFLAGS="$(DPKG_LDFLAGS)" \
	    ../configure \
		--enable-shared \
		$(common_configure_args)

	$(call __post_configure,$(buildd_shared))

	touch stamps/stamp-configure-shared

stamps/stamp-configure-static: stamps/stamp-patch
	rm -rf $(buildd_static)
	mkdir -p $(buildd_static)
	cd $(buildd_static) && \
	  CC="$(CC)" CFLAGS="$(OPT_CFLAGS)" LDFLAGS="$(DPKG_LDFLAGS)" \
	    ../configure \
		$(common_configure_args)

	$(call __post_configure,$(buildd_static))

	touch stamps/stamp-configure-static

stamps/stamp-configure-debug: stamps/stamp-patch
	rm -rf $(buildd_debug)
	mkdir -p $(buildd_debug)
	cd $(buildd_debug) && \
	  CC="$(CC)" CFLAGS="$(DEBUG_CFLAGS)" LDFLAGS="$(DPKG_LDFLAGS)" \
	    ../configure \
		$(common_configure_args) \
		--with-pydebug

	$(call __post_configure,$(buildd_debug))

	touch stamps/stamp-configure-debug

stamps/stamp-configure-shared-debug: stamps/stamp-patch
	rm -rf $(buildd_shdebug)
	mkdir -p $(buildd_shdebug)
	cd $(buildd_shdebug) && \
	  CC="$(CC)" CFLAGS="$(DEBUG_CFLAGS)" LDFLAGS="$(DPKG_LDFLAGS)" \
	    ../configure \
		$(common_configure_args) \
		--enable-shared \
		--with-pydebug

	$(call __post_configure,$(buildd_shdebug))

	touch stamps/stamp-configure-shared-debug

define __post_configure
	egrep \
	  "^#($$(awk -v ORS='|' '$$2 ~ /^extension$$/ {print $$1}' debian/PVER-minimal.README.Debian.in)XX)" \
	  Modules/Setup.dist \
	    | sed -e 's/^#//' -e 's/-Wl,-Bdynamic//;s/-Wl,-Bstatic//' \
	    >> $(1)/Modules/Setup.local

	: # unconditionally run makesetup
	cd $(1) && \
	  ../Modules/makesetup -c ../Modules/config.c.in -s Modules \
	    Modules/Setup.config Modules/Setup.local Modules/Setup
	mv $(1)/config.c $(1)/Modules/

	: # and fix the timestamps
	$(MAKE) -C $(1) Makefile Modules/config.c

	: # apply workaround for missing os.fsync
	sed 's/HAVE_SYNC/HAVE_FSYNC/g' $(1)/pyconfig.h \
		> $(1)/pyconfig.h.new
	touch -r $(1)/pyconfig.h $(1)/pyconfig.h.new
	mv -f $(1)/pyconfig.h.new $(1)/pyconfig.h
endef

stamps/stamp-mincheck: stamps/stamp-build-static debian/PVER-minimal.README.Debian.in
	for m in $(MIN_MODS) $(MIN_PACKAGES) $(MIN_EXTS) $(MIN_BUILTINS); do \
	  echo "import $$m"; \
	done > $(buildd_static)/minmods.py
	cd $(buildd_static) && ./python ../debian/pymindeps.py minmods.py \
	  > $(buildd_static)/mindeps.txt
	if [ -x /$(PREFIX)/bin/dot ]; then \
	  python debian/depgraph.py < $(buildd_static)/mindeps.txt \
	    > $(buildd_static)/mindeps.dot; \
	  dot -Tpng -o $(buildd_static)/mindeps.png \
	    $(buildd_static)/mindeps.dot; \
	else true; fi
	cd $(buildd_static) && ./python ../debian/mincheck.py \
		minmods.py mindeps.txt
	touch stamps/stamp-mincheck

TEST_RESOURCES = all
ifeq ($(on_buildd),yes)
  TEST_RESOURCES := $(TEST_RESOURCES),-network,-urlfetch
endif
TESTOPTS = -w -l -u$(TEST_RESOURCES)
TEST_EXCLUDES =
ifeq ($(on_buildd),yes)
  TEST_EXCLUDES += test_codecmaps_cn test_codecmaps_hk \
        test_codecmaps_jp test_codecmaps_kr test_codecmaps_tw \
        test_normalization test_ossaudiodev
  ifneq (,$(filter $(DEB_HOST_ARCH), mips mipsel powerpc kfreebsd-i386 sparc))
    TEST_EXCLUDES += test_threading
  endif
  ifeq (,$(wildcard $(HOME)))
    TEST_EXCLUDES += test_site
  endif
endif
ifeq (,$(wildcard /dev/dsp))
  TEST_EXCLUDES += test_linuxaudiodev test_ossaudiodev
endif
ifneq (,$(filter $(DEB_HOST_ARCH), armel hppa powerpc))
  TEST_EXCLUDES += test_multiprocessing
endif
ifneq (,$(filter $(DEB_HOST_ARCH), hppa))
  TEST_EXCLUDES += test_fork1 test_socketserver test_threading test_wait3 test_wait4 test_gdb
endif
ifneq (,$(filter $(DEB_HOST_ARCH), arm avr32))
  TEST_EXCLUDES += test_ctypes
endif
ifneq (,$(filter $(DEB_HOST_ARCH), m68k avr32 kfreebsd-amd64 kfreebsd-i386))
  TEST_EXCLUDES += test_bsddb3
endif
ifneq (,$(filter $(DEB_HOST_ARCH), arm armel avr32 m68k))
  ifeq ($(on_buildd),yes)
    TEST_EXCLUDES += test_compiler
  endif
endif
TEST_EXCLUDES += test_gdb
# fails on some buildds
ifneq (,$(filter $(DEB_HOST_ARCH), amd64))
  TEST_EXCLUDES += test_io
endif
ifneq (,$(filter $(DEB_HOST_ARCH), kfreebsd-amd64 kfreebsd-i386))
  TEST_EXCLUDES += test_io test_signal
endif
ifneq (,$(filter $(DEB_HOST_ARCH), hurd-i386))
  TEST_EXCLUDES += test_io test_random test_signal test_socket test_socketserver test_ssl test_threading
endif
ifneq (,$(TEST_EXCLUDES))
  TESTOPTS += -x $(sort $(TEST_EXCLUDES))
endif

ifneq (,$(wildcard /$(PREFIX)/bin/localedef))
  SET_LOCPATH = LOCPATH=$(CURDIR)/locales
endif

stamps/stamp-check:
ifeq ($(WITHOUT_CHECK),yes)
	echo "check run disabled for this build" > $(buildd_static)/test_results
else
	: # build locales needed by the testsuite
	rm -rf locales
	mkdir locales
	if which localedef >/dev/null 2>&1; then \
	  sh debian/locale-gen; \
	fi

	@echo ========== test environment ============
	@env
	@echo ========================================
  ifeq (,$(findstring $(DEB_HOST_ARCH), alpha))
	( \
	  echo '#! /bin/sh'; \
	  echo 'set -x'; \
	  echo 'export TERM=$${TERM:-dumb}'; \
	  echo '$(buildd_static)/python $(CURDIR)/debian/script.py test_results '\''make test TESTOPTS="$(filter-out test_gdb,$(TESTOPTS))"'\'; \
	  echo 'echo DONE'; \
	) > $(buildd_debug)/run_tests
	chmod 755 $(buildd_debug)/run_tests
	@echo "BEGIN test debug"
	-cd $(buildd_debug) && time xvfb-run -a -e xvfb-run.log ./run_tests
	@echo "END test debug"
  endif

	( \
	  echo '#! /bin/sh'; \
	  echo 'set -x'; \
	  echo 'export TERM=$${TERM:-dumb}'; \
	  echo 'export $(SET_LOCPATH)'; \
	  echo '$(buildd_static)/python $(CURDIR)/debian/script.py test_results '\''make test EXTRA_CFLAGS="$(EXTRA_OPT_CFLAGS)" TESTOPTS="$(TESTOPTS)"'\'; \
	  echo 'echo DONE'; \
	) > $(buildd_static)/run_tests
	chmod 755 $(buildd_static)/run_tests
	@echo "BEGIN test static"
	-cd $(buildd_static) && time xvfb-run -a -e xvfb-run.log ./run_tests
	@echo "END test static"

	( \
	  echo '#! /bin/sh'; \
	  echo 'set -x'; \
	  echo 'export TERM=$${TERM:-dumb}'; \
	  echo 'export $(SET_LOCPATH)'; \
	  echo '$(buildd_static)/python $(CURDIR)/debian/script.py test_results '\''make test EXTRA_CFLAGS="$(EXTRA_OPT_CFLAGS)" TESTOPTS="$(TESTOPTS)"'\'; \
	  echo 'echo DONE'; \
	) > $(buildd_shared)/run_tests
	chmod 755 $(buildd_shared)/run_tests
	@echo "BEGIN test shared"
	-cd $(buildd_shared) && time xvfb-run -a -e xvfb-run.log ./run_tests
	@echo "END test shared"
endif
	cp -p $(buildd_static)/test_results debian/
	touch stamps/stamp-check

stamps/stamp-pystone:
	@echo "BEGIN pystone static"
	cd $(buildd_static) && ./python ../Lib/test/pystone.py
	cd $(buildd_static) && ./python ../Lib/test/pystone.py
	@echo "END pystone static"
	@echo "BEGIN pystone shared"
	cd $(buildd_shared) \
		&& LD_LIBRARY_PATH=$${LD_LIBRARY_PATH:+$$LD_LIBRARY_PATH:}. ./python ../Lib/test/pystone.py
	cd $(buildd_shared) \
		&& LD_LIBRARY_PATH=$${LD_LIBRARY_PATH:+$$LD_LIBRARY_PATH:}. ./python ../Lib/test/pystone.py
	@echo "END pystone shared"
	@echo "BEGIN pystone debug"
	cd $(buildd_debug) && ./python ../Lib/test/pystone.py
	cd $(buildd_debug) && ./python ../Lib/test/pystone.py
	@echo "END pystone debug"
	touch stamps/stamp-pystone

#ifeq (,$(filter $(DEB_HOST_ARCH), arm armel avr32 hppa mips mipsel m68k))
  pybench_options = -C 2 -n 5 -w 4
#endif

stamps/stamp-pybench:
ifeq ($(WITHOUT_BENCH),yes)
	echo "pybench run disabled for this build" > $(buildd_static)/pybench.log
else
	@echo "BEGIN pybench static"
	cd $(buildd_static) \
	  && time ./python ../Tools/pybench/pybench.py -f run1.pybench $(pybench_options)
	cd $(buildd_static) \
	  && ./python ../Tools/pybench/pybench.py -f run2.pybench -c run1.pybench $(pybench_options)
	@echo "END pybench static"
	@echo "BEGIN pybench shared"
	cd $(buildd_shared) \
	  && LD_LIBRARY_PATH=$${LD_LIBRARY_PATH:+$$LD_LIBRARY_PATH:}. \
		./python ../Tools/pybench/pybench.py -f run1.pybench $(pybench_options)
	cd $(buildd_shared) \
	  && LD_LIBRARY_PATH=$${LD_LIBRARY_PATH:+$$LD_LIBRARY_PATH:}. \
		./python ../Tools/pybench/pybench.py -f run2.pybench -c run1.pybench $(pybench_options)
	@echo "END pybench shared"
	@echo "BEGIN shared/static comparision"
	$(buildd_static)/python Tools/pybench/pybench.py \
	  -s $(buildd_static)/run2.pybench -c $(buildd_shared)/run2.pybench \
	  | tee $(buildd_static)/pybench.log
	@echo "END shared/static comparision"
endif
	touch stamps/stamp-pybench

minimal-test:
	rm -rf mintest
	mkdir -p mintest/lib mintest/dynlib mintest/testlib mintest/all-lib
	cp -p $(buildd_static)/python mintest/
	cp -p $(foreach i,$(MIN_MODS),Lib/$(i).py) \
		mintest/lib/
	cp -a $(foreach i,$(MIN_PACKAGES),Lib/$(i)) \
		mintest/lib/
#	cp -p $(foreach i,$(MIN_EXTS),$(buildd_static)/build/lib*/$(i).so) \
#		mintest/dynlib/
	cp -p Lib/unittest.py mintest/lib/
	cp -pr Lib/test mintest/lib/
	cp -pr Lib mintest/all-lib
	cp -p $(buildd_static)/build/lib*/*.so mintest/all-lib/
	( \
	  echo "import sys"; \
	  echo "sys.path = ["; \
	  echo "    '$(CURDIR)/mintest/lib',"; \
	  echo "    '$(CURDIR)/mintest/dynlib',"; \
	  echo "]"; \
	  cat Lib/test/regrtest.py; \
	) > mintest/lib/test/mintest.py
	cd mintest && ./python -E -S lib/test/mintest.py \
	     -x test_codecencodings_cn test_codecencodings_hk \
		test_codecencodings_jp test_codecencodings_kr \
		test_codecencodings_tw test_codecs test_multibytecodec \

stamps/stamp-doc-html:
	dh_testdir
	#disable $(MAKE) -C Doc html
	touch stamps/stamp-doc-html

build-doc: stamps/stamp-patch stamps/stamp-build-doc
stamps/stamp-build-doc: stamps/stamp-doc-html
	touch stamps/stamp-build-doc

control-file:
	sed -e "s/@PVER@/$(PVER)/g" \
	    -e "s/@VER@/$(VER)/g" \
	    -e "s/@PYSTDDEP@/$(PYSTDDEP)/g" \
	    -e "s/@PRIO@/$(PY_PRIO)/g" \
	    -e "s/@MINPRIO@/$(PY_MINPRIO)/g" \
		debian/control.in > debian/control.tmp
ifeq ($(distribution),Ubuntu)
  ifneq (,$(findstring ubuntu, $(PKGVERSION)))
	m='Ubuntu Core Developers <ubuntu-devel-discuss@lists.ubuntu.com>'; \
	sed -i "/^Maintainer:/s/\(.*\)/Maintainer: $$m\nXSBC-Original-\1/" \
	  debian/control.tmp
  endif
endif
	[ -e debian/control ] \
	  && cmp -s debian/control debian/control.tmp \
	  && rm -f debian/control.tmp && exit 0; \
	  mv debian/control.tmp debian/control



clean: control-file
	dh_testdir
	dh_testroot
	$(MAKE) -f debian/rules unpatch
	rm -rf stamps .pc
	rm -f debian/test_results

	$(MAKE) -C Doc clean
	sed 's/^@/#/' Makefile.pre.in | $(MAKE) -f - srcdir=. distclean
	rm -rf Lib/test/db_home
	rm -rf $(buildd_static) $(buildd_shared) $(buildd_debug) $(buildd_shdebug)
	find -name '*.py[co]' | xargs -r rm -f
	rm -f Lib/lib2to3/*.pickle
	rm -rf locales
	rm -rf $(d)-dbg

	for f in debian/*.in; do \
	  f2=`echo $$f | sed "s,PVER,$(PVER),g;s/@VER@/$(VER)/g;s,\.in$$,,"`; \
	  if [ $$f2 != debian/control ] && [ $$f2 != debian/source.lintian-overrides ]; then \
	    rm -f $$f2; \
	  fi; \
	done
	dh_clean

stamps/stamp-control:
	: # We have to prepare the various control files

	for f in debian/*.in; do \
	  f2=`echo $$f | sed "s,PVER,$(PVER),g;s/@VER@/$(VER)/g;s,\.in$$,,"`; \
	  if [ $$f2 != debian/control ]; then \
	    sed -e "s/@PVER@/$(PVER)/g;s/@VER@/$(VER)/g" \
		    -e "s/@PRIORITY@/$(PRIORITY)/g" \
		    -e "s,@SCRIPTDIR@,/$(scriptdir),g" \
		    -e "s,@INFO@,$(info_docs),g" \
		  <$$f >$$f2; \
	  fi; \
	done
ifeq ($(DEB_HOST_ARCH_BITS),64)
	sed -i 's/\(Py_InitModule4[^@]*\)@/\1_64@/' \
		debian/lib$(PVER).symbols debian/$(PVER)-dbg.symbols
endif

2to3-man:
	help2man --no-info --version-string=$(VER) --no-discard-stderr \
	  --name 'Python2 to Python3 converter' \
	  2to3-$(VER) > debian/2to3-$(VER).1

install: build-arch stamps/stamp-install
stamps/stamp-install: stamps/stamp-build control-file stamps/stamp-control
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	: # make install into tmp and subsequently move the files into
	: # their packages' directories.
	install -d $(d)/$(PREFIX)
ifeq ($(with_interp),static)
	$(MAKE) -C $(buildd_static) install prefix=$(CURDIR)/$(d)/$(PREFIX)
else
	$(MAKE) -C $(buildd_shared) install prefix=$(CURDIR)/$(d)/$(PREFIX)
endif
	-find $(d)/$(PREFIX)/lib/python$(VER) -name '*_failed*.so'
	find $(d)/$(PREFIX)/lib/python$(VER) -name '*_failed*.so' | xargs -r rm -f

	mv $(d)/$(PREFIX)/lib/python$(VER)/site-packages \
		$(d)/$(PREFIX)/lib/python$(VER)/dist-packages

	: # remove files, which are not packaged
	rm -f $(d)/$(PREFIX)/bin/smtpd.py
	rm -rf $(d)/$(PREFIX)/lib/python$(VER)/ctypes/macholib
	rm -f $(d)/$(PREFIX)/lib/pkgconfig/python.pc
	rm -f $(d)/$(PREFIX)/bin/python2{,-config}
	rm -f $(d)/$(PREFIX)/share/man/man1/python2{,-config}.1

	: # fix some file permissions
	chmod a-x $(d)/$(scriptdir)/{fractions,lib-tk/Tix}.py

	: # move manpages to new names
	if [ -d $(d)/$(PREFIX)/man/man1 ]; then \
	    mkdir -p $(d)/$(PREFIX)/share/man; \
	    mv $(d)/$(PREFIX)/man/man1/* $(d)/$(PREFIX)/share/man/man1/; \
	    rm -rf $(d)/$(PREFIX)/man/; \
	fi
	cp -p debian/pydoc.1 $(d)/$(PREFIX)/share/man/man1/pydoc$(VER).1

	: # Symlinks to /$(PREFIX)/bin for some tools
	ln -sf ../lib/python$(VER)/pdb.py $(d)/$(PREFIX)/bin/pdb$(VER)
	cp -p debian/pdb.1 $(d)/$(PREFIX)/share/man/man1/pdb$(VER).1

	: # versioned install only
	rm -f $(d)/$(PREFIX)/bin/python-config

	mv $(d)/$(PREFIX)/bin/2to3 $(d)/$(PREFIX)/bin/2to3-$(VER)
	cp debian/2to3-$(VER).1 $(d)/$(PREFIX)/share/man/man1/2to3-$(VER).1

#	: # remove the bsddb stuff
#	rm -rf $(d)/$(scriptdir)/bsddb
#	rm -f $(d)/$(scriptdir)/lib-dynload/_bsddb.so

	: # Remove version information from the egg-info file
	mv $(d)/$(scriptdir)/lib-dynload/Python-$(VER)*.egg-info \
		$(d)/$(scriptdir)/lib-dynload/Python-$(VER).egg-info

	dh_installdirs -p$(p_lib) \
		$(scriptdir)/config \
		$(PREFIX)/share/doc
	: # install the shared library
	cp -p $(buildd_shared)/libpython$(VER).so.1.0 $(d_lib)/$(PREFIX)/lib/
	ln -sf libpython$(VER).so.1.0 $(d_lib)/$(PREFIX)/lib/libpython$(VER).so.1
	ln -sf ../../libpython$(VER).so.1 \
		$(d_lib)/$(scriptdir)/config/libpython$(VER).so
	ln -sf $(p_base) $(d_lib)/$(PREFIX)/share/doc/$(p_lib)

	ln -sf libpython$(VER).so.1 $(d)/$(PREFIX)/lib/libpython$(VER).so

ifeq ($(with_interp),shared)
	: # install the statically linked runtime
	install -m755 $(buildd_static)/python $(d)/$(PREFIX)/bin/python$(VER)-static
endif

	mv $(d)/$(PREFIX)/bin/pydoc $(d)/$(PREFIX)/bin/pydoc$(VER)
	cp -p Tools/i18n/pygettext.py $(d)/$(PREFIX)/bin/pygettext$(VER)
	cp -p debian/pygettext.1 $(d)/$(PREFIX)/share/man/man1/pygettext$(VER).1

	: # install the Makefile of the shared python build
	sed -e '/^OPT/s,-O3,-O2,' \
	    -e 's,^RUNSHARED *=.*,RUNSHARED=,' \
		build-shared/Makefile > $(d)/$(scriptdir)/config/Makefile

	: # Move the binary and the minimal libraries into $(p_min).
	dh_installdirs -p$(p_min) \
		etc/$(PVER) \
		$(PREFIX)/bin \
		$(PREFIX)/include/$(PVER) \
		$(PREFIX)/share/man/man1 \
		$(scriptdir)/lib-dynload \
		$(scriptdir)/config
	DH_COMPAT=2 dh_movefiles -p$(p_min) --sourcedir=$(d) \
		$(PREFIX)/bin/python$(VER) \
		$(PREFIX)/share/man/man1/python$(VER).1 \
		$(foreach i,$(MIN_MODS),$(scriptdir)/$(i).py) \
		$(foreach i,$(MIN_PACKAGES),$(scriptdir)/$(i)) \
		$(foreach i,$(MIN_ENCODINGS),$(scriptdir)/$(i)) \
		$(scriptdir)/config/Makefile \
		$(PREFIX)/include/$(PVER)/pyconfig.h \
		$(scriptdir)/site.py

#		$(foreach i,$(MIN_EXTS),$(scriptdir)/lib-dynload/$(i).so) \

	: # Install sitecustomize.py.
	cp -p debian/sitecustomize.py $(d_min)/etc/$(PVER)/
	patch --no-backup -d $(d_min)/$(scriptdir) < debian/patches/site-builddir.diff
	dh_link -p$(p_min) /etc/$(PVER)/sitecustomize.py \
		/$(scriptdir)/sitecustomize.py

	: # Move the static library and the header files into $(p_dev).
#	mv $(d)/$(PREFIX)/share/include/python$(VER)/* $(d)/$(PREFIX)/include/python$(VER)/.
#	rm -rf $(d)/$(PREFIX)/share/include
	dh_installdirs -p$(p_dev) \
		$(PREFIX)/share/doc/python$(VER) \
		$(scriptdir) \
		$(scriptdir)/doc/html \
		$(PREFIX)/include \
		$(PREFIX)/share/man/man1 \
		$(PREFIX)/lib
	cp -p Misc/HISTORY Misc/README.valgrind Misc/gdbinit \
		debian/README.maintainers \
		debian/test_results $(buildd_static)/pybench.log \
	    $(d_dev)/$(PREFIX)/share/doc/python$(VER)/

	DH_COMPAT=2 dh_movefiles -p$(p_dev) --sourcedir=$(d) \
		$(PREFIX)/lib/python$(VER)/config \
		$(PREFIX)/include/python$(VER) \
		$(PREFIX)/lib/libpython$(VER).so \
		$(PREFIX)/lib/libpython$(VER).a \
		$(PREFIX)/lib/pkgconfig/python-$(VER).pc \
		$(PREFIX)/bin/python$(VER)-config \
		$(PREFIX)/lib/python$(VER)/distutils/command/wininst-*.exe

	ln -sf $(PVER)/config/libpython$(VER).a \
		$(d_dev)/$(PREFIX)/lib/libpython$(VER).a

	cp -p debian/python-config.1 \
		$(d_dev)/$(PREFIX)/share/man/man1/python$(VER)-config.1
	cp -p $(buildd_shared)/libpython$(VER)-pic.a \
		$(d_dev)/$(PREFIX)/lib/python$(VER)/config/

ifeq ($(with_tk),yes)
	: # Move the Tkinter files into $(p_tk).
	dh_installdirs -p$(p_tk) \
		$(scriptdir) \
		$(PREFIX)/lib/python$(VER)/lib-dynload
	DH_COMPAT=2 dh_movefiles -p$(p_tk) --sourcedir=$(d) \
		$(PREFIX)/lib/python$(VER)/lib-dynload/_tkinter.so
endif

ifeq ($(with_gdbm),yes)
	: # gdbm and dbm modules into $(p_gdbm).
	dh_installdirs -p$(p_gdbm) \
		$(PREFIX)/lib/python$(VER)/lib-dynload
	DH_COMPAT=2 dh_movefiles -p$(p_gdbm) --sourcedir=$(d) \
		$(PREFIX)/lib/python$(VER)/lib-dynload/gdbm.so
endif

#	: # The test framework into $(p_base), regression tests dropped
	DH_COMPAT=2 dh_movefiles -p$(p_base) --sourcedir=$(d) \
		$(scriptdir)/test/{regrtest.py,test_support.py,__init__.py,pystone.py}
	rm -rf $(d)/$(scriptdir)/test
	rm -rf $(d)/$(scriptdir)/ctypes/test
	rm -rf $(d)/$(scriptdir)/bsddb/test
	rm -rf $(d)/$(scriptdir)/email/test
	rm -rf $(d)/$(scriptdir)/json/tests
	rm -rf $(d)/$(scriptdir)/sqlite3/test
	rm -rf $(d)/$(scriptdir)/distutils/tests
	rm -rf $(d)/$(scriptdir)/lib2to3/tests
	rm -rf $(d)/$(scriptdir)/unittest/test
	rm -rf $(d)/$(scriptdir)/lib-tk/test

	: # IDLE
	mv $(d)/$(PREFIX)/bin/idle $(d)/$(PREFIX)/bin/idle-python$(VER)
	rm -f $(d)/$(PREFIX)/lib/python$(VER)/idlelib/idle.bat
	dh_installdirs -p$(p_idle) \
		$(PREFIX)/bin \
		$(PREFIX)/share/man/man1
	DH_COMPAT=2 dh_movefiles -p$(p_idle) --sourcedir=$(d) \
		$(PREFIX)/lib/python$(VER)/idlelib \
		$(PREFIX)/bin/idle-python$(VER)
	cp -p debian/idle-$(PVER).1 $(d_idle)/$(PREFIX)/share/man/man1/

	: # Move the demos and tools into $(p_exam)'s doc directory
	dh_installdirs -p$(p_exam) \
		$(PREFIX)/share/doc/python$(VER)/examples

	cp -rp Demo Tools $(d_exam)/$(PREFIX)/share/doc/python$(VER)/examples/
	rm -rf $(d_exam)/$(PREFIX)/share/doc/python$(VER)/examples/Demo/sgi
	: # IDLE is in its own package:
	rm -rf $(d_exam)/$(PREFIX)/share/doc/python$(VER)/examples/Tools/idle
	: # XXX: We don't need rgb.txt, we'll use our own:
	rm -rf $(d_exam)/$(PREFIX)/share/doc/python$(VER)/examples/Tools/pynche/X

	: # XXX: Some files in upstream Demo and Tools have strange
	: # exec permissions, make lintian glad:
	-chmod 644 $(d_tk)/$(scriptdir)/lib-tk/Tix.py
	-chmod 644 $(d)/$(scriptdir)/runpy.py

	cd $(d_exam)/$(PREFIX)/share/doc/python$(VER)/examples && chmod 644 \
	  Demo/{classes/*.py*,comparisons/patterns} \
	  Demo/{rpc/test,threads/*.py*,md5test/*} \
	  Demo/pdist/{client.py,cmdfw.py,cmptree.py,cvslib.py,cvslock.py,FSProxy.py,mac.py,rcsclient.py,rcslib.py,security.py,server.py,sumtree.py} \
	  Demo/scripts/{morse.py,newslist.doc} \
	  Demo/sockets/{broadcast.py,ftp.py,mcast.py,radio.py} \
	  Demo/tix/{bitmaps/{tix.gif,*x[pb]m*},samples/*.py} \
	  Demo/tkinter/guido/{AttrDialog.py,hanoi.py,hello.py,imagedraw.py,imageview.py,listtree.py,ManPage.py,ShellWindow.py,wish.py} \
	  Tools/scripts/pydocgui.pyw \
	  Tools/scripts/mailerdaemon.py

	: # Replace all '#!' calls to python with $(PY_INTERPRETER)
	: # and make them executable
	for i in `find debian -mindepth 3 -type f ! -name '*.dpatch'`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! $(PY_INTERPRETER)\1,' \
		$$i > $$i.temp; \
	  if cmp --quiet $$i $$i.temp; then \
	    rm -f $$i.temp; \
	  else \
	    mv -f $$i.temp $$i; \
	    chmod 755 $$i; \
	    echo "fixed interpreter: $$i"; \
	  fi; \
	done

	: # Move the docs into $(p_base)'s /$(PREFIX)/share/doc/$(PVER) directory,
	: # all other packages only have a copyright file.
	dh_installdocs -p$(p_base) \
		README Misc/NEWS Misc/ACKS
	mkdir -p $(d_base)/$(PREFIX)/share/doc/$(p_base)/
	ln -sf NEWS.gz $(d_base)/$(PREFIX)/share/doc/$(p_base)/changelog.gz
	dh_installdocs --all -N$(p_base) -N$(p_dev) -N$(p_dbg) -N$(p_lib) debian/README.Debian

	: # IDLE has its own changelogs, docs...
	dh_installchangelogs -p$(p_idle) Lib/idlelib/ChangeLog
	dh_installdocs -p$(p_idle) Lib/idlelib/{NEWS,README,TODO,extend}.txt

	mkdir -p $(d_idle)/$(PREFIX)/share/applications
	cp -p debian/idle.desktop \
		$(d_idle)/$(PREFIX)/share/applications/idle-$(PVER).desktop

	: # those packages have own README.Debian's
	mkdir -p $(d_base)/$(PREFIX)/share/doc/$(PVER)
	mkdir -p $(d_idle)/$(PREFIX)/share/doc/$(p_idle)
	install -m 644 -p debian/README.$(p_base) \
		$(d_base)/$(PREFIX)/share/doc/$(PVER)/README.Debian
	install -m 644 -p debian/README.$(p_idle) \
		$(d_idle)/$(PREFIX)/share/doc/$(p_idle)/README.Debian
ifeq ($(with_tk),yes)
	cp -p debian/README.Tk $(d_tk)/$(PREFIX)/share/doc/$(p_tk)/
endif

	: # The rest goes into $(p_base)
	mkdir -p $(d)/$(PREFIX)/lib/python$(VER)/dist-packages
	(cd $(d) && tar cf - .) | (cd $(d_base) && tar xpf -)
	sh debian/dh_rmemptydirs -p$(p_base)
	rm -f $(d_base)/$(PREFIX)/bin/python

	: # install egg-info for arparse
	install -m 644 debian/argparse.egg-info $(d_base)/$(scriptdir)/

	: # Install menu icon
	dh_installdirs -p$(p_base) $(PREFIX)/share/pixmaps
	cp -p debian/pylogo.xpm $(d_base)/$(PREFIX)/share/pixmaps/$(PVER).xpm

	: # generate binfmt file
	mkdir -p $(d_min)/$(PREFIX)/share/binfmts
	$(buildd_static)/python debian/mkbinfmt.py $(PVER) \
		> $(d_min)/$(PREFIX)/share/binfmts/$(PVER)

	: # desktop entry
	mkdir -p $(d_base)/$(PREFIX)/share/applications
	cp -p debian/$(PVER).desktop \
		$(d_base)/$(PREFIX)/share/applications/$(PVER).desktop

	: # remove some things
	-find debian -name .cvsignore | xargs rm -f
	-find debian -name '*.py[co]' | xargs rm -f

	: # remove empty directories, when all components are in place
	-find debian ! -name lib-dynload -type d -empty -exec rm -f {} \; -print

	: # install debug package
	rm -rf $(d)-dbg
	$(MAKE) -C $(buildd_debug) install DESTDIR=$(CURDIR)/$(d)-dbg
	dh_installdirs -p$(p_dbg) \
		$(PREFIX)/bin \
		$(PREFIX)/share/man/man1 \
		$(scriptdir)/lib-dynload \
		$(PREFIX)/include/$(PVER)_d \
		$(PREFIX)/lib/pkgconfig \
		$(PREFIX)/share/doc/$(p_base)
	cp -p Misc/SpecialBuilds.txt $(d_dbg)/$(PREFIX)/share/doc/$(p_base)/
	cp -p debian/$(PVER)-dbg.README.Debian \
		$(d_dbg)/$(PREFIX)/share/doc/$(p_base)/README.debug
	cp -p $(buildd_debug)/python $(d_dbg)/$(PREFIX)/bin/$(PVER)-dbg
	sed '1s,#!.*python[^ ]*\(.*\),#! $(PY_INTERPRETER)-dbg\1,' \
		$(d)-dbg/$(PREFIX)/bin/$(PVER)-config \
		> $(d_dbg)/$(PREFIX)/bin/$(PVER)-dbg-config
	chmod 755 $(d_dbg)/$(PREFIX)/bin/$(PVER)-dbg-config
	cp -p $(buildd_debug)/build/lib*/*_d.so \
		$(d_dbg)/$(scriptdir)/lib-dynload/
	cp -p $(buildd_shdebug)/libpython$(VER)_d.so.1.0 $(d_dbg)/$(PREFIX)/lib/
	ln -sf libpython$(VER)_d.so.1.0 $(d_dbg)/$(PREFIX)/lib/libpython$(VER)_d.so.1
	ln -sf libpython$(VER)_d.so.1 $(d_dbg)/$(PREFIX)/lib/libpython$(VER)_d.so
	sed -e '/^Libs:/s,-lpython$(VER),-lpython$(VER)_d,' \
	  -e '/^Cflags:/s,python$(VER),python$(VER)_d,' \
	  $(d)-dbg/$(PREFIX)/lib/pkgconfig/python-$(VER).pc \
	  > $(d_dbg)/$(PREFIX)/lib/pkgconfig/python-$(VER)-dbg.pc
ifneq ($(with_gdbm),yes)
	rm -f $(d_dbg)/$(scriptdir)/lib-dynload/gdbm_d.so
	rm -f $(d_dbg)/$(PREFIX)/lib/debug/$(scriptdir)/lib-dynload/gdbm.so
endif
ifneq ($(with_tk),yes)
	rm -f $(d_dbg)/$(scriptdir)/lib-dynload/_tkinter_d.so
	rm -f $(d_dbg)/$(PREFIX)/lib/debug/$(scriptdir)/lib-dynload/_tkinter.so
endif
#	rm -f $(d_dbg)/$(scriptdir)/lib-dynload/_bsddb_d.so

	cp -a $(d)-dbg/$(scriptdir)/config_d $(d_dbg)/$(scriptdir)/
	ln -sf ../../libpython$(VER)_d.so \
		$(d_dbg)/$(scriptdir)/config_d/libpython$(VER)_d.so
	ln -sf libpython$(VER)_d.so \
		$(d_dbg)/$(scriptdir)/config_d/libpython$(VER).so
	ln -sf libpython$(VER)_d.a \
		$(d_dbg)/$(scriptdir)/config_d/libpython$(VER).a

	for i in $(d_dev)/$(PREFIX)/include/$(PVER)/*; do \
	  i=$$(basename $$i); \
	  case $$i in pyconfig.h) continue; esac; \
	  ln -sf ../$(PVER)/$$i $(d_dbg)/$(PREFIX)/include/$(PVER)_d/$$i; \
	done
	cp -p $(buildd_debug)/pyconfig.h $(d_dbg)/$(PREFIX)/include/$(PVER)_d/
	ln -sf $(PVER).1.gz $(d_dbg)/$(PREFIX)/share/man/man1/$(PVER)-dbg.1.gz
	ln -sf $(PVER)-config.1.gz $(d_dbg)/$(PREFIX)/share/man/man1/$(PVER)-dbg-config.1.gz

	for i in debian/*.overrides; do \
	  b=$$(basename $$i .overrides); \
	  install -D -m 644 $$i debian/$$b/$(PREFIX)/share/lintian/overrides/$$b; \
	done

	touch stamps/stamp-install

# Build architecture-independent files here.
binary-indep: build-indep install stamps/stamp-control
	dh_testdir -i
	dh_testroot -i

	: # $(p_doc) package
	dh_installdirs -p$(p_doc) \
		$(PREFIX)/share/doc/$(p_base) \
		$(PREFIX)/share/doc/$(p_doc)
	dh_installdocs -p$(p_doc)
	#cp -a Doc/build/html $(d_doc)/$(PREFIX)/share/doc/$(p_base)/
	#rm -f $(d_doc)/$(PREFIX)/share/doc/$(p_base)/html/_static/jquery.js
	#rm -f $(d_doc)/$(PREFIX)/share/doc/$(p_base)/html/_static/underscore.js
	#dh_link -p$(p_doc) \
	#	/$(PREFIX)/share/doc/$(p_base)/html \
	#		/$(PREFIX)/share/doc/$(p_doc)/html \
	#	/$(PREFIX)/share/javascript/jquery/jquery.js \
	#		/$(PREFIX)/share/doc/$(p_base)/html/_static/jquery.js \
	#	/$(PREFIX)/share/javascript/underscore/jquery.js \
	#		/$(PREFIX)/share/doc/$(p_base)/html/_static/underscore.js

	: # devhelp docs
	#python debian/pyhtml2devhelp.py \
	#	$(d_doc)/$(PREFIX)/share/doc/$(p_base)/html index.html $(VER) \
	#	> $(d_doc)/$(PREFIX)/share/doc/$(p_base)/html/$(PVER).devhelp
	#gzip -9v $(d_doc)/$(PREFIX)/share/doc/$(p_base)/html/$(PVER).devhelp
	#dh_link -p$(p_doc) \
	#	/$(PREFIX)/share/doc/$(p_base)/html /$(PREFIX)/share/devhelp/books/$(PVER)

	dh_installdebconf -i $(dh_args)
	dh_installexamples -i $(dh_args)
	dh_installmenu -i $(dh_args)
	-dh_icons -i $(dh_args) || dh_iconcache -i $(dh_args)
	dh_installchangelogs -i $(dh_args)
	dh_link -i $(dh_args)
	dh_compress -i $(dh_args) -X.py -X.cls -X.css -X.txt -X.json -X.js -Xobjects.inv -Xgdbinit
	dh_fixperms -i $(dh_args)

	: # make python scripts starting with '#!' executable
	for i in `find debian -mindepth 3 -type f ! -name '*.dpatch' ! -perm 755`; do \
	  if head -1 $$i | grep -q '^#!'; then \
	    chmod 755 $$i; \
	    echo "make executable: $$i"; \
	  fi; \
	done
	-find $(d_doc) -name '*.txt' -perm 755 -exec chmod 644 {} \;

	dh_installdeb -i $(dh_args)
	dh_gencontrol -i $(dh_args)
	dh_md5sums -i $(dh_args)
	dh_builddeb -i $(dh_args)

# Build architecture-dependent files here.
binary-arch: build-arch install
	dh_testdir -a
	dh_testroot -a
#	dh_installdebconf -a
	dh_installexamples -a
	dh_installmenu -a
	-dh_icons -a || dh_iconcache -a
#	dh_installmime -a
	dh_installchangelogs -a
	for i in $(p_dev) $(p_dbg) $(p_lib); do \
	  rm -rf debian/$$i/$(PREFIX)/share/doc/$$i; \
	  ln -s $(p_base) debian/$$i/$(PREFIX)/share/doc/$$i; \
	done
	-find debian ! -perm -200 -print -exec chmod +w {} \;
ifneq ($(with_tk),yes)
	rm -f $(d_base)/$(scriptdir)/lib-dynload/_tkinter.so
endif
ifneq ($(with_gdbm),yes)
	rm -f $(d_base)/$(scriptdir)/lib-dynload/gdbm.so
endif
	dh_strip -a -N$(p_dbg) -Xdebug -Xdbg --dbg-package=$(p_dbg)
	mkdir -p $(d_dbg)/$(PREFIX)/lib/debug/$(PREFIX)/bin/
	mkdir -p $(d_dbg)/$(PREFIX)/lib/debug/$(PREFIX)/lib/
	cp Tools/gdb/libpython.py $(d_dbg)/$(PREFIX)/lib/debug/$(PREFIX)/bin/$(PVER)-gdb.py
	ln -sf $(PVER)-gdb.py $(d_dbg)/$(PREFIX)/lib/debug/$(PREFIX)/bin/$(PVER)-dbg-gdb.py
	ln -sf ../bin/$(PVER)-gdb.py \
		$(d_dbg)/$(PREFIX)/lib/debug/$(PREFIX)/lib/lib$(PVER).so.1.0-gdb.py
	ln -sf ../bin/$(PVER)-gdb.py \
		$(d_dbg)/$(PREFIX)/lib/lib$(PVER)_d.so.1.0-gdb.py
	dh_link -a
	dh_compress -a -X.py
	dh_fixperms -a

	: # make python scripts starting with '#!' executable
	for i in `find debian -mindepth 3 -type f ! -name '*.dpatch' ! -perm 755`; do \
	  if head -1 $$i | grep -q '^#!'; then \
	    chmod 755 $$i; \
	    echo "make executable: $$i"; \
	  fi; \
	done

	dh_makeshlibs -p$(p_lib) -V '$(p_lib)'
	dh_makeshlibs -p$(p_dbg) -V '$(p_dbg)'
# don't include the following symbols, found in extensions
# which either can be built as builtin or extension.
	#sed -ri '/^ (_check_|asdl_|fast_save_|init)/d' \
	#  $(d_lib)/DEBIAN/symbols $(d_dbg)/DEBIAN/symbols
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

# rules to patch the unpacked files in the source directory
# ---------------------------------------------------------------------------
# various rules to unpack addons and (un)apply patches.
# 	- patch / apply-patches
#	- unpatch / reverse-patches

patchdir	= debian/patches

glibc_version := $(shell dpkg -s locales | awk '/^Version:/ {print $$2}')
broken_utimes := $(shell dpkg --compare-versions $(glibc_version) lt 2.3.5 && echo yes || echo no)

$(patchdir)/series: $(patchdir)/series.in
	cpp -E \
	  -D$(distribution) \
	  $(if $(filter $(broken_utimes),yes),-DBROKEN_UTIMES) \
	  $(if $(filter $(with_fpectl),yes),-DWITH_FPECTL) \
	  -Darch_os_$(DEB_HOST_ARCH_OS) -Darch_$(DEB_HOST_ARCH) \
	  -o - $(patchdir)/series.in \
	  | egrep -v '^(#.*|$$)' > $(patchdir)/series

patch: stamps/stamp-patch
stamps/stamp-patch: $(patchdir)/series
	dh_testdir
	QUILT_PATCHES=$(patchdir) quilt push -a || test $$? = 2
	rm -rf autom4te.cache configure
	#autoconf
	cp backup_configure configure

	mkdir -p stamps
	echo ""; echo "Patches applied in this version:" > stamps/pxx
	for i in $$(cat $(patchdir)/series); do \
	  echo ""; echo "$$i:"; \
	  sed -n 's/^# *DP: */  /p' $(patchdir)/$$i; \
	done >> stamps/pxx
	mv stamps/pxx $@

reverse-patches: unpatch
unpatch:
	QUILT_PATCHES=$(patchdir) quilt pop -a -R || test $$? = 2
	rm -f stamps/stamp-patch $(patchdir)/series
	rm -rf configure autom4te.cache

update-patches: $(patchdir)/series
	export QUILT_PATCHES=$(patchdir); \
	export QUILT_REFRESH_ARGS="--no-timestamps --no-index -pab"; \
	export QUILT_DIFF_ARGS="--no-timestamps --no-index -pab"; \
	while quilt push; do quilt refresh; done

binary: binary-indep binary-arch

.PHONY: control-file configure build clean binary-indep binary-arch binary install

# Local Variables:
# mode: makefile
# end:
