#!/usr/bin/make -f
# debian/rules file for guile-X.Y-non-dfsg.

SHELL := /bin/bash

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

deb_src_maj_ver := 1
deb_src_min_ver := 8
deb_src_mic_ver := 8
deb_src_rev := 1

upstream_ver := $(deb_src_maj_ver).$(deb_src_min_ver).$(deb_src_mic_ver)

deb_src_ver := $(upstream_ver)+$(deb_src_rev)
deb_src_eff_ver := $(deb_src_maj_ver).$(deb_src_min_ver)
deb_pkg_basename := guile-$(deb_src_eff_ver)
deb_doc_pkg := $(deb_pkg_basename)-doc-non-dfsg

# If we ever need it, we can create a copy that doesn't assume ./debian/
define deb_sub
  perl -p \
    -e "s|\@UPSTREAM_VER\@|$(upstream_ver)|go;" \
    -e "s|\@DEB_SRC_VER\@|$(deb_src_ver)|go;" \
    -e "s|\@DEB_SRC_MAJ_VER\@|$(deb_src_maj_ver)|go;" \
    -e "s|\@DEB_SRC_MIN_VER\@|$(deb_src_min_ver)|go;" \
    -e "s|\@DEB_SRC_MIC_VER\@|$(deb_src_mic_ver)|go;" \
    -e "s|\@DEB_SRC_EFF_VER\@|$(deb_src_eff_ver)|go;" \
    -e "s|\@DEB_PKG_BASENAME\@|$(deb_pkg_basename)|go;" \
      < $(1) > $(2)
endef

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

configure_args = \
  --host=$(DEB_HOST_GNU_TYPE) \
  --build=$(DEB_BUILD_GNU_TYPE) \
  --prefix=/usr

define checkdir
  dh_testdir GUILE-VERSION debian/changelog
endef

%:
	dh $@ --parallel --with autoreconf

.PHONY: buildpackage
buildpackage:
	$(checkdir)
	dpkg-buildpackage -D -us -uc -rfakeroot -i'\.git|\.pc'

.PHONY: check-vars
check-vars:
	@echo deb_src_ver: $(deb_src_ver)
	@echo deb_src_maj_ver: $(deb_src_maj_ver)
	@echo deb_src_min_ver: $(deb_src_min_ver)
	@echo deb_src_mic_ver: $(deb_src_mic_ver)
	@echo deb_pkg_basenmae: $(deb_pkg_basename)

autogen_install_files := $(addprefix debian/, \
 guile-$(deb_src_eff_ver)-doc-non-dfsg.info \
 guile-$(deb_src_eff_ver)-doc-non-dfsg.install)

$(autogen_install_files): debian/rules

debian/guile-$(deb_src_eff_ver)-doc-non-dfsg.%: debian/guile-doc.%
	$(call deb_sub,$<,$@)

override_dh_testdir:
	$(checkdir)

override_dh_autoreconf:
	cd doc/goops  && ln -sf goops.texi goops-$(deb_src_eff_ver).texi
	cd doc/ref  && ln -sf guile.texi guile-$(deb_src_eff_ver).texi
	dh_autoreconf -- autoreconf -i --force --verbose

override_dh_auto_configure:
	./configure ${configure_args}

override_dh_install: $(autogen_install_files)
	dh_install --fail-missing -Xusr/share/info/dir
	rm debian/$(deb_doc_pkg)/usr/share/info/dir

override_dh_clean:
	dh_clean $(autogen_install_files) \
	  doc/goops/goops-$(deb_src_eff_ver).texi \
	  doc/ref/guile-$(deb_src_eff_ver).texi \
	  doc/goops/goops-1.8.info \
	  doc/ref/guile-1.8.info \
	  doc/ref/guile-1.8.info-1 \
	  doc/ref/guile-1.8.info-2 \
	  doc/ref/guile-1.8.info-3 \
	  doc/ref/guile-1.8.info-4 \
	  doc/ref/guile-1.8.info-5 \
	  doc/ref/guile-1.8.info-6 \
	  doc/ref/guile-1.8.info-7
