#!/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-2012 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.

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

SHELL := /bin/bash
quilt := QUILT_PATCHES=debian/patches 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.

# The official upstream version defined by AC_INIT in configure.in.
upstream_ver := 24.3

# This must be the version that's actually used at runtime for things
# like load-path.  Historically, it's only differed from upstream_ver
# by a trailing letter, i.e. 20.5a -> 20.5.
runtime_ver := $(shell echo $(upstream_ver) | perl -pe 's/[a-z]+$$//o')
major_ver := $(shell echo $(runtime_ver) | perl -pe 's/\..*$$//o')
minor_ver := $(shell echo $(runtime_ver) | perl -pe 's/^[^.]*\.//o')

# The name of the Debian source package
src_name := $(shell dpkg-parsechangelog | egrep '^Source:')
src_name := $(shell echo $(src_name) | perl -pe 's/Source:\s+//o')

# The version from the changelog (i.e. 20.5-1)
debian_ver := $(shell dpkg-parsechangelog | egrep '^Version:')
debian_ver := $(shell echo $(debian_ver) | perl -pe 's/Version:\s+//o')

# The Debian revision (i.e. the 1 from 20.5-1)
# Always everything after the last '-'
debian_rev := $(shell 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 echo $(debian_ver) | perl -pe 's/-[^-]+$$//o')

# The flavor (i.e. emacs21) currently matches the source package name.
flavor := $(shell echo $(src_name) | perl -pe 's/-non-dfsg$$//o')

# These files must always exist, i.e. can't ever be cleaned.
persistent_autogen_files := debian/control debian/copyright
nonpersistent_autogen_files := debian/$(flavor)-common-non-dfsg.README.Debian
autogen_files := $(persistent_autogen_files) $(nonpersistent_autogen_files)

pkgdir_common := $(CURDIR)/debian/$(flavor)-common-non-dfsg
common_etc_dir := $(pkgdir_common)/usr/share/emacs/$(runtime_ver)/etc

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|\@DEB_FLAVOR\@|$(flavor)|go;" \
      < debian/$(1) > debian/$(2)
endef

%:
	dh $@ --parallel

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

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

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

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

define gen_makefile
  perl -p \
    -e "s|\@top_srcdir\@|$(CURDIR)|go;" \
    -e "s|\@SHELL\@|/bin/bash|go;" \
    -e "s|\@srcdir\@|$(CURDIR)/$1|go;" \
    -e "s|\@VPATH\@|$(CURDIR)/$1|go;" \
    -e "s|\@MAKEINFO\@|makeinfo|go;" \
    -e "s|\@MKDIR_P\@|mkdir -p|go;" \
    -e "s|\@INFO_OPTS\@|--no-split|go;" \
    -e "s|\@INFO_EXT\@|.info|go;" \
    < $1/Makefile.in > $1/Makefile
endef

override_dh_auto_configure: $(autogen_files)
	$(call gen_makefile,doc/emacs)
	$(call gen_makefile,doc/lispintro)
	$(call gen_makefile,doc/lispref)
	$(call gen_makefile,doc/misc)

override_dh_auto_build: $(autogen_files)
	rm -rf info && install -d info
	$(MAKE) -C doc/emacs
	$(MAKE) -C doc/lispintro
	$(MAKE) -C doc/lispref
	$(MAKE) -C doc/misc

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

override_dh_auto_install: pkg_name := $(flavor)-common-non-dfsg
override_dh_auto_install: $(autogen_files)
	rm -rf $(pkgdir_common) && install -d $(pkgdir_common)

        # info files
	install -d $(pkgdir_common)/usr/share/info/emacs-$(major_ver)
	cp -a info/* $(pkgdir_common)/usr/share/info/emacs-$(major_ver)/

        # etc files
	install -d $(common_etc_dir)
	cp -a etc/* $(common_etc_dir)/
	rm $(common_etc_dir)/COPYING

	install -d $(pkgdir_common)/usr/share/doc/$(flavor)-common-non-dfsg

        # Mangle info menu entries for subdir.
	chmod 755 debian/mangle-info
	DEBIAN_INFO_PREFIX=emacs-$(major_ver) \
	  debian/mangle-info $(pkg_infodir)/eintr.info
	DEBIAN_INFO_PREFIX=emacs-$(major_ver) \
	  debian/mangle-info $(pkg_infodir)/emacs.info
	DEBIAN_INFO_PREFIX=emacs-$(major_ver) \
	  debian/mangle-info $(pkg_infodir)/elisp.info
        # Handle doc/misc files.
	set -e; \
	set -x; \
	for f in $$(make -f doc/misc/Makefile -f debian/makefile-bit-doc-misc \
	              echo-info-targets \
	            | grep INFO_TARGETS= | cut -b 14-); \
	do \
	  DEBIAN_INFO_PREFIX=emacs-$(major_ver) \
	    debian/mangle-info $(pkg_infodir)/$${f%.info}.info; \
	done

	install -d $(pkgdir_common)/usr/share/lintian/overrides
	cp -a debian/emacs-common-non-dfsg.lintian-overrides \
	  $(pkgdir_common)/usr/share/lintian/overrides/$(flavor)-common-non-dfsg

override_dh_testdir:
	dh_testdir debian/emacs-common-non-dfsg.README etc/THE-GNU-PROJECT

override_dh_clean: $(persistent_autogen_files)
	rm -rf \
	  debian/*-stamp \
	  debian/*.tmp \
	  debian/emacs-common-non-dfsg.README.00 \
	  debian/emacs-common-non-dfsg.README.01 \
	  debian/emacs-common-non-dfsg.README.tmp \
	  doc/emacs/Makefile \
	  doc/lispintro/Makefile \
	  doc/lispref/Makefile \
	  doc/misc/Makefile \
	  info
	rm -f $(nonpersistent_autogen_files)
	dh_clean
