#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
ifeq ($(DEB_HOST_ARCH),amd64)
  SCHEME_IMPLEMENTATIONS=gambc chez mitscheme
else
  SCHEME_IMPLEMENTATIONS=gambc chez
endif

export PERL_HASH_SEED = 0

%:
	dh $@

# configure script does not accept usual --build, etc. options.
# Scheme sources should be installed in /usr/share not /usr/lib.
override_dh_auto_configure:
	./configure --prefix=/usr --libdir=/usr/share

execute_before_dh_auto_build:
	touch -r schmooz.scm slib.texi  # no rebuild of .txi files is needed
	touch clrnamdb.scm  # clrnamdb.scm does not need to be rebuilt
	touch collectx.scm  # collectx.scm needs scm, which is not a build dep

execute_after_dh_auto_build:
	$(MAKE) slib.info
	$(MAKE) html

execute_after_dh_install:
# 	Remove unwanted info/dir.gz
	rm -f debian/slib/usr/share/info/dir*
# 	Remove configure script, etc. installed by upstream makefile
	for i in configure slib.sh syncase.sh Makefile slib.nsi slib.spec; do \
	  rm -f debian/slib/usr/share/slib/$$i; \
	done
# 	Generate slibcat under /var/lib/slib for each supported Scheme
	for i in $(SCHEME_IMPLEMENTATIONS); do \
	  mkdir -p debian/slib/var/lib/slib/$$i; \
	done
# 	Generate slibcat for Gambit
	SCHEME_LIBRARY_PATH=$(CURDIR)/debian/slib/usr/share/slib/ \
	  GAMBIT_IMPLEMENTATION_PATH=debian/slib/var/lib/slib/gambc/ \
	  gsi -f gambit.init -e "(require 'new-catalog)"
#	Generate slibcat for Chez
	echo "(require 'new-catalog)" | \
	  SCHEME_LIBRARY_PATH=$(CURDIR)/debian/slib/usr/share/slib/ \
	  CHEZ_IMPLEMENTATION_PATH=debian/slib/var/lib/slib/chez/ \
	  chezscheme -q chez.init
ifeq ($(DEB_HOST_ARCH),amd64)
#	Generate slibcat for MIT Scheme
	echo "(require 'new-catalog)" | \
	  SCHEME_LIBRARY_PATH=$(CURDIR)/debian/slib/usr/share/slib/ \
	  MITSCHEME_IMPLEMENTATION_PATH=debian/slib/var/lib/slib/mitscheme/ \
	  mit-scheme --batch-mode --no-init-file --load mitscheme.init
endif
#	Strip the temporary DESTDIR from the slibcat paths
	for i in $(SCHEME_IMPLEMENTATIONS); do \
	  sed -i 's|$(CURDIR)/debian/slib||' debian/slib/var/lib/slib/$$i/slibcat; \
	done
