#!/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 2019 Andreas Beckmann <anbe@debian.org>
# Copyright 1994,1995 Ian Jackson
# Copyright 2004-2005 Jérôme Marant <jerome@debian.org>
# Copyright 1998-2020, 2022-2023 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/architecture.mk
include /usr/share/dpkg/pkg-info.mk

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

SHELL := /bin/bash
TERM := linux
quilt := QUILT_PATCHES=debian/patches QUILT_PATCHES_PREFIX=y quilt
pf := set -o pipefail

# This package uses debian/source/format 3.0 (quilt).

# 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.

# (You might imagine that a VPATH build would allow us to avoid having
#  to copy the entire source tree for each of the three builds, but it
#  turns out that the emacs build process doesn't entirely respect the
#  VPATH and modifies all kinds of files in the srcdir (e.g. *.elc,
#  *.map, etc.).

# The official upstream version defined by AC_INIT in configure.in.
upstream_ver := $(shell debian/upstream-version)
ifeq (,$(upstream_ver))
  $(error 'Unable to find upstream version number.')
endif

# This must be the version that's actually used at runtime for things
# like load-path.  It may not be the same as the upstream version
# (i.e. when you have upstream 20.5a, the functional version may still
# be 20.5), so sometimes we may have to do this by hand.
runtime_ver := $(shell $(pf); echo $(upstream_ver) | perl -pe 's/[a-z]+$$//o')
major_ver := $(shell $(pf); echo $(runtime_ver) | perl -pe 's/\..*$$//o')
minor_ver := $(shell $(pf); echo $(runtime_ver) | perl -pe 's/^[^.]*\.//o')

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

# The Debian revision (i.e. the 1 from 20.5-1)
# Always everything after the last '-'
debian_rev := $(shell $(pf); echo $(debian_ver) | perl -pe 's/.*-//o')

# Might be 21.3, or it might be 21.3+1 if we've had to have more than
# one re-release of the upstream source (rare, but it happens), or if
# we have to split the package for DFSG purposes.  Always everything
# before the last '-'
debsrc_ver := $(shell $(pf); echo $(debian_ver) | perl -pe 's/-[^-]+$$//o')

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

######################################################################
# Customizable variables

bin_priority := 28

# This might also be something like 2006-09-09 for snapshots.
menu_ver := $(major_ver)

######################################################################

# 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

deb_host_multiarch := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

CFLAGS = $(shell dpkg-buildflags --get CFLAGS)
CFLAGS += -Wall
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)

joblimit := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
ifeq (,$(joblimit))
  joblimit := 1
endif

# Separate alternatives into variant (flavor) specific and common.
# For now, we only handle /usr/bin.
var_alternatives := emacsclient
common_alternatives := ctags ebrowse etags

# Info files that are going to show up in the main dir.
main_dir_info_files := efaq.info

# Files that the build stage depends on (may also be listed in other vars).
persistent_autogen_build_files := debian/copyright
nonpersistent_autogen_build_files :=

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

nonpersistent_autogen_install_files := \
  debian/emacs-bin-common.postinst \
  debian/emacs-bin-common.prerm \
  debian/emacs-common.README.Debian \
  debian/emacs-common.postinst \
  debian/emacs-common.prerm \
  debian/emacs-nox.lintian-overrides \
  debian/emacs-nox.postinst \
  debian/emacs-nox.prerm \
  debian/emacs-lucid.lintian-overrides \
  debian/emacs-lucid.postinst \
  debian/emacs-lucid.prerm \
  debian/emacs-gtk.lintian-overrides \
  debian/emacs-gtk.postinst \
  debian/emacs-gtk.prerm \
  debian/emacs-pgtk.lintian-overrides \
  debian/emacs-pgtk.postinst \
  debian/emacs-pgtk.prerm

autogen_build_files := \
  $(nonpersistent_autogen_build_files) $(persistent_autogen_build_files)

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

persistent_autogen_files := \
  $(persistent_autogen_build_files) $(persistent_autogen_install_files)

nonpersistent_autogen_files := \
  $(nonpersistent_autogen_build_files) $(nonpersistent_autogen_install_files)


# Build directories
pkgdir_common := $(CURDIR)/debian/emacs-common
pkgdir_bin_common := $(CURDIR)/debian/emacs-bin-common
pkgdir_gtk := $(CURDIR)/debian/emacs-gtk
pkgdir_pgtk := $(CURDIR)/debian/emacs-pgtk
pkgdir_nox := $(CURDIR)/debian/emacs-nox
pkgdir_lucid := $(CURDIR)/debian/emacs-lucid
pkgdir_el := $(CURDIR)/debian/emacs-el

install_dir_gtk := $(CURDIR)/debian/install-gtk
install_dir_pgtk := $(CURDIR)/debian/install-pgtk
install_dir_nox := $(CURDIR)/debian/install-nox
install_dir_lucid := $(CURDIR)/debian/install-lucid

libexec_dir_emacs := usr/libexec/emacs/$(runtime_ver)/$(DEB_HOST_GNU_TYPE)

local_lpath := /etc/emacs
local_lpath := $(local_lpath):/usr/local/share/emacs/$(runtime_ver)/site-lisp
local_lpath := $(local_lpath):/usr/local/share/emacs/site-lisp
local_lpath := $(local_lpath):/usr/share/emacs/$(runtime_ver)/site-lisp
local_lpath := $(local_lpath):/usr/share/emacs/site-lisp

# Installation local_lpath
local_lpath_install \
  := $(pkgdir_common)/$(subst :,:$(pkgdir_common)/,$(local_lpath))

# If we ever need to do the stripping outside of dh_strip, just add an
# INSTALL_STRIP="-s" to the make vars below.

define emacs_inst
  $(MAKE) -C debian/$(1) install \
    DESTDIR=$(2) \
    infodir=/usr/share/info/emacs \
    localstatedir=/var
endef

# If we ever need it, we can create a copy that doesn't assume ./debian/
define deb_sub
  perl -p \
    -e "s|\@PKG_NAME\@|$(pkg_name)|go;" \
    -e "s|\@MAJOR_VERSION\@|$(major_ver)|go;" \
    -e "s|\@MINOR_VERSION\@|$(minor_ver)|go;" \
    -e "s|\@FULL_VERSION\@|$(runtime_ver)|go;" \
    -e "s|\@PACKAGE_VERSION\@|$(debian_ver)|go;" \
    -e "s|\@DEBIAN_REV\@|$(deb_rev)|go;" \
    -e "s|\@UPSTREAM_VERSION\@|$(upstream_ver)|go;" \
    -e "s|\@DEBSRC_VERSION\@|$(debsrc_ver)|go;" \
    -e "s|\@DEBORIG_VERSION\@|$(deborig_ver)|go;" \
    -e "s|\@MENU_VERSION\@|$(menu_ver)|go;" \
    -e "s|\@X_SUPPORT\@|$(xsupport)|go;" \
    -e "s|\@BIN_PRIORITY\@|$(bin_priority)|go;" \
    -e "s|\@DEB_VAR_ALTERNATIVES\@|$(var_alternatives)|go;" \
    -e "s|\@DEB_COMMON_ALTERNATIVES\@|$(common_alternatives)|go;" \
      < $(1) > $(2)
endef

%:
	dh $@

check-vars:
	@echo "src_name: $(src_name)"
	@echo "upstream_ver: $(upstream_ver)"
	@echo "debian_ver: $(debian_ver)"
	@echo "debsrc_ver: $(debsrc_ver)"
	@echo "deborig_ver: $(deborig_ver)"
	@echo "debian_rev: $(debian_rev)"
	@echo "runtime_ver: $(runtime_ver)"
	@echo "major_ver: $(major_ver)"
	@echo "minor_ver: $(minor_ver)"

.PHONY: check-autogen
check-autogen: $(persistent_autogen_files) $(nonpersistent_autogen_files)

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

debian/emacs-gtk.%: xsupport := "gtk"
debian/emacs-gtk.%: pkg_name := emacs-gtk

debian/emacs-pgtk.%: xsupport := "pgtk"
debian/emacs-pgtk.%: pkg_name := emacs-pgtk

debian/emacs-nox.%: xsupport := "nox"
debian/emacs-nox.%: pkg_name := emacs-nox

debian/emacs-lucid.%: xsupport := "lucid"
debian/emacs-lucid.%: pkg_name := emacs-lucid

debian/emacs-common.%: pkg_name := emacs-common
debian/emacs-bin-common.%: pkg_name := emacs-bin-common

debian/%: debian/%.in debian/changelog debian/rules
	$(call deb_sub,$<,$@)

debian/emacs-nox.%: debian/emacsVAR.% debian/changelog debian/rules
	$(call deb_sub,$<,$@)

debian/emacs-gtk.%: debian/emacsVAR.% debian/changelog debian/rules
	$(call deb_sub,$<,$@)

debian/emacs-pgtk.%: debian/emacsVAR.% debian/changelog debian/rules
	$(call deb_sub,$<,$@)

debian/emacs-lucid.%: debian/emacsVAR.% debian/changelog debian/rules
	$(call deb_sub,$<,$@)

debian/emacs-common.README.Debian: \
  debian/emacs-common.README.in debian/patches/*.patch debian/patches/series \
  debian/rules debian/patch-to-news
	cd debian && \
	  csplit -s -f emacs-common.README. \
	  emacs-common.README.in '/@@PATCH_LIST_HERE@@/'
	cp debian/emacs-common.README.00 debian/emacs-common.README.tmp
	debian/patch-to-news $$($(quilt) series) \
	  >> debian/emacs-common.README.tmp
	tail -n +2 \
	  < debian/emacs-common.README.01 \
	  >> debian/emacs-common.README.tmp
	mv debian/emacs-common.README.tmp $@

# Uncomment to experiment with NATIVE_FULL_AOT - tests don't appear to pass yet.
#export NATIVE_FULL_AOT ?= 1

# common configure flags
confflags += --prefix=/usr
confflags += --sharedstatedir=/var/lib
confflags += --libexecdir=/usr/libexec
confflags += --localstatedir=/var/lib
confflags += --infodir=/usr/share/info
confflags += --mandir=/usr/share/man
confflags += --with-libsystemd
confflags += --with-pop=yes
confflags += --enable-locallisppath=$(local_lpath)
confflags += --with-sound=alsa
confflags += --without-gconf
confflags += --with-mailutils
# At the moment, there's no mips64el libgccjitN-dev
ifeq (,$(filter $(DEB_HOST_ARCH),mips64el riscv64 s390x))
  deb_native_compilation_enabled := 1
  confflags += --with-native-compilation
endif

# x configure flags
confflags_gtk := $(confflags)
confflags_gtk += --with-cairo
confflags_gtk += --with-x=yes
confflags_gtk += --with-x-toolkit=gtk3
# For those who prefer the old-style non-toolkit scrollbars, just
# change the assignment below to --without-toolkit-scroll-bars.  The
# resulting emacs-gtk package will have the old scrollbars.
confflags_gtk += --with-toolkit-scroll-bars

# pgtk configure flags
confflags_pgtk := $(confflags)
confflags_pgtk += --with-pgtk

# nox configure flags
confflags_nox := $(confflags)
confflags_nox += --with-x=no
confflags_nox += --without-gsettings

# lucid configure flags
confflags_lucid := $(confflags)
confflags_lucid += --with-x=yes
confflags_lucid += --with-x-toolkit=lucid
confflags_lucid += --with-toolkit-scroll-bars
confflags_lucid += --without-gsettings

define cfg_tree
  cd $(1) && \
    CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" \
    CC=gcc-14 \
    REL_ALLOC=no \
      $(CURDIR)/debian/build-src/configure $(confflags) $(2)
endef

define build_cmd
  $(MAKE) -C $(1) -j $(joblimit)
  # If we don't use bootstrap, we need to explicitly build info.
  $(MAKE) -C $(1) -j $(joblimit) info
endef

# Don't mess with the source tree (we build in debian/build-*)
override_dh_update_autotools_config:
override_dh_autoreconf:
override_dh_auto_configure:

debian/stamp-configured: $(autogen_build_files)
        # If the deps for this target change, start over
	rm -rf debian/build-src debian/build-gtk debian/build-pgtk debian/build-lucid debian/build-nox
	mkdir debian/build-src
	cp -a $$(ls -A | egrep -v '^(\.git|\.pc|debian)$$') debian/build-src
	cp -a /usr/share/misc/config.guess /usr/share/misc/config.sub \
	  debian/build-src
	cd debian/build-src && ./autogen.sh
	mkdir debian/build-lucid debian/build-nox debian/build-gtk debian/build-pgtk
        # Create one VPATH build per-config
	$(call cfg_tree,debian/build-gtk,$(confflags_gtk))
	$(call cfg_tree,debian/build-pgtk,$(confflags_pgtk))
	$(call cfg_tree,debian/build-nox,$(confflags_nox))
	$(call cfg_tree,debian/build-lucid,$(confflags_lucid))
	touch $@

override_dh_auto_build: debian/stamp-configured
        # The first build will bootstrap build-src, which will speed
        # the remaining builds.
	$(call build_cmd,debian/build-gtk)
	$(call build_cmd,debian/build-pgtk)
	$(call build_cmd,debian/build-nox)
	$(call build_cmd,debian/build-lucid)

override_dh_auto_test:
  ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -C debian/build-lucid -j $(joblimit) check
	$(MAKE) -C debian/build-nox -j $(joblimit) check
	$(MAKE) -C debian/build-gtk -j $(joblimit) check
	$(MAKE) -C debian/build-pgtk -j $(joblimit) check
  endif

ifdef deb_native_compilation_enabled
  define maybe_install_native_lisp
    install -d $(2)/usr/lib/emacs/$(runtime_ver)
    cp -a $(1)/usr/lib/emacs/$(runtime_ver)/native-lisp \
      $(2)/usr/lib/emacs/$(runtime_ver)
  endef
endif

define install_common_binpkg_bits
  # args: (1) srcdir (2) pkgdir (3) pkgname (4) bin-suffix

  install -d $(2)/usr/bin/
  cp -a $(1)/usr/bin/emacs-$(upstream_ver) $(2)/usr/bin/emacs-$(4)
  set -x; for f in $(var_alternatives); do \
    cp -a "$(1)/usr/bin/$$f"  "$(2)/usr/bin/$$f.emacs"; \
  done

  test -f $(1)/$(libexec_dir_emacs)/emacs-*.pdmp
  install -d $(2)/$(libexec_dir_emacs)
  cp -a $(1)/$(libexec_dir_emacs)/emacs-*.pdmp $(2)/$(libexec_dir_emacs)/

  $(call maybe_install_native_lisp,$(1),$(2))

  install -d $(2)/usr/share/emacs/$(runtime_ver)/etc
  cp -a $(1)/usr/share/emacs/$(runtime_ver)/etc/DOC \
	$(2)/usr/share/emacs/$(runtime_ver)/etc/

  install -d $(2)/usr/share/man/man1
  dh_link -p$(3) \
    usr/share/man/man1/emacs.1.gz \
    usr/share/man/man1/emacs-$(4).1.gz

  dh_link -p$(3) usr/share/doc/emacs-common usr/share/doc/$(3)

endef

override_dh_auto_install: $(autogen_install_files)
	rm -rf \
	  $(install_dir_gtk) $(install_dir_pgtk) \
	  $(install_dir_nox) $(install_dir_lucid) \
	  $(pkgdir_common)/* \
	  $(pkgdir_bin_common)/* \
	  $(pkgdir_gtk)/* \
	  $(pkgdir_pgtk)/* \
	  $(pkgdir_nox)/* \
	  $(pkgdir_lucid)/* \
	  $(pkgdir_el)/*

	$(call emacs_inst,build-pgtk,$(install_dir_pgtk))

        ##################################################
        # emacs-common
        ifneq (,$(findstring emacs-common, $(shell dh_listpackages)))
	  install -d $(pkgdir_common)
	  cp -a $(install_dir_pgtk)/* $(pkgdir_common)

	  rm -r $(pkgdir_common)/usr/bin
	  # Move to emacs-pgtk; only that pkg needs it, and it causes
	  # a gsettings-related dependency to be added (#1050393).
	  rm -r $(pkgdir_common)/usr/share/glib-2.0/schemas/org.gnu.emacs.defaults.gschema.xml
	  # Remove and verify expectation it's empty
	  cd $(pkgdir_common)/usr/share && rmdir --parents glib-2.0/schemas
	  rm \
	    $(pkgdir_common)/$(libexec_dir_emacs)/hexl \
	    $(pkgdir_common)/$(libexec_dir_emacs)/emacs-*.pdmp \
	    $(pkgdir_common)/$(libexec_dir_emacs)/rcs2log
	  cd $(pkgdir_common)/usr \
	    && rmdir --parents libexec/emacs/$(runtime_ver)/$(DEB_HOST_GNU_TYPE)

  ifdef deb_native_compilation_enabled
	rm -r $(pkgdir_common)/usr/lib/emacs/$(runtime_ver)/native-lisp
  endif

	cd $(pkgdir_common)/usr/share/emacs/$(runtime_ver)/etc \
	  && test -f DOC
	cd $(pkgdir_common)/usr/share/emacs/$(runtime_ver)/etc \
	  && rm DOC

	  # lisp path directories
	  install -d $(pkgdir_common)/etc/emacs/site-start.d
	  install -d $(pkgdir_common)/usr/share/emacs/site-lisp

	install -m 0644 debian/00debian.el \
	  $(pkgdir_common)/etc/emacs/site-start.d/

	  cd $(pkgdir_common)/usr/share/emacs/$(runtime_ver)/etc/images/icons \
	    && convert hicolor/16x16/apps/emacs.{png,xpm}
	  cd $(pkgdir_common)/usr/share/emacs/$(runtime_ver)/etc/images/icons \
	    && convert hicolor/32x32/apps/emacs.{png,xpm}

	  # Remove emacs23 icon versions
	  cd $(pkgdir_common)/usr/share/emacs/$(runtime_ver)/etc/images/icons \
	    && rm hicolor/*/apps/emacs{22,23}.*
	  cd $(pkgdir_common)/usr/share/emacs/$(runtime_ver)/etc/images/icons \
	    && rm hicolor/*/mimetypes/emacs-document23.svg
	  cd $(pkgdir_common)/usr/share/icons/hicolor/scalable/mimetypes \
	    && rm emacs-document23.svg

	  # Remove redundant emacs.desktop file.
	  rm $(pkgdir_common)/usr/share/emacs/$(runtime_ver)/etc/emacs.desktop
	  rm $(pkgdir_common)/usr/share/applications/emacs.desktop

	  gunzip $(pkgdir_common)/usr/share/man/man1/*.gz
	  cd $(pkgdir_common)/usr/share/man/man1/ && \
	    for f in *.1; do mv $$f $$(basename $${f} .1).emacs.1; done

	  # At least etc/COPYING is needed by M-x describe-copying.
	  rm $(pkgdir_common)/usr/share/emacs/$(runtime_ver)/etc/COPYING
	  rm $(pkgdir_common)/usr/share/emacs/$(runtime_ver)/lisp/COPYING
	  dh_link -pemacs-common /usr/share/common-licenses/GPL-3 \
	    usr/share/emacs/$(runtime_ver)/etc/COPYING
	  dh_link -pemacs-common /usr/share/common-licenses/GPL-3 \
	    usr/share/emacs/$(runtime_ver)/lisp/COPYING

	  $(pf); cd $(pkgdir_common) && \
	    find -name "*.elc" | perl -pe 's/\.elc$$/\.el/o' | xargs rm -f

	  $(pf); cd $(pkgdir_common) && \
	    find -name "*.elc" | perl -pe 's/\.elc$$/\.el\.gz/o' | xargs rm -f

	  # Remove extraneous info dir files.  These may not exist if dpkg
	  # is 1.5.4 or newer.
	  rm -f $(pkgdir_common)/usr/share/info/emacs/dir
	  rm -f $(pkgdir_common)/usr/share/info/emacs/dir.old

	  install -d $(pkgdir_common)/usr/local/share/emacs/site-lisp
        endif

        ##################################################
        # emacs-bin-common
        ifneq (,$(findstring emacs-bin-common, $(shell dh_listpackages)))
	  # Move common binaries to emacs-bin-common.  For now, just
	  # take them from pgtk.

	  ## usr/bin
	  install -d $(pkgdir_bin_common)/usr/bin
	  cp -a $(install_dir_pgtk)/usr/bin $(pkgdir_bin_common)/usr/bin/debtmp

	  set -x; \
	  cd $(pkgdir_bin_common)/usr/bin; \
	  for f in $(common_alternatives); do mv "debtmp/$$f" "$$f.emacs"; done

	  set -x; \
	  cd $(pkgdir_bin_common)/usr/bin/debtmp; \
	  for f in $(var_alternatives); do rm "$$f"; done

	  # Now there should ony be emacs and emacs-VER
	  cd $(pkgdir_bin_common)/usr/bin/debtmp \
	    && rm emacs emacs-$(upstream_ver)

	  # Failure here should indicate something new upstream that
	  # needs to be in var_alternatives or common_alternatives.



	  ls -A $(pkgdir_bin_common)/usr/bin/debtmp



	  rmdir $(pkgdir_bin_common)/usr/bin/debtmp

	  ## libexec
	  cp -a $(install_dir_pgtk)/usr/libexec $(pkgdir_bin_common)/usr
          # pdmp files are flavor-specific
	  test -f $(pkgdir_bin_common)/$(libexec_dir_emacs)/emacs-*.pdmp
	  rm $(pkgdir_bin_common)/$(libexec_dir_emacs)/emacs-*.pdmp

        endif

        ##################################################
        # emacs-gtk
        ifneq (,$(findstring emacs-gtk, $(shell dh_listpackages)))
	  $(call emacs_inst,build-gtk,$(install_dir_gtk))
	  $(call install_common_binpkg_bits,$(install_dir_gtk),$(pkgdir_gtk),emacs-gtk,gtk)

          # install desktop entries
	  install -d $(pkgdir_gtk)/usr/share/applications
	  install -m 0644 \
	    debian/emacs.desktop \
	    debian/emacs-term.desktop \
	    $(pkgdir_gtk)/usr/share/applications/
        endif

        ##################################################
        # emacs-pgtk
        ifneq (,$(findstring emacs-pgtk, $(shell dh_listpackages)))
	  $(call install_common_binpkg_bits,$(install_dir_pgtk),$(pkgdir_pgtk),emacs-pgtk,pgtk)

          # install desktop entries
	  install -d $(pkgdir_pgtk)/usr/share/applications
	  install -m 0644 \
	    debian/emacs.desktop \
	    debian/emacs-term.desktop \
	    $(pkgdir_pgtk)/usr/share/applications/
	  # install GSettings schema
	  install -D \
	    $(install_dir_pgtk)/usr/share/glib-2.0/schemas/org.gnu.emacs.defaults.gschema.xml \
	    $(pkgdir_pgtk)/usr/share/glib-2.0/schemas/org.gnu.emacs.defaults.gschema.xml
        endif

        ##################################################
        # emacs-nox
        ifneq (,$(findstring emacs-nox, $(shell dh_listpackages)))
	  $(call emacs_inst,build-nox,$(install_dir_nox))
	  $(call install_common_binpkg_bits,$(install_dir_nox),$(pkgdir_nox),emacs-nox,nox)
          # install desktop entry
	  install -d $(pkgdir_nox)/usr/share/applications
	  install -m 0644 \
	    debian/emacs-term.desktop $(pkgdir_nox)/usr/share/applications/
	  rm -rf $(install_dir_nox)
        endif

        ##################################################
        # emacs-lucid
        ifneq (,$(findstring emacs-lucid, $(shell dh_listpackages)))
	  $(call emacs_inst,build-lucid,$(install_dir_lucid))
	  $(call install_common_binpkg_bits,$(install_dir_lucid),$(pkgdir_lucid),emacs-lucid,lucid)

          # install desktop entries
	  install -d $(pkgdir_lucid)/usr/share/applications
	  install -m 0644 \
	    debian/emacs.desktop \
	    debian/emacs-term.desktop \
	    $(pkgdir_lucid)/usr/share/applications/
	  rm -rf $(install_dir_lucid)
        endif

        ##################################################
        # emacs-el
        ifneq (,$(findstring emacs-el, $(shell dh_listpackages)))
	  install -d $(pkgdir_el)
	  $(pf); \
	  (cd $(install_dir_pgtk) && find -name "*.el" -o -name "*.el.gz" -print0 \
	   | tar cpf - --null --files-from -) \
	     | (cd $(pkgdir_el) && tar xpf -)
        endif

        ##################################################
        # final cleanup
	rm -rf $(install_dir_gtk)
	rm -rf $(install_dir_pgtk)
	rm -rf $(install_dir_nox)
	rm -rf $(install_dir_lucid)

# Install the per-user systemd unit in a disabled state by default.
override_dh_installsystemduser:
	dh_installsystemduser --no-enable

# This won't be necessary once strip-nondeterminism is fixed.
.PHONY: override_dh_strip_nondeterminism
override_dh_strip_nondeterminism:
	if grep "emacs-el" <(dh_listpackages); then \
	  cp -a debian/emacs-el/usr/share/emacs/$(runtime_ver)/lisp/version.el.gz \
	    debian/elgz-canary; \
	fi
	cd debian && \
	find $$(cd .. && dh_listpackages) \
	  -name "*.el.gz" -printf "%T@ %p\0" > elgz-info
	dh_strip_nondeterminism
	set -eo pipefail; \
	  cd debian; \
	  while read -r -d $$'\0' line; do \
	    ts="$${line%% *}"; \
	    path="$${line#* }"; \
	    touch --date=@"$$ts" "$$path"; \
	  done < elgz-info
	if grep -q "emacs-el" <(dh_listpackages); then \
	  set -e; \
	  test ! debian/elgz-canary -nt \
	    debian/emacs-el/usr/share/emacs/$(runtime_ver)/lisp/version.el.gz; \
	  test ! debian/elgz-canary -ot \
	    debian/emacs-el/usr/share/emacs/$(runtime_ver)/lisp/version.el.gz; \
	fi

override_dh_testdir:
	dh_testdir debian/emacsVAR.postinst

override_dh_auto_clean: $(persistent_autogen_files)
	true

override_dh_clean: $(persistent_autogen_files)
	dh_clean
	rm -rf \
	  debian/stamp-* \
	  debian/build-lucid \
	  debian/build-nox \
	  debian/build-src \
	  debian/build-gtk \
	  debian/build-pgtk \
	  debian/elgz-canary \
	  debian/elgz-info \
	  debian/emacs-common.README.00 \
	  debian/emacs-common.README.01 \
	  debian/install-lucid \
	  debian/install-nox \
	  debian/install-gtk \
	  debian/install-pgtk
	rm -f $(nonpersistent_autogen_files)