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

export SHELL = /bin/bash

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

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

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

distribution := $(shell dpkg-vendor --query Vendor)
distrelease  := $(shell lsb_release -cs)

export VER=2.7
export NVER=2.8
export PVER=python2.7

PREVVER	:= $(shell awk '/^python/ && NR > 1 {print substr($$2,2,length($$2)-2); exit}' debian/changelog)
# first version with Multi-Arch: allowed
PREVVER := 2.7.15~rc1-1~

# not yet allowed
ma_allowed	= yes

ifeq (,$(filter $(distrelease),lenny etch squeeze wheezy lucid maverick natty oneiric precise quantal raring saucy trusty))
  bd_i586 = dpkg-dev (>= 1.17.11), python2.7:any (>= 2.7.11-11)
endif


PWD		:= $(shell pwd)

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

rst2html	= rst2html

with_doc	= yes
ifneq ($(with_doc),yes)
  NOPKGS	= -Npython-doc
endif

build: stamp-build
build-arch: stamp-build
build-indep: stamp-build
stamp-build:
	touch stamp-build

stamp-doc-policy:
	xsltproc --nonet --novalid debian/html.xsl debian/python-policy.dbk \
	    | w3m -o display_charset=UTF-8 -cols 70 -dump -no-graph -T text/html > python-policy.txt
	mv -f python-policy.txt debian/
	rm -rf debian/python-policy.html
	mkdir python-policy.html
	( cd python-policy.html; xsltproc --nonet --novalid ../debian/chunk.xsl ../debian/python-policy.dbk )
	mv -f python-policy.html debian/
	make -C pydist
	touch stamp-doc-policy

stamp-doc: stamp-doc-policy
	touch stamp-doc

control-file:
	sed -e "s/@PVER@/$(PVER)/g" \
	    -e "s/@NVER@/$(NVER)/g" \
	    -e "s/@VER@/$(VER)/g" \
	    -e "s/@PREVVER@/$(PREVVER)/g" \
	    -e "s/@bd_i586@/$(bd_i586)/g" \
		<debian/control.in \
	    $(if $(ma_allowed),,| grep -v '^Multi-Arch: allowed$$') \
		>debian/control.tmp
ifeq ($(distribution),Ubuntu)
  ifneq (,$(findstring ubuntu, $(PKGVERSION)))
	m='Ubuntu 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

faqs = index general programming library extending windows gui installed
get-faq:
	# Note: This rule is broken due to web site reorg at python.org
	rm -rf faq
	mkdir faq
	cd faq && \
	  wget -nv $(foreach p,$(faqs),http://python.org/doc/faq/$(p).ht)
	sed 's,\<\([a-zA-Z0-9]*\.html\)\>,faq/\1,' faq/index.ht \
		| awk '/^Translations$$/ {exit} {print}' > faq/FAQ.ht
	( \
	  echo ''; \
	  echo 'This is a local copy of the online FAQ located at'; \
	  echo 'http://python.org/doc/faq.'; \
	  echo "The copy was fetched and generated in `date '+%B %Y'`."; \
	) >> faq/FAQ.ht
	rm -f faq/index.ht

make-faq:
	set -e; \
	cd faq && \
	for p in *.ht; do \
	  p=`basename $$p .ht`; \
	  echo $(rst2html) $$p.ht $$p.html; \
	  awk 'NR > 2' $$p.ht > $$p.ht2; \
	  $(rst2html) $$p.ht2 $$p.html; \
	  rm -f $$p.ht2; \
	done

MANPAGES ?= dh_python2.1 pycompile.1 pyclean.1

%.1: %.rst
	rst2man $< > $@

# Manually run this rule if there is a need to update the dh_python2 man pages
make-man: $(MANPAGES)
	cd pydist && \
	rst2html README.PyDist > README.PyDist.html

clean: control-file
	dh_testdir
	dh_testroot
	rm -f stamp-*
	rm -f patch-stamp*

	rm -f debian/python-policy.txt
	rm -rf debian/python-policy.html

	set -e;\
	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 \
	        rm -f $$f2; \
	    fi; \
	done
	rm -f debian/*.py[co]
	make clean
	dh_clean

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

	set -e;\
	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" \
		  <$$f >$$f2; \
	    fi; \
	done

install: build stamp-dh_python stamp-install
stamp-install: stamp-build control-file stamp-control
	dh_testdir
	dh_testroot
	dh_installdirs usr/share/doc/python/faq
	dh_install

	set -e; \
	cd faq && \
	for p in *.ht; do \
	  p=`basename $$p .ht`; \
	  cp $$p.html ../debian/python/usr/share/doc/python/faq/; \
	done
	mv debian/python/usr/share/doc/python/faq/FAQ.html \
		debian/python/usr/share/doc/python/

	touch stamp-install

stamp-dh_python:
	dh_testdir
	dh_testroot
	dh_installdirs
	-make check_versions
	DESTDIR=debian/python PREFIX=/usr make install-dev
	DESTDIR=debian/python-minimal PREFIX=/usr make install-runtime
	# disabled by default, run manually if you want to update it
	# (requires apt-file and network connection)
	#make -C pydist dist_fallback
	touch $@

# Build architecture-independent files here.
binary-indep: build install stamp-doc
	dh_testdir -i
	dh_testroot -i
	dh_installman -i

ifeq ($(with_doc),yes)
	dh_installdocs -ppython-doc debian/README.Debian
	dh_installchangelogs -ppython-doc
	mkdir -p debian/python-doc/usr/share/doc/python
	ln -sf ../python$(VER)-doc/html \
		debian/python-doc/usr/share/doc/python/html
	mkdir -p debian/python-doc/usr/share/doc/python-doc
	ln -sf ../python$(VER)-doc/html \
		debian/python-doc/usr/share/doc/python-doc/html
endif

#	dh_installdebconf -i $(NOPKGS)
	dh_lintian -i
	dh_installdocs -i $(NOPKGS) --all debian/README.Debian
	dh_installchangelogs -i $(NOPKGS)

	dh_compress -i $(NOPKGS)
	dh_fixperms -i $(NOPKGS)
	dh_installdeb -i $(NOPKGS)
	dh_gencontrol -i $(NOPKGS)
	dh_md5sums -i $(NOPKGS)
	dh_builddeb -i $(NOPKGS)

# Build architecture-dependent files here.
binary-arch: build install stamp-doc
	dh_testdir -a
	dh_testroot -a
	dh_installman -a

	: # provide the python and python.1 defaults
	mkdir -p debian/python-minimal/usr/bin
	ln -sf python$(VER) debian/python-minimal/usr/bin/python
	ln -sf python$(VER) debian/python-minimal/usr/bin/python2
	mkdir -p debian/python-minimal/usr/share/man/man1
	ln -sf python$(VER).1.gz \
		debian/python-minimal/usr/share/man/man1/python.1.gz
	ln -sf python$(VER).1.gz \
		debian/python-minimal/usr/share/man/man1/python2.1.gz

	: # 
	mkdir -p debian/python-minimal/usr/share/python
	cp -p debian/debian_defaults \
		debian/python-minimal/usr/share/python/

	install -m 755 debian/pyversions.py \
		debian/python-minimal/usr/share/python/
	install -m 644 debian/pyversions.1 \
		debian/python-minimal/usr/share/man/man1/
	dh_link -ppython-minimal /usr/share/python/pyversions.py /usr/bin/pyversions

#	install -m 755 debian/pycompile \
#		debian/python-minimal/usr/bin/
#	install -m 755 debian/pyclean \
#		debian/python-minimal/usr/bin/

#	mkdir debian/python-minimal/usr/share/python-central
#	cp -p debian/python.pycentral debian/python-minimal/usr/share/python-central/

	mkdir -p debian/python/usr/bin
	ln -sf pydoc$(VER) debian/python/usr/bin/pydoc
	ln -sf pygettext$(VER) debian/python/usr/bin/pygettext
	ln -sf pdb$(VER) debian/python/usr/bin/pdb

	mkdir -p debian/python/usr/share/python
	install -m 644 debian/python.mk \
		debian/python/usr/share/python/

	mkdir -p debian/python/usr/share/man/man1
	ln -sf pydoc$(VER).1.gz \
		debian/python/usr/share/man/man1/pydoc.1.gz
	ln -sf pygettext$(VER).1.gz \
		debian/python/usr/share/man/man1/pygettext.1.gz
	ln -sf pdb$(VER).1.gz \
		debian/python/usr/share/man/man1/pdb.1.gz

	mkdir -p debian/python/usr/share/doc/python
	cp -a debian/python-policy.{html,dbk,txt} \
		debian/python/usr/share/doc/python/

	mkdir -p debian/python/usr/share/pixmaps
	ln -sf python$(VER).xpm debian/python/usr/share/pixmaps/python.xpm

	: # add symlinks to policy files
	mkdir -p debian/python/usr/share/doc/python$(VER)
	for ext in html dbk.gz txt.gz; do \
	  ln -sf ../python/python-policy.$$ext \
		debian/python/usr/share/doc/python$(VER)/python-policy.$$ext; \
	done

	mkdir -p debian/python/usr/share/apps/konsole
	cp -p debian/python.desktop debian/python/usr/share/apps/konsole/

	: # provide the python-config default
	mkdir -p debian/python-dev/usr/bin
	ln -sf python$(VER)-config debian/python-dev/usr/bin/python-config
	ln -sf python$(VER)-config debian/python-dev/usr/bin/python2-config
	mkdir -p debian/python-dev/usr/share/man/man1
	ln -sf python$(VER)-config.1.gz \
		debian/python-dev/usr/share/man/man1/python-config.1.gz
	ln -sf python$(VER)-config.1.gz \
		debian/python-dev/usr/share/man/man1/python2-config.1.gz

	: # provide pkgconfig defaults
	mkdir -p debian/python-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	ln -sf python-$(VER).pc debian/python-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/python.pc
	ln -sf python.pc debian/python-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/python2.pc
	mkdir -p debian/python-dbg/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
	ln -sf python-$(VER)-dbg.pc debian/python-dbg/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/python-dbg.pc
	ln -sf python-dbg.pc debian/python-dbg/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/python2-dbg.pc

	: # provide the python-dbg and python-dbg.1 defaults
	mkdir -p debian/python-dbg/usr/bin
	ln -sf python$(VER)-dbg debian/python-dbg/usr/bin/python-dbg
	ln -sf python$(VER)-dbg debian/python-dbg/usr/bin/python2-dbg
	ln -sf python$(VER)-dbg-config debian/python-dbg/usr/bin/python-dbg-config
	ln -sf python$(VER)-dbg-config debian/python-dbg/usr/bin/python2-dbg-config
	mkdir -p debian/python-dbg/usr/share/man/man1
	ln -sf python$(VER)-dbg.1.gz \
		debian/python-dbg/usr/share/man/man1/python-dbg.1.gz
	ln -sf python$(VER)-dbg.1.gz \
		debian/python-dbg/usr/share/man/man1/python2-dbg.1.gz
	ln -sf python$(VER)-dbg-config.1.gz \
		debian/python-dbg/usr/share/man/man1/python-dbg-config.1.gz
	ln -sf python$(VER)-dbg-config.1.gz \
		debian/python-dbg/usr/share/man/man1/python2-dbg-config.1.gz
	mkdir -p debian/python-dbg/usr/share/doc/python
	ln -sf ../python$(VER)/SpecialBuilds.txt.gz \
		debian/python-dbg/usr/share/doc/python/SpecialBuilds.txt.gz
	ln -sf ../python$(VER)/README.debug \
		debian/python-dbg/usr/share/doc/python/README.debug

	mkdir -p debian/python/usr/lib/valgrind
	cp -p debian/valgrind-python.supp \
		debian/python/usr/lib/valgrind/python.supp

	: # provide the DEB_HOST_GNU_TYPE/DEB_HOST_MULTIRACH python-config defaults
	mkdir -p debian/libpython-dev/usr/bin
	ln -sf $(DEB_HOST_MULTIARCH)-python$(VER)-config \
		debian/libpython-dev/usr/bin/$(DEB_HOST_MULTIARCH)-python-config
	mkdir -p debian/libpython-dev/usr/share/man/man1
	ln -sf $(DEB_HOST_MULTIARCH)-python$(VER)-config.1.gz \
		debian/libpython-dev/usr/share/man/man1/$(DEB_HOST_MULTIARCH)-python-config.1.gz
ifneq ($(DEB_HOST_MULTIARCH),$(DEB_HOST_GNU_TYPE))
	ln -sf $(DEB_HOST_GNU_TYPE)-python$(VER)-config \
		debian/libpython-dev/usr/bin/$(DEB_HOST_GNU_TYPE)-python-config
	ln -sf $(DEB_HOST_GNU_TYPE)-python$(VER)-config.1.gz \
		debian/libpython-dev/usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-python-config.1.gz
endif

	mkdir -p debian/libpython-dbg/usr/bin
	ln -sf $(DEB_HOST_MULTIARCH)-python$(VER)-dbg-config \
		debian/libpython-dbg/usr/bin/$(DEB_HOST_MULTIARCH)-python-dbg-config
	mkdir -p debian/libpython-dbg/usr/share/man/man1
	ln -sf $(DEB_HOST_MULTIARCH)-python$(VER)-dbg-config.1.gz \
		debian/libpython-dbg/usr/share/man/man1/$(DEB_HOST_MULTIARCH)-python-dbg-config.1.gz
ifneq ($(DEB_HOST_MULTIARCH),$(DEB_HOST_GNU_TYPE))
	ln -sf $(DEB_HOST_GNU_TYPE)-python$(VER)-dbg-config \
		debian/libpython-dbg/usr/bin/$(DEB_HOST_GNU_TYPE)-python-dbg-config
	ln -sf $(DEB_HOST_GNU_TYPE)-python$(VER)-dbg-config.1.gz \
		debian/libpython-dbg/usr/share/man/man1/$(DEB_HOST_GNU_TYPE)-python-dbg-config.1.gz
endif

	dh_lintian -a
	dh_installdocs -a $(NOPKGS) --all debian/README.Debian
	dh_installchangelogs -a $(NOPKGS)

	for p in all all-dev all-dbg dbg dev; do \
	  p=python-$$p; \
	  rm -rf debian/$$p/usr/share/doc/$$p; \
	  ln -sf python debian/$$p/usr/share/doc/$$p; \
	done
	rm -rf debian/libpython-all-dev/usr/share/doc/libpython-all-dev
	ln -sf libpython-dev debian/libpython-all-dev/usr/share/doc/libpython-all-dev
	rm -rf debian/libpython-all-dbg/usr/share/doc/libpython-all-dbg
	ln -sf libpython-dbg debian/libpython-all-dbg/usr/share/doc/libpython-all-dbg

	dh_compress -a $(NOPKGS)
	dh_fixperms -a $(NOPKGS)
	dh_installdeb -a $(NOPKGS)
	dh_gencontrol -a $(NOPKGS)
	dh_md5sums -a $(NOPKGS)
	dh_builddeb -a $(NOPKGS)

# Build architecture-dependent files here.
binary-arch: build install
# nothing to do

binary: binary-indep binary-arch

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

# Local Variables:
# mode: makefile
# end:
