#!/usr/bin/make -f

# Basic package information.
package := $(shell grep Source debian/control | sed 's/^Source: //')
date	:= $(shell date +"%Y-%m-%d")
version := $(shell dpkg-parsechangelog | awk '/^Version:/ { print $$2; exit }')

# Select the Markdown implementation to use.
MDWN	:= multimarkdown

export MDWN

# Location of the source and build directories.
SRCTOP	:= $(CURDIR)
TMPTOP	:= $(SRCTOP)/debian/tmp
DOCDIR	:= $(TMPTOP)/usr/share/doc/$(package)
LIBDIR	:= $(TMPTOP)/usr/share/doc-base

# DocBook source files in the top-level directory.  We do some common actions
# with each of these: build text, HTML, and one-page HTML output.
XML_FILES   := policy menu-policy perl-policy

# The upgrading-checklist used to be a document of its own, which was merged
# with the conversion to DocBook. Keep backwards compatibility files.
XML_SPLIT_FILES := upgrading-checklist

XML_VERSION := copyright-format/version.xml debconf_spec/include/version.xml \
	       version.xml

# Markdown source files in the top-level directory.  We generate text and
# HTML versions from these.
MDWN_FILES  := Process README

# doc-base description files for the documents we include.
DESC_FILES  := copyright-format-1.0 debian-policy debian-menu-policy \
	       debian-perl-policy debconf-spec fhs autopkgtest

# Our local copy of the File Hierarchy Standard.  We don't build this from
# source, but we do have a copy of the source in FHS_ARCHIVE.
FHS_ARCHIVE := fhs-2.3-source.tar.gz
FHS_FILES   := fhs-2.3.html fhs-2.3.ps.gz fhs-2.3.txt.gz fhs-2.3.pdf.gz

# A list of the simple Policy files that we include in the documentation
# directory of the generated package.  The directories of HTML output are
# handled separately.
POLICY_FILES := $(XML_FILES:=.txt.gz)					\
		$(XML_FILES:=-1.html)					\
		$(XML_SPLIT_FILES:=.txt.gz)				\
		$(XML_SPLIT_FILES:=-1.html)				\
		virtual-package-names-list.txt	                        \
		copyright-format/copyright-format-1.0.html		\
		copyright-format/copyright-format-1.0.txt.gz		\
		autopkgtest/autopkgtest.html				\
		autopkgtest/autopkgtest.txt.gz				\
		debconf_spec/debconf_specification.html			\
		debconf_spec/debconf_specification.txt.gz		\
		policy.ps.gz policy.pdf.gz				\
		README.txt README.html README.css			\
		Process.txt Process.html

# Used by the clean rules.  STAMPS_TO_CLEAN are the stamp files used to tell
# make that a target has completed, and are removed first.  FILES_TO_CLEAN
# are individual generated files to remove.  DIRS_TO_CLEAN are entire
# directories to remove.
STAMPS_TO_CLEAN := stamp-binary stamp-build
DIRS_TO_CLEAN   := $(XML_FILES:=.html) debian/tmp fhs
FILES_TO_CLEAN  := $(XML_FILES:=.txt) $(XML_FILES:=.txt.gz)		\
		   $(XML_FILES:=.html.tar.gz) $(XML_FILES:=-1.html)	\
		   $(XML_SPLIT_FILES:=.txt) $(XML_SPLIT_FILES:=.txt.gz)	\
		   $(XML_SPLIT_FILES:=-1.html)				\
		   $(MDWN_FILES:=.html) $(MDWN_FILES:=.txt)		\
		   policy.pdf.gz policy.ps.gz				\
		   policy.pdf policy.ps policy.tpt policy.txt		\
		   autopkgtest/version.txt				\
		   $(XML_VERSION)					\
		   version.ent						\
		   debian/files

# Install files and directories with the correct ownership and permissions.
install := install -p -o root -g root -m 644
mkdir   := install -d -o root -g root -m 755


all build build-indep: stamp-build
build-arch:
stamp-build: $(MDWN_FILES:=.md)				\
	     $(XML_FILES:=.xml)				\
	     $(XML_SPLIT_FILES:=.xml)			\
	     $(XML_VERSION)				\
	     copyright-format/copyright-format-1.0.xml	\
	     autopkgtest/autopkgtest.md			\
	     autopkgtest/version.txt			\
	     debconf_spec/include/commands.xml		\
	     debconf_spec/include/priorities.xml	\
	     debconf_spec/include/statuscodes.xml	\
	     debconf_spec/include/types.xml
	$(MAKE)	$(XML_FILES:=.xml.validate) \
		$(XML_FILES:=.html.tar.gz) \
		$(XML_FILES:=-1.html) \
		$(XML_FILES:=.txt.gz) \
		$(XML_SPLIT_FILES:=-1.html) \
		$(XML_SPLIT_FILES:=.txt.gz) \
		policy.ps.gz policy.pdf.gz
	$(MAKE) $(MDWN_FILES:=.html) \
		$(MDWN_FILES:=.txt)
	$(MAKE) -C copyright-format all
	$(MAKE) -C autopkgtest all
	$(MAKE) -C debconf_spec all
	touch stamp-build

# Create the version files for inclusion in the various documents.  We want
# to put the Policy version and date in each document, even if they
# separately have their own versions.
configure: $(XML_VERSION) autopkgtest/version.txt
$(XML_VERSION): debian/changelog
	rm -f $@
	echo '<?xml version="1.0" encoding="utf-8"?>' > $@
	echo '<!ENTITY version "$(version)">'        >> $@
	echo '<!ENTITY date    "$(date)">'           >> $@
autopkgtest/version.txt: debian/changelog
	rm -f $@
	echo                                     > $@
	echo '---'                               >> $@
	echo 'Debian Policy $(version), $(date)' >> $@

clean:
	rm -f $(STAMPS_TO_CLEAN)
	$(MAKE) -C copyright-format clean
	$(MAKE) -C autopkgtest clean
	$(MAKE) -C debconf_spec clean
	rm -f $(FILES_TO_CLEAN)
	rm -rf $(DIRS_TO_CLEAN)
	rm -f core `find . \( -name '*.orig' -o -name '*.rej' -o	\
	  -name '*~' -o -name '*.bak' -o -name '#*#' -o			\
	  -name '.*.orig' -o -name '.*.rej' -o -name '.SUMS' -o		\
	  -size 0 \) -print` TAGS


binary: binary-indep binary-arch
binary-arch:
binary-indep: stamp-binary
stamp-binary: stamp-build
	@test $$(id | sed -e 's/(.*$$//' -e 's/^uid=//') -eq 0 || \
	    (echo 'You must run this as root (or use fakeroot)' >&2; exit 1)
	rm -rf		$(TMPTOP)
	$(mkdir)	$(TMPTOP)/DEBIAN
	$(mkdir)	$(DOCDIR)/fhs
	$(mkdir)	$(LIBDIR)
#
# Install simple files.
#
	$(install)	$(POLICY_FILES)		$(DOCDIR)/
	$(install)	$(FHS_FILES)		$(DOCDIR)/fhs/
	$(install)	debian/changelog	$(DOCDIR)/
	$(install)	debian/copyright	$(DOCDIR)/
#
# Install generated HTML directories.
#
	@set -ex; for file in $(XML_FILES); do			\
		tar -C $(DOCDIR) -zxf $$file.html.tar.gz;	\
	done
#
# Install doc-base files.
#
	@set -ex; for file in $(DESC_FILES); do			\
		$(install) $$file.desc $(LIBDIR)/$$file;	\
	done
#
# Compress files and build MD5 checksums.
#
	gzip -fn9 $(DOCDIR)/changelog
	gzip -fn9 $(DOCDIR)/*.txt
	@set -ex; cd debian/tmp; \
	find . -path './DEBIAN' -prune -o -type f -printf '%P\0' \
		| xargs -r0 md5sum > DEBIAN/md5sums
#
# Build the package.
#
	dpkg-gencontrol -p$(package) -Pdebian/tmp
	chown -R root:root debian/tmp
	chmod -R go=rX debian/tmp
	dpkg --build debian/tmp ..
	touch stamp-binary


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