#!/usr/bin/make -f


LOCALE_PATH=debian/tmpdir/usr/lib/locale
LOCALE_NAME=en_US
LOCALE_CHARSET=UTF-8
HOME = $(shell pwd)
export HOME

export SHELL=/bin/bash
export DH_VERBOSE=1

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

export sbcl_arch=$(shell dpkg-architecture -qDEB_BUILD_ARCH |sed s/i386/x86/)
ARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq (${ARCH},powerpc)
   CFLAGS:=-O2
endif

pkg=sbcl
TARGET=debian/sbcl
SOURCE_TARGET=debian/sbcl-source
DOC_TARGET=debian/sbcl-doc
DESTDIR=$(CURDIR)/debian/tmp/usr

ARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)

FEATURES=":sb-core-compression"

ifneq (,$(filter x86 amd64,$(sbcl_arch)))
	FEATURES += " :sb-thread :sb-futex"
endif

ifneq (,$(filter kfreebsd-%,$(sbcl_arch)))
	FEATURES += " :os-provides-dlopen"
endif

configure: configure-stamp
configure-stamp:
	echo "(lambda (features) (setf features (union features (list $(FEATURES)))))" > customize-target-features.lisp

	cat customize-target-features.lisp

	dh_testdir

	touch configure-stamp

build: configure-stamp build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: 
	dh_testdir
#   create the locale:
	mkdir -p ${LOCALE_PATH}
	localedef -i "${LOCALE_NAME}" -f "${LOCALE_CHARSET}" "${LOCALE_PATH}/${LOCALE_NAME}.${LOCALE_CHARSET}"
	export LC_ALL=${LOCALE_NAME}.${LOCALE_CHARSET}
	export LANG=en_US.UTF-8
	export LOCPATH=${LOCALE_PATH} 
#   Using the locale

	+echo environment:
	env
	+echo

#   make scripts runable
	chmod a+x ./src/runtime/linux-nm ./make.sh ./clean.sh ./tools-for-build/canonicalize-whitespace
	chmod a+x ./tools-for-build/whitespacely-canonical-filenames ./tools-for-build/grep-noncanonical-whitespace  
	chmod a+x ./tools-for-build/check-canonical-whitespace ./tools-for-build/canonicalize-whitespace-1
	echo "using sbcl to rebuild with core /usr/lib/sbcl/sbcl-dist.core, checking"
	ls -l /usr/bin/sbcl /usr/lib/sbcl/sbcl.core
	CFLAGS="-DSBCL_HOME=`pwd`/stage1/ -O2" GNUMAKE=make ./make.sh '/usr/bin/sbcl --core /usr/lib/sbcl/sbcl.core  --sysinit /dev/null --userinit /dev/null --disable-debugger'
	mkdir stage1
	mv output/sbcl.core stage1/stage1.cor_
	mv src/runtime/sbcl stage1/stage1
#   save stage1
	tar cvf stage1.tar stage1/
	rm -rf stage1 || true
	sh clean.sh || true
#   restore stage1
	tar xvf stage1.tar
	rm stage1.tar
	echo "rebuild again with new version"
	CFLAGS="-DSBCL_HOME=/usr/lib/sbcl/ -O2" GNUMAKE=make ./make.sh --prefix="/usr/" --xc-host="`pwd`/stage1/stage1 --core `pwd`/stage1/stage1.cor_ --sysinit /dev/null --userinit /dev/null --disable-debugger"

	make -C doc/internals all html

#   start running tests
	-GNUMAKE=make sh -c 'cd tests && sh ./run-tests.sh' || printf "the tests failed\n"
#   see what the result is
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp: 
	dh_testdir
	cd doc/manual && make docstrings && make
	touch build-indep-stamp

clean:
	dh_testdir
	dh_testroot

	rm -rf target || true
	rm -rf stage1 || true
	rm -rf debian/tmpdir || true
	rm -rf .fontconfig || true
#   Add here commands to clean up after the build process.
	GNUMAKE=make sh clean.sh || true
	(cd src/runtime ; touch Config ; make clean ) || true
#   clean up CVS stuff
	rmdir contrib/systems/ obj/ output/ || true

	make -C doc/internals clean

	dh_clean

install:  build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	INSTALL_ROOT="$(DESTDIR)" sh install.sh

	dh_install

	cp debian/binfmt $(TARGET)/usr/share/binfmts/sbcl
	cp debian/sbcl-run $(TARGET)/usr/lib/sbcl
	chmod a+x $(TARGET)/usr/lib/sbcl/sbcl-run

	find $(TARGET)/usr/lib/sbcl/ -type f -name \*.c -exec chmod -x '{}' \;

#   move the indep stuff
	dh_installinfo -psbcl-doc $(DESTDIR)/share/info/sbcl.info 
	rm $(DESTDIR)/share/info/asdf.info $(DESTDIR)/share/info/sbcl.info
	rm $(DESTDIR)/share/doc/sbcl/asdf.pdf

	cp debian/index-docs.html \
		$(DOC_TARGET)/usr/share/doc/sbcl-doc/html/index.html

	rm -f $(SOURCE_TARGET)/usr/share/sbcl-source/contrib/asdf-install/asdf-install	
	rm -f $(SOURCE_TARGET)/usr/share/sbcl-source/contrib/asdf/LICENSE


	find $(SOURCE_TARGET)/usr/share/sbcl-source -type f -name \*.fasl -or -name \*.o -or -name \*.log \
		-or -name \*.so -or -name a.out | xargs rm
	find $(SOURCE_TARGET)/usr/share/sbcl-source/ -type f -name \*.c -exec chmod -x '{}' \;
	rm $(SOURCE_TARGET)/usr/share/sbcl-source/src/runtime/sbcl  
	rm $(SOURCE_TARGET)/usr/share/sbcl-source/src/runtime/sbcl.nm 

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installdebconf -i
	dh_installcron -i
	dh_installinfo -i
	dh_installman -i
	dh_link -i
	dh_lintian -i
	dh_compress -i -X *.html
	dh_fixperms -i
	find $(TARGET) $(SOURCE_TARGET) $(DOC_TARGET) -name .cvsignore -print0 | xargs -t0 rm -rf || true
	find $(TARGET) $(SOURCE_TARGET) $(DOC_TARGET) -name CVS -print0 | xargs -t0 rm -rf || true
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	find $(TARGET) $(SOURCE_TARGET) $(DOC_TARGET) -name CVS -or -name .cvsignore -print0 | xargs -t0 rm -rf || true
	dh_installman -a
	dh_strip -a
	dh_link -a
	dh_lintian -a
	dh_compress -a
	dh_fixperms -a
	dh_lisp sbcl
	dh_installdeb -a
	dh_shlibdeps -a

#   add the FASL version of this release to substvars
	stage1/stage1 --core stage1/stage1.cor_ --load "debian/fasl-version.lisp"

	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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