#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# Some lines taken from debmake, by Christoph Lameter.

# build in verbose mode by default to make it easy to diangose issues
export NOISY=1

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# FOR AUTOCONF 2.52 AND NEWER ONLY
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

# See below
-include build/environment.mak

ifneq (,$(shell which dpkg-buildflags))
  export CXXFLAGS = $(shell dpkg-buildflags --get CXXFLAGS)
  export LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
  export CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)
else
  ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    export CXXFLAGS = -O0 -g -Wall
  else
    export CXXFLAGS = -O2 -g -Wall
  endif
endif

# Default rule
build:

PKG=apt
DEB_BUILD_PROG:=debuild --preserve-envvar PATH --preserve-envvar CCACHE_DIR -us -uc $(DEB_BUILD_PROG_OPTS)

# Determine the build directory to use
BASE=.
ifdef BUILD
BUILD_POSSIBLE := $(BUILD) $(BASE)/$(BUILD)
else
BUILD_POSSIBLE := $(BASE) $(BASE)/build-$(shell uname --machine) $(BASE)/build
endif
BUILDX:= $(foreach i,$(BUILD_POSSIBLE),$(wildcard $(i)/environment.mak*))
BUILDX:= $(patsubst %/,%,$(firstword $(dir $(BUILDX))))
override BLD := $(BUILDX)

ifeq ($(words $(BLD)),0)
override BLD := ./build
endif

# APT Programs in apt-utils
APT_UTILS=ftparchive sortpkgs extracttemplates

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

# Find the libapt-pkg major version for use in other control files
include buildlib/libversion.mak

# Determine which library package names to use
LIBAPT_PKG=libapt-pkg$(LIBAPTPKG_MAJOR)
LIBAPT_INST=libapt-inst$(LIBAPTINST_MAJOR)

# do not fail as we are just experimenting with symbol files for now
export DPKG_GENSYMBOLS_CHECK_LEVEL=0

build-binary: build/build-binary-stamp
build-debiandoc: build/build-debiandoc-stamp
build-manpages: build/build-manpages-stamp

# Note that this is unconditionally done first as part of loading environment.mak
# The true is needed to force make to reload environment.mak after running
# configure-stamp. Otherwise we can get stale or invalid, or missing config data.
build/environment.mak: build/configure-stamp
	@true

configure: configure.ac
build/configure-stamp: configure buildlib/config.sub buildlib/config.guess
	dh_testdir
	-mkdir build
	cp COPYING debian/copyright
	cd build && CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" CPPFLAGS="$(CPPFLAGS)" ../configure $(confflags)
	touch $@

build/build-binary-stamp: build/configure-stamp
	# Add here commands to compile the package.
	$(MAKE) binary
	# compat symlink for the locale split
	mkdir -p build/usr/share 
	cd build/usr/share && ln -f -s ../../locale .
	# compile and run tests
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	$(MAKE) test
else
	@echo "Tests DISABLED"
endif
	touch $@

build/build-debiandoc-stamp: build/configure-stamp
	# Add here commands to compile the package.
	$(MAKE) debiandoc
	touch $@

build/build-manpages-stamp: build/configure-stamp
	# Add here commands to compile the package.
	$(MAKE) manpages
	touch $@

clean:
	dh_testdir
	dh_clean

	[ ! -f Makefile ] || $(MAKE) clean distclean
	rm -rf build
	rm -f debian/$(LIBAPT_PKG).install debian/$(LIBAPT_INST).install \
	      debian/libapt-pkg-dev.install debian/apt.install
	test ! -L buildlib/config.guess || rm -f buildlib/config.guess
	test ! -L buildlib/config.sub || rm -f buildlib/config.sub

debian/%.install: debian/%.install.in
	sed 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' $< > $@

# Build architecture-independent files here.
libapt-pkg-doc: build-debiandoc
	dh_testdir -p$@
	dh_testroot -p$@
	dh_prep -p$@
	dh_installdirs -p$@
#
# libapt-pkg-doc install
#
	rm -f $(BLD)/doc/doxygen/html/*.map $(BLD)/doc/doxygen/html/*.md5
	dh_installdocs -p$@ $(BLD)/docs/design* \
			    $(BLD)/docs/dpkg-tech* \
			    $(BLD)/docs/files* \
			    $(BLD)/docs/method* \
			    doc/libapt-pkg2_to_3.txt \
			    doc/style.txt \
			    $(BLD)/doc/doxygen/html
	dh_installexamples -p$@

	dh_installchangelogs -p$@
	dh_strip -p$@
	dh_compress -p$@ -X.xhtml
	dh_fixperms -p$@
	dh_installdeb -p$@
	dh_gencontrol -p$@
	dh_md5sums -p$@
	dh_builddeb -p$@

apt-doc: build-debiandoc
	dh_testdir -p$@
	dh_testroot -p$@
	dh_prep -p$@
#
# apt-doc install
#
	# Copy the guides
	dh_installdocs -p$@ $(BLD)/docs/guide*.text \
			    $(BLD)/docs/guide*.html \
			    $(BLD)/docs/offline*.text \
			    $(BLD)/docs/offline*.html
	dh_installchangelogs -p$@
	dh_compress -p$@
	dh_fixperms -p$@
	dh_installdeb -p$@
	dh_gencontrol -p$@
	dh_md5sums -p$@
	dh_builddeb -p$@



# Build architecture-dependent files here.

apt_MANPAGES = apt apt-cache apt-cdrom apt-config apt-get apt-key apt-mark apt-secure apt apt.conf apt_preferences sources.list
apt: build-binary build-manpages debian/apt.install
	dh_testdir -p$@
	dh_testroot -p$@
	dh_prep -p$@
	dh_installdirs -p$@
#
# apt install
#
	cp debian/apt.conf.autoremove debian/$@/etc/apt/apt.conf.d/01autoremove
	cp debian/apt.auto-removal.sh debian/$@/etc/kernel/postinst.d/apt-auto-removal
	chmod 755 debian/$@/etc/kernel/postinst.d/apt-auto-removal
	# install bash completion
	cp share/bash-completions/* debian/$@/usr/share/bash-completion/completions/
	# install vendor specific apt confs
	find -L vendor/current -name 'apt.conf-*' | while read conf; do cp "$${conf}" "debian/$@/etc/apt/apt.conf.d/$${conf#*-}"; done

	# make rosetta happy and remove pot files in po/ (but leave stuff
	# in po/domains/* untouched) and cp *.po into each domain dir
	rm -f build/po/*.pot
	rm -f po/*.pot

	dh_install -p$@ --sourcedir=$(BLD)

	# Remove the bits that are in apt-utils
	rm $(addprefix debian/$@/usr/bin/apt-,$(APT_UTILS) dump-solver internal-solver)

	# https has its own package
	rm debian/$@/usr/lib/apt/methods/https

	# move the mirror failure script in place
	#mv debian/$@/usr/bin/apt-report-mirror-failure \
	#   debian/$@/usr/lib/apt/apt-report-mirror-failure \

	# move the apt-helper in place
	mv debian/$@/usr/bin/apt-helper debian/$@/usr/lib/apt/apt-helper 

	dh_bugfiles -p$@
	dh_lintian -p$@
	dh_installexamples -p$@ $(BLD)/docs/examples/*
	dh_installman -p$@ $(wildcard $(patsubst %,doc/en/%.[158],$(apt_MANPAGES)) $(patsubst %,doc/*/%.*.[158],$(apt_MANPAGES)))
	dh_installcron -p$@
	dh_installdocs -p$@
	dh_installchangelogs -p$@
	dh_installlogrotate -p$@
	dh_strip -p$@
	dh_compress -p$@
	dh_fixperms -p$@
	dh_makeshlibs -p$@
	dh_installdeb -p$@
	dh_shlibdeps -p$@
	dh_gencontrol -p$@ -- -Vapt:keyring="$(shell ./vendor/getinfo keyring-package)"
	dh_md5sums -p$@
	dh_builddeb -p$@

libapt-pkg-dev: build-binary debian/libapt-pkg-dev.install
	dh_testdir -p$@
	dh_testroot -p$@
	dh_prep -p$@
	dh_installdirs -p$@
#
# libapt-pkg-dev install
#
	dh_install -p$@ --sourcedir=$(BLD)
	dh_installdocs -p$@
	dh_installchangelogs -p$@
	dh_strip -p$@
	dh_compress -p$@
	dh_fixperms -p$@
	dh_installdeb -p$@
	dh_gencontrol -p$@ -- -Vlibapt-pkg-name=$(LIBAPT_PKG) -Vlibapt-inst-name=$(LIBAPT_INST)
	dh_md5sums -p$@
	dh_builddeb -p$@

apt-utils_MANPAGES = apt-sortpkgs apt-ftparchive apt-extracttemplates
apt-utils: build-binary build-manpages
	dh_testdir -p$@
	dh_testroot -p$@
	dh_prep -p$@
	dh_installdirs -p$@

	cp $(addprefix $(BLD)/bin/apt-,$(APT_UTILS)) debian/$@/usr/bin/
	cp $(BLD)/bin/apt-dump-solver debian/$@/usr/lib/apt/solvers/dump
	cp $(BLD)/bin/apt-internal-solver debian/$@/usr/lib/apt/solvers/apt

	dh_install -p$@ --sourcedir=$(BLD)
	dh_link -p$@
	dh_installdocs -p$@
	dh_installexamples -p$@

	# Install the man pages..
	dh_installman -p$@ $(wildcard $(patsubst %,doc/en/%.[158],$(apt-utils_MANPAGES)) $(patsubst %,doc/*/%.*.[158],$(apt-utils_MANPAGES)))

	dh_installchangelogs -p$@
	dh_strip -p$@
	dh_compress -p$@
	dh_fixperms -p$@
	dh_makeshlibs -p$@
	dh_installdeb -p$@
	dh_shlibdeps -p$@
	dh_gencontrol -p$@
	dh_md5sums -p$@
	dh_builddeb -p$@

$(LIBAPT_PKG): build-binary debian/$(LIBAPT_PKG).install
	dh_testdir -p$@
	dh_testroot -p$@
	dh_prep -p$@
	dh_installdirs -p$@

	dh_install -p$@ --sourcedir=$(BLD)
	dh_installdocs -p$@
	dh_installchangelogs -p$@
	dh_strip -p$@
	dh_compress -p$@
	dh_fixperms -p$@
	dh_makeshlibs -p$@
	dh_installdeb -p$@
	dh_shlibdeps -p$@ 
	dh_gencontrol -p$@
	dh_md5sums -p$@
	dh_builddeb -p$@

$(LIBAPT_INST): build-binary debian/$(LIBAPT_INST).install
	dh_testdir -p$@
	dh_testroot -p$@
	dh_prep -p$@
	dh_installdirs -p$@

	dh_install -p$@ --sourcedir=$(BLD)
	dh_installdocs -p$@
	dh_installchangelogs -p$@
	dh_strip -p$@
	dh_compress -p$@
	dh_fixperms -p$@
	dh_makeshlibs -p$@
	dh_installdeb -p$@
	dh_shlibdeps -p$@ 
	dh_gencontrol -p$@
	dh_md5sums -p$@
	dh_builddeb -p$@

apt-transport-https: build-binary libapt-pkg-dev
	dh_testdir -p$@
	dh_testroot -p$@
	dh_prep -p$@
	dh_installdirs -p$@

	dh_install -p$@ --sourcedir=$(BLD)
	dh_installdocs -p$@ debian/apt-transport-https.README
	dh_installexamples -p$@

	# Install the man pages..
	dh_installman -p$@

	dh_installchangelogs -p$@
	dh_strip -p$@
	dh_compress -p$@
	dh_fixperms -p$@
	dh_installdeb -p$@
	dh_shlibdeps -p$@ 
	dh_gencontrol -p$@
	dh_md5sums -p$@
	dh_builddeb -p$@

buildlib/config.guess buildlib/config.sub:
	$(MAKE) "$@"

configure:
	$(MAKE) configure

# translate targets to targets required by debian-policy
binary-arch: $(LIBAPT_PKG) $(LIBAPT_INST) apt libapt-pkg-dev apt-utils apt-transport-https
binary-indep: apt-doc libapt-pkg-doc
binary: binary-indep binary-arch
build-arch: build-binary
build-indep: build-manpages build-debiandoc
build: build-indep build-arch

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