#!/usr/bin/make -f

SHELL := /bin/bash

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk

export ARCH_OPTS := $(CFLAGS)


%:
	dh $@

override_dh_testdir:
	dh_testdir stalin.sc

# Symlink the prebuilt file for the current architecture.  It may be
# that if the ./stalin-architecture output matches for two
# architectures, then the .c files will be the same, but all of the
# following matches were determined by creating the relevant
# stalin-arch-ARCH.c file (via "make stalin-arch-ARCH.c" after adding
# the entry to include/stalin.architectures) and then diffing it
# against existing prebuilt files.
stalin-arch-amd64.c:
	ln -sf debian/prebuilt-src/stalin-arch-amd64.c $@
stalin-arch-arm64.c:
	ln -sf debian/prebuilt-src/stalin-arch-amd64.c $@
stalin-arch-ia64.c:
	ln -sf debian/prebuilt-src/stalin-arch-amd64.c $@
stalin-arch-i386.c:
	ln -sf debian/prebuilt-src/stalin-arch-i386.c $@
stalin-arch-sparc.c:
	cp -a debian/prebuilt-src/stalin-arch-sparc.c $@.tmp
	patch $@.tmp debian/prebuilt-src/stalin-arch-sparc-from-i386.diff
	mv $@.tmp $@

override_dh_auto_build: stalin-arch-$(DEB_HOST_ARCH).c
	./build
	./build-gl-fpi

        # Tests
	rm -rf debian/tmp-test
	mkdir debian/tmp-test
	echo '(display "hello") (newline)' > debian/tmp-test/hello.scm
	cd debian/tmp-test && $(CURDIR)/stalin -On hello.scm
	test -x debian/tmp-test/hello
	test $$(debian/tmp-test/hello) = "hello"

        # These don't work on all debian platforms yet.
        #cd benchmarks && ./compile-and-run-stalin-old-benchmarks

override_dh_auto_install:
	rm -rf debian/stalin
	install -d debian/stalin

        # Binaries
	install -d debian/stalin/usr/bin
	cp debian/stalin-script debian/stalin/usr/bin/stalin

        # libs (some of these should be in share, but stalin doesn't make the
        # distinction ATM)
	install -d debian/stalin/usr/lib/stalin
	cp -r include/* debian/stalin/usr/lib/stalin
	install -s include/stalin debian/stalin/usr/lib/stalin/

	dh_auto_install

override_dh_clean:
	dh_clean
	rm -rf debian/tmp-test stalin-arch-$(DEB_HOST_ARCH).c

# Local variables:
# mode: makefile
# End:
