#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.

DEB_HOST_MULTIARCH?=	$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Aim for the top (almost), adapt if anything should break on the buildds.
DEB_BUILD_MAINT_OPTIONS=	hardening=+all,-pie
export DEB_BUILD_MAINT_OPTIONS

GFORTH_VER = `sed -e 's/^[^ ]\+ (\([^)+-]\+\).*/\1/; 1q' debian/changelog`
D = $(CURDIR)/debian/gforth
DC = $(CURDIR)/debian/gforth-common
DL = $(CURDIR)/debian/gforth-lib
ULIB = $D/usr/lib/$(DEB_HOST_MULTIARCH)/gforth/$(GFORTH_VER)
USHARE = $D/usr/share/gforth/$(GFORTH_VER)

# arch-specific options
common_opts :=--with-lispdir=/usr/share/emacs/site-lisp/gforth
m68k_opts   :=--enable-force-reg 

configure_options := $(common_opts)

ifeq ($(DEB_BUILD_ARCH),m68k)
configure_options = $(common_opts) $(m68k_opts)
endif

shellout := $(shell ls -d debian/kernl-save 2> /dev/null)
save-dir=frog$(shellout)

override_dh_auto_configure:
ifeq ($(save-dir),frog)
	install -d debian/kernl-save
	cp kernl*.fi debian/kernl-save
endif
	no_dynamic_default=1 dh_auto_configure -- $(configure_options)

override_dh_auto_clean:
	dh_auto_clean
ifneq ($(save-dir),frog)
	cp debian/kernl-save/* .
	rm -r debian/kernl-save
endif

override_dh_auto_install:
	touch $D/usr/lib/gforth/site-forth/site-init.fs
	dh_auto_install -- DESTDIR=$D
	chmod 755 $(USHARE)/filedump.fs $(USHARE)/httpd.fs $(USHARE)/sieve.fs \
		$(USHARE)/kernl*.fi $(ULIB)/gforth.fi
	mkdir -p $(DC)/usr/share
	mv $D/usr/share/gforth $(DC)/usr/share/gforth
	mkdir -p $(DL)/usr/lib
	mv $D/usr/lib/$(DEB_HOST_MULTIARCH) $(DL)/usr/lib/$(DEB_HOST_MULTIARCH)
	find $D $(DC) $(DL) -type d -empty -print | xargs rmdir
	find $D $(DC) $(DL) -name '*.la' -print | xargs rm

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_makeshlibs:
	# This is somewhat ugly - we only need the misc:Pre-Depends part of
	# dh_makeshlibs, but we have to put up with invoking it anyway :)
	dh_makeshlibs -pgforth-lib -n
	rm -f $(CURDIR)/debian/gforth-lib/DEBIAN/shlibs

%:
	dh $@ --with autoreconf
