#!/usr/bin/make -f
# debian/rules file for Guile.
#
# This file was derived from a public domain file, originally written
# by Joey Hess (from file included in examples in debhelper package).
#
# You can build any of the binary packages independantly, via
# binary-<package> targets.

SHELL := /bin/bash

PACKAGE := guile-1.6
upstream_archive := guile-1.6_1.6.8.orig.tar.gz
upstream_archive_dir := guile-1.6.8

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

export DH_COMPAT := 4

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

quilt := QUILT_PATCHES=debian/patches quilt

deb_trash :=

# If you change this please change the debian/control qthreads entry and the
# arch-specific .install targets below too.
WORKING_QTHREADS_ARCHS := i386 alpha

ifeq ($(findstring ${DEB_HOST_ARCH},${WORKING_QTHREADS_ARCHS}),${DEB_HOST_ARCH})
ENABLE_THREADS := --with-threads=yes
endif

QTHREADS_DOT_INSTALLS := \
  $(foreach arch,${WORKING_QTHREADS_ARCHS},debian/guile-1.6-dev.install.${arch})

configure_args = \
	--host=$(DEB_HOST_GNU_TYPE) \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--prefix=/usr \
	--with-gnu-ld \
	--enable-dynamic-linking \
	${ENABLE_THREADS}

# Use -O1.  Otherwise guile may use up too much stack on some
# architectures.
#
# http://lists.gnu.org/archive/html/guile-devel/2006-10/msg00007.html
#
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	configure_args += CFLAGS="-O1 -g"
endif

# It looks like Debian's packaging tools can't handle an empty file,
# so we use need-empty-autofiles-diff (non-empty) to know when we
# should create an empty autofiles.diff.

define checkdir
  @if ! test -e debian/patches/autofiles.diff; \
  then \
    if test -e debian/need-empty-autofiles-diff; \
    then \
      touch debian/patches/autofiles.diff; \
    else \
      echo; \
      echo "The Debian autofiles patch is not available."; \
      echo "Please run this command:"; \
      echo "  debian/rules autofiles-sync"; \
      echo; \
      false; \
    fi; \
  fi
  dpkg-parsechangelog > /dev/null
  dh_testdir debian/guile-1.6.postinst
  @if ! test -f libguile/guile.c; \
  then \
    echo; \
    echo -n "The upstream source does not appear to be available."; \
    echo "  Please put the contents"; \
    echo -n "of $(upstream_archive) into the"; \
    echo " current directory."; \
    echo; \
    false; \
  fi
endef

define patch
  $(quilt) push -a
endef

define unpatch
  test -z "$$($(quilt) applied)" || $(quilt) pop -a
  rm -f debian/stamp/patch
  rm -rf .pc
endef

define cleanup_all
  #   this goes first so we clean using patched code
  rm -f debian/stamp/build
  -$(MAKE) clean
  rm -f debian/stamp/config
  -$(MAKE) distclean
  rm -f ${QTHREADS_DOT_INSTALLS}
  rm -f debian/libqthreads-12.install.*
  dh_clean
  $(unpatch)
  #   now clean up everything else (mostly ./debian related stuff)
  find -name '*~' | xargs --no-run-if-empty rm -f
  rm -rf $(deb_trash)
  test ! -e debian/stamp || rmdir debian/stamp
endef

define update_debian_autofiles_quilt
  $(cleanup_all)
  if ! test -e debian/patches/autofiles.diff; \
  then \
    touch debian/patches/autofiles.diff; \
  fi;
  $(patch)
  test "$$($(quilt) top)" = autofiles.diff
  $(quilt) pop
  mkdir -p debian/tmp-autofiles/old
  tar cpSf - --exclude ./debian --exclude ./.pc . \
    | tar -C debian/tmp-autofiles/old -xpSf -
  cp -a debian/tmp-autofiles/old debian/tmp-autofiles/new
  # rm aclocal.m4 so it doesn't confuse newer autoconfs, but touch it
  # so ./Makefile won't be upset if it's not recreated (b/c not needed).
  #cd debian/tmp-autofiles/new && rm -f aclocal.m4
  #cd debian/tmp-autofiles/new && touch aclocal.m4
  cd debian/tmp-autofiles/new && ./guile-aclocal.sh
  cd debian/tmp-autofiles/new && libtoolize --force --copy --automake
  cd debian/tmp-autofiles/new && autoheader
  cd debian/tmp-autofiles/new && autoconf
  cd debian/tmp-autofiles/new && automake --add-missing

  cd debian/tmp-autofiles/new/guile-readline && aclocal
  cd debian/tmp-autofiles/new/guile-readline && libtoolize --copy --automake
  cd debian/tmp-autofiles/new/guile-readline && autoconf
  cd debian/tmp-autofiles/new/guile-readline && automake --add-missing

  cd debian/tmp-autofiles/new && rm -rf autom4te.cache
  cd debian/tmp-autofiles/new/guile-readline && rm -rf autom4te.cache
  cd debian/tmp-autofiles && diff -ruN old new > new.diff; \
    test $$? -eq 1 -o $$? -eq 0
  set -e; \
  if test -s debian/tmp-autofiles/new.diff; \
  then \
    $(quilt) import -f -P autofiles.diff debian/tmp-autofiles/new.diff; \
  else \
    echo yes > debian/need-empty-autofiles-diff; \
    echo -n > debian/patches/autofiles.diff; \
  fi
  $(patch)
  $(cleanup_all)
endef
deb_trash += debian/tmp-autofiles

# we don't sync this automatically on clean because it's expensive and
# should only be done by a maintainer with time to deal with any mess
# it might create.
autofiles-sync:
	$(update_debian_autofiles_quilt)
.PHONY: autofiles-sync

.PHONY: check-diff
check-diff:
	$(checkdir)
	$(cleanup_all)
	@test -r ../$(upstream_archive)
	@rm -rf debian/tmp-diff && mkdir debian/tmp-diff
	@cd debian/tmp-diff && tar xzpSf ../../../$(upstream_archive)
	@cd debian/tmp-diff && mv $(upstream_archive_dir) orig

	mkdir debian/tmp-diff/new
	tar cpf - --exclude './debian' --exclude './.pc' . \
	  | (cd debian/tmp-diff/new && tar xpf -)

	@echo
	@echo "########################################"
	@echo "### Diffs outside ./debian"
	@cd debian/tmp-diff && diff -ruN orig new
	@echo "########################################"
	@echo "### Empty file list differences"
	@diff -u \
	  <(cd debian/tmp-diff/orig && find -size 0) \
	  <(cd debian/tmp-diff/new && find -size 0) \
	    > debian/tmp-diff/empty-files.diff || test $$? -eq 1
	@cat debian/tmp-diff/empty-files.diff
	@test ! -s debian/tmp-diff/empty-files.diff || false
	@echo "########################################"
	@rm -rf debian/tmp-diff

deb_trash += debian/tmp-diff

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

${QTHREADS_DOT_INSTALLS}: debian/guile-1.6-dev.install
	cat $< > $@ && echo debian/tmp/usr/lib/libqthreads.la >> $@

debian/stamp/patch:
	$(checkdir)
	$(patch)
	mkdir -p $(dir $@) && touch $@

debian/stamp/config: debian/stamp/patch ${QTHREADS_DOT_INSTALLS}
	$(foreach arch,${WORKING_QTHREADS_ARCHS},\
	  echo 'debian/tmp/usr/lib/libqthreads.so.*' \
	    > debian/libqthreads-12.install.${arch};)
	./configure ${configure_args}
	mkdir -p $(dir $@) && touch $@

build: debian/stamp/build
debian/stamp/build: debian/stamp/config
	$(checkdir)
	$(MAKE)
	$(MAKE) check
	mkdir -p $(dir $@) && touch $@

clean:
	$(checkdir)
	dh_testroot
	$(cleanup_all)

install: DH_OPTIONS=
install: build
	$(checkdir)
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	dh_install

	perl -pi \
	  -e 's|^#!\s*/usr/bin/guile([^-])|#!/usr/bin/guile-1.6$$1|o' \
	  -e ' if $$. == 1;' \
	  debian/guile-1.6-dev/usr/bin/guile-config \
	  debian/guile-1.6-dev/usr/bin/guile-snarf \
	  debian/guile-1.6-dev/usr/bin/guile-tools

	cd debian/guile-1.6/usr/bin/ && mv guile guile-1.6

	mkdir -p debian/guile-1.6-slib/usr/share/guile/1.6
	cd debian/guile-1.6-slib/usr/share/guile/1.6 && ln -s ../../slib .

# This single target is used to build all the packages, all at once, or
# one at a time. So keep in mind: any options passed to commands here will
# affect _all_ packages. Anything you want to only affect one package
# should be put in another target, such as the install target.
binary-common:
	$(checkdir)
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installman
#	dh_installcron
	dh_installinfo
        # handle http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=337215
	perl -wpi -e 's/\\Q//gmo; s/\\E//gmo' \
	  debian/guile-1.6-doc.postinst.debhelper
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build install
	 $(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

# Any other binary targets build just one binary package at a time.
binary-%: build install
	make -f debian/rules binary-common DH_OPTIONS=-p$*

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
