#!/usr/bin/make -f
# debian/rules file for building the Debian GNU/Linux package bzip2.
# Copyright (C) 1999, 2000, 2001, 2002 Philippe Troin
# Copyright (C) 2004-2007 Anibal Monsalve Salazar <anibal@debian.org>
# Copyright 2014 Canonical Ltd.

SHELL = /bin/bash

DEB_BUILD_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_ARCH		:= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS	:= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_MULTIARCH      ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
        CROSS=CC=$(DEB_HOST_GNU_TYPE)-gcc
        CC=$(DEB_HOST_GNU_TYPE)-gcc
else
        CROSS=CC=gcc
        CC=gcc
endif

DEBCFLAGS:=`dpkg-buildflags --get CFLAGS`
DEBCFLAGS +=`dpkg-buildflags --get CPPFLAGS`
WGET=wget

DEBVERSION:=$(shell head -n 1 debian/changelog \
		    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
UPVERSION:=$(shell echo $(DEBVERSION) | sed -e 's/-[0-9.]*$$//')

ifneq (,$(findstring /$(DEB_HOST_ARCH)/,/i386/powerpc/sparc/s390/))
	build64-stamp := build64-stamp
	CC64=$(CC) -m64
endif
ifeq ($(DEB_HOST_ARCH),i386)
	CC64 += -march=x86-64 -mtune=x86-64
endif

ifneq (,$(findstring /$(DEB_HOST_ARCH)/,/amd64/ppc64/))
	build32-stamp := build32-stamp
	lib32 := usr/lib32
endif

%:
	dh $@

build-arch: build-stamp $(build32-stamp) $(build64-stamp)
build-stamp:
	# Add here commands to compile the package.
	dh_auto_build -- $(CROSS) DEBCFLAGS="$(DEBCFLAGS)"
	touch build-stamp

build-indep: bzip2.info
bzip2.info: manual.texi
	makeinfo -o bzip2.info manual.texi

override_dh_auto_test:
	dh_auto_test -- DEBCFLAGS="$(DEBCFLAGS)"

# requires: docbook-xml docbook2x
manual.texi: manual.xml
	docbook2x-texi --to-stdout manual.xml | \
	awk '/^@direntry/,/^@end direntry/ {if ($$0 !~ /@/) {print "* Bzip2: (bzip2).               A program and library for data compression."; next}} {print}' > manual.texi

build32: build32-stamp
build32-stamp:
	$(testdir)
	rm -rf 32
	mkdir 32
	cp -p *.h *.c Makefile 32/
	$(MAKE) -C 32 "$(CROSS) -m32" DEBCFLAGS="$(DEBCFLAGS)"
	touch build32-stamp

build64: build64-stamp
build64-stamp:
	$(testdir)
	rm -rf 64
	mkdir 64
	cp -p *.h *.c Makefile 64/
	$(MAKE) -C 64 CC="$(CC64)" DEBCFLAGS="$(DEBCFLAGS)"
	touch build64-stamp

override_dh_auto_clean:
	rm -f build*-stamp
	dh_auto_clean
	rm -rf 32 64
	rm -rf *.sho libbz2.so*
	rm -f bzip2.info*

override_dh_link:
	dh_link
ifneq (,$(filter $(shell dh_listpackages), bzip2-doc))
	for i in debian/bzip2-doc/usr/share/doc/bzip2/*.{ps,texi,html}; do \
	  b=$$(basename $$i); \
	  ln -sf ../bzip2/$$b debian/bzip2-doc/usr/share/doc/bzip2-doc/$$b; \
	done
endif
ifneq (,$(filter $(shell dh_listpackages), bzip2))
	( cd debian/bzip2/usr/share/man/man1 && \
	  for i in bunzip2 bzcat bzip2recover; do \
	    ln -s bzip2.1.gz $$i.1.gz; \
	  done )
endif

override_dh_auto_install:
	dh_auto_install -- PREFIX=$(CURDIR)/debian/tmp
	# Development package
	rm debian/tmp/lib/libbz2.so
	install -d debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)

	# This isn't the soname, and nothing should be looking for this file,
	# but leave it alone for now
	ln -s libbz2.so.1.0.4 debian/tmp/lib/libbz2.so.1

	ln -s /lib/$(DEB_HOST_MULTIARCH)/libbz2.so.1.0 debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libbz2.so
	ln -sf bzdiff debian/tmp/bin/bzcmp
	ln -sf bzgrep debian/tmp/bin/bzegrep
	ln -sf bzgrep debian/tmp/bin/bzfgrep
	ln -sf bzmore debian/tmp/bin/bzless
	install -d debian/tmp/usr/share
	mv debian/tmp/man debian/tmp/usr/share/man

ifneq (,$(build32-stamp))
	install -d debian/tmp/$(lib32)
	cp -a 32/libbz2.so* 32/libbz2.a debian/tmp/$(lib32)/
	# This isn't the soname, and nothing should be looking for this file,
	# but leave it alone for now
	ln -s libbz2.so.1.0.4 debian/tmp/$(lib32)/libbz2.so.1
endif
ifneq (,$(build64-stamp))
	install -d debian/tmp/usr/lib64
	cp -a 64/libbz2.so* 64/libbz2.a debian/tmp/usr/lib64/
	# This isn't the soname, and nothing should be looking for this file,
	# but leave it alone for now
	ln -s libbz2.so.1.0.4 debian/tmp/usr/lib64/libbz2.so.1
endif

override_dh_gencontrol:
ifeq ($(DEB_HOST_ARCH),amd64)
	echo 'dev:Depends=libc6-dev-i386' >> debian/lib32bz2-dev.substvars
endif
ifeq ($(DEB_HOST_ARCH),ppc64)
	echo 'dev:Depends=libc6-dev-powerpc' >> debian/lib32bz2-dev.substvars
endif
ifeq ($(DEB_HOST_ARCH),i386)
	echo 'dev:Depends=libc6-dev-amd64' > debian/lib64bz2-dev.substvars
endif
ifeq ($(DEB_HOST_ARCH),powerpc)
	echo 'dev:Depends=libc6-dev-ppc64' > debian/lib64bz2-dev.substvars
endif
ifeq ($(DEB_HOST_ARCH),s390)
	echo 'dev:Depends=libc6-dev-s390x' > debian/lib64bz2-dev.substvars
endif
ifeq ($(DEB_HOST_ARCH),sparc)
	echo 'dev:Depends=libc6-dev-sparc64' > debian/lib64bz2-dev.substvars
endif
	dh_gencontrol

override_dh_install:
	dh_install --fail-missing

override_dh_installdocs:
	cat debian/copyright.in LICENSE > debian/copyright
	dh_installdocs -plibbz2-dev --link-doc=libbz2-1.0
	dh_installdocs -plib32bz2-dev --link-doc=lib32bz2-1.0
	dh_installdocs -plib64bz2-dev --link-doc=lib64bz2-1.0
	dh_installdocs -Nlibbz2-dev -Nlib32bz2-dev -Nlib64bz2-dev

print-version:
	@@echo "Debian version:          $(DEBVERSION)"
	@@echo "Upstream version:        $(UPVERSION)"

get-orig-source:
	$(WGET) -O bzip2_$(UPVERSION).orig.tar.gz \
	  ftp://sourceware.cygnus.com/pub/bzip2/v100/bzip2-$(UPVERSION).tar.gz

.PHONY: print-version get-orig-source
