#!/usr/bin/make -f
# -*- makefile -*-

# This file is licensed under the terms of the Gnu Public License.
# With the one additional provision that Ian Jackson's name may not be
# removed from the file.

# Copyright 1994,1995 Ian Jackson
# Copyright 2004-2005 Jrme Marant <jerome@debian.org>
# Copyright 1998-2018 Rob Browning <rlb@defaultvalue.org>

# Originally copied from the GNU Hello Debian rules file (1.3).
# Modified for emacs by Mark Eichin <eichin@kitten.gen.ma.us>.
# Debhelper support added via one of Joey Hess' example files.
# See the debian/changelog for further historical information.

include /usr/share/dpkg/pkg-info.mk

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

SHELL := /bin/bash
pf := set -o pipefail

# Emacs 28+ reads the version from org.el, which we don't have.
org_version := 9.5

# Must match emacs24{,-lucid,-nox} packages
bin_priority := 28

# For now we assume that emacs' versioning scheme is always
# MAJOR.MINORtinyrev where MAJOR and MINOR are integers and tinyrev is
# an optional lowercase letter (or letters).  We also assume that
# upstream uses a numbering scheme that sorts in a "Debian friendly"
# way.  So far that's always been true.  If it becomes false, some of
# the values below will have to be set manually.

# From /usr/share/dpkg/pkg-info.mk
src_name := $(DEB_SOURCE)
debian_ver := $(DEB_VERSION)

major_ver := $(shell echo $(debian_ver) | sed 's/\..*//')

# Everything before the last '-' (21.3, 21.3a, 21.3+1, etc.)
debsrc_ver := $(shell echo $(debian_ver) | sed 's/-[^-]\+$$//')

# Everything before the last '-' minus epoch
deborig_ver := $(shell echo $(debsrc_ver) | sed 's/^[0-9]\+://')

# Everything before the last '+' (21.2, 21.3a, etc.) minus epoch
upstream_ver := $(shell echo $(deborig_ver) | sed 's/+[^+]\+$$//')

# 20.5, not 20.5a (used in load-path, etc.)
runtime_ver := $(shell echo $(upstream_ver) | sed 's/[^0-9]\+$$//')

# These files must always exist, i.e. can't ever be cleaned.
persistent_autogen_install_files := debian/copyright

transient_autogen_install_files := \
  debian/emacs-common-non-dfsg.README.Debian \
  debian/emacs-common-non-dfsg.lintian-overrides

autogen_install_files := \
  $(persistent_autogen_install_files) $(transient_autogen_install_files)

autogen_installdeb_files := debian/emacs-common-non-dfsg.postinst

pkgdir_common := $(CURDIR)/debian/emacs-common-non-dfsg

etc_dir := /usr/share/emacs/$(runtime_ver)/etc
etc_files := DEVEL.HUMOR JOKES

deb_patches := $(addprefix debian/patches/,$(shell cat debian/patches/series))

%:
	dh $@

# For now, assumes there are no "|" characters in the expansions.
debian/autogen.sed: debian/rules debian/changelog
	echo 's|@MAJOR_VERSION@|$(major_ver)|g' >> '$@.tmp'
	echo 's|@PACKAGE_VERSION@|$(debian_ver)|g' >> '$@.tmp'
	echo 's|@UPSTREAM_VERSION@|$(upstream_ver)|g' >> '$@.tmp'
	echo 's|@DEBSRC_VERSION@|$(debsrc_ver)|g' >> '$@.tmp'
	echo 's|@DEBORIG_VERSION@|$(deborig_ver)|g' >> '$@.tmp'
	mv '$@.tmp' '$@'

.PHONY: debian-sync
debian-sync: $(persistent_autogen_install_files)
        # so dh pattern rule doesn't try to handle this target
	true

debian/emacs-common-non-dfsg.README.Debian: \
  debian/emacs-common-non-dfsg.README.Debian.in \
  debian/patches/*.patch $(deb_patches) \
  debian/rules debian/patch-to-news
	perl -p \
	  -e 's|^\@DEBIAN_NEWS\@\n|`debian/patch-to-news $(deb_patches)`|e' \
	  debian/emacs-common-non-dfsg.README.Debian.in > "$@.tmp"
	mv "$@.tmp" "$@"

debian/%: debian/%.in debian/autogen.sed
	sed -f debian/autogen.sed "$<" > "$@.tmp"
	mv "$@.tmp" "$@"

debian/emacs-common-non-dfsg.postinst:
	shopt -s nullglob; \
	DEB_ALT_PRIORITY='$(bin_priority)' \
	DEB_INFO_SUBDIR=emacs \
	debian/emacs-common-non-dfsg.postinst.gen \
	  info/*.info info/*.info-[0-9] info/*.info-[0-9][0-9] \
	    > '$@.tmp'
	mv '$@.tmp' '$@'

doc/%/Makefile: doc/%/Makefile.in debian/rules
	sed \
	  -e "s|@top_srcdir@|$(CURDIR)|g" \
	  -e "s|@SHELL@|/bin/bash|g" \
	  -e "s|@srcdir@|.|g" \
	  -e "s|@MAKEINFO@|makeinfo|g" \
	  -e "s|@MKDIR_P@|mkdir -p|g" \
	  -e "s|@INFO_OPTS@|--no-split|g" \
	  -e "s|@INFO_EXT@|.info|g" "$<" > "$@.tmp"
	mv "$@.tmp" "$@"

doc/emacs/emacsver.texi: doc/emacs/emacsver.texi.in
	sed -e 's|@version@|$(upstream_ver)|g' "$^" > "$@"

doc_makefiles := \
  doc/emacs/Makefile \
  doc/lispintro/Makefile \
  doc/lispref/Makefile \
  doc/misc/Makefile

override_dh_auto_configure: $(doc_makefiles) doc/emacs/emacsver.texi

# This target just supports parallel builds
.PHONY: debian/stamp/doc-%
debian/stamp/doc-%: $(doc_makefiles)
	$(MAKE) -C $(dir $(patsubst debian/stamp/doc-%,%,$@))

# Only needed because parallel seems to be disabled in auto_build otherwise
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  parallel := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
  parallel := 1
endif

override_dh_auto_build: $(doc_makefiles)
	rm -rf info && install -d info
	DEB_EMACS_BUILD_ORG_DOC_VERSION=$(org_version) \
	  $(MAKE) -j$(parallel) \
	    -f debian/rules $(foreach m,$(doc_makefiles),debian/stamp/doc-$(m))

override_dh_auto_install: ; true

pkg_infodir := $(pkgdir_common)/usr/share/info/emacs

override_dh_install: $(autogen_install_files)
	install -d $(pkg_infodir)
	test -e info/emacs.info
	shopt -s nullglob; \
	  cp -a info/*.info info/*.info-[0-9] info/*.info-[0-9][0-9] \
	    $(pkg_infodir)
	install -d $(pkgdir_common)/$(etc_dir)
	install --mode 0644 $(addprefix etc/,$(etc_files)) \
	  $(pkgdir_common)/$(etc_dir)
	dh_install

override_dh_testdir:
	dh_testdir \
	  debian/emacs-common-non-dfsg.README.Debian.in \
	  doc/emacs/emacs.texi

override_dh_clean: $(persistent_autogen_install_files)
	rm -f \
	  doc/emacs/emacsver.texi \
	  doc/misc/modus-themes.texi \
	  doc/misc/org.texi
	rm -rf \
	  debian/*.tmp \
	  doc/emacs/Makefile \
	  doc/lispintro/Makefile \
	  doc/lispref/Makefile \
	  doc/misc/Makefile \
	  info \
	  debian/stamp
	rm -f \
	  $(autogen_installdeb_files) \
	  $(doc_makefiles) \
	  $(transient_autogen_install_files) \
	  debian/autogen.sed
	dh_clean

override_dh_installdeb: $(autogen_installdeb_files)
	dh_installdeb
