#!/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
export DEB_BUILD_MAINT_OPTIONS

GFORTH_VER = `sed -e 's/^[^ ]\+ (\([^)+-]\+\).*/\1/; 1q' debian/changelog`
D = $(CURDIR)/debian/tmp
DG = $(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)

packages := $(shell dh_listpackages)

# arch-specific options
sitelispdir :=/usr/share/emacs/site-lisp/gforth
common_opts :=--with-lispdir='${sitelispdir}'
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:
	mkdir -p $D/usr/lib/gforth/site-forth
	touch $D/usr/lib/gforth/site-forth/site-init.fs
	mkdir -p $D${sitelispdir}
	dh_auto_install
	chmod 755 $(USHARE)/filedump.fs $(USHARE)/httpd.fs $(USHARE)/sieve.fs \
		$(USHARE)/kernl*.fi $(ULIB)/gforth.fi

prunedirs=
ifneq (,$(filter gforth-common,$(packages)))
prunedirs+=	$(DC)
endif
ifneq (,$(filter gforth-lib,$(packages)))
prunedirs+=	$(DL)
endif
ifneq (,$(filter gforth,$(packages)))
prunedirs+=	$(DG)
endif

override_dh_install:
ifneq (,$(filter gforth-common,$(packages)))
	mkdir -p $(DC)/usr/share
	mv $D/usr/share/gforth $(DC)/usr/share/gforth
else
	rm -rf $D/usr/share/gforth
endif
ifneq (,$(filter gforth-lib,$(packages)))
	mkdir -p $(DL)/usr/lib
	mv $D/usr/lib/$(DEB_HOST_MULTIARCH) $(DL)/usr/lib/$(DEB_HOST_MULTIARCH)
else
	rm -rf $D/usr/lib/$(DEB_HOST_MULTIARCH)
endif
ifneq (,$(filter gforth,$(packages)))
	mkdir -p $(DG)
	mv $D/* $(DG)/
endif
ifneq (,$(prunedirs))
	find $(prunedirs) -type d -empty -delete
	find $(prunedirs) -name '*.la' -delete
endif

override_dh_installchangelogs:
	dh_installchangelogs -X ChangeLog
	set -e; for p in $$(dh_listpackages); do \
		install -m 644 NEWS "debian/$$p/usr/share/doc/$$p/"; \
	done

override_dh_makeshlibs:

override_dh_missing:
	dh_missing --fail-missing

%:
	dh $@ --no-parallel
