#!/usr/bin/make -f

## can't call $(shell ) here as the file is generated during the build
#BIGLOO_RELEASE=$$(grep ^RELEASE= ${BUILD_TREE}/Makefile.config | cut -d= -f2)
BIGLOO_RELEASE=$(shell /bin/ls bigloo*.tar.gz | sed 's/bigloo\([^-]*\)\(-.*\)*.tar.gz/\1/')

TAR_DIR = bigloo${BIGLOO_RELEASE}

PKG_VERSION=$(shell dpkg-parsechangelog | grep ^Version: | cut -d' ' -f2)

JVM_ARCHS=$(shell grep -E '^(Package|Architecture)' debian/control | grep -A1 bigloo-backend-jvm | grep ^Architecture|cut -d: -f2 | tr , ' ')

include /usr/share/dbs/dbs-build.mk

ifeq (,$(DEB_BUILD_GNU_TYPE))
 include /usr/share/dbs/dpkg-arch.mk
endif

EXTRA_CONFIG_FLAGS=
NO_PACKAGES=
CC=gcc

ARCH=$(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq (${ARCH},arm)
#CC=gcc-3.2
EXTRA_CONFIG_FLAGS += --coflags=-O2 --cpicflags=-fPIC
else
#CC=gcc
endif

ifeq (,$(filter $(ARCH),$(JVM_ARCHS)))
EXTRA_CONFIG_FLAGS += --jvm=no
NO_PACKAGES += -Nbigloo-backend-jvm
GCJ=
else
EXTRA_CONFIG_FLAGS += --javaprefix=/usr/lib/fjsdk/bin
GCJ=gcj
endif

export DH_OPTIONS
export LC_ALL=C

ELISPDIR=/usr/share/emacs/site-lisp/bigloo-ude

$(STAMP_DIR)/configure: $(patched)
	dh_testdir
	cd ${BUILD_TREE} && env ./configure ${EXTRA_CONFIG_FLAGS} --prefix=/usr \
			--mandir=/usr/share/man/man1 \
			--infodir=/usr/share/info \
			--ld=${CC} --cc=${CC} \
			--bee=full \
			--customgc=yes \
			--dotnet=no # for now
	touch $(STAMP_DIR)/configure

# FIXME: build-indep will be restored as a dependency of build only when
# autobuilders call build-arch directly, or we get a build-dep loop with
# scribe.  It is listed as an install-indep target, so that should not
# cause any problem, appart from having docs build only at "binary" time.
build: build-arch # build-indep

build-arch: $(STAMP_DIR)/build-arch
$(STAMP_DIR)/build-arch: $(STAMP_DIR)/configure
	dh_testdir

ifneq ($(GCJ),)
	mkdir -p ${BUILD_TREE}/bin
	$(GCJ) --main=jigloo -o ${BUILD_TREE}/bin/jigloo ${BUILD_TREE}/jigloo/jigloo.java
endif

	env \
		LD_LIBRARY_PATH=$(CURDIR)/${BUILD_TREE}/lib/${BIGLOO_RELEASE}:${LD_LIBRARY_PATH} \
			$(MAKE) -C $(BUILD_TREE)
	env \
		BIGLOOLIB=$(CURDIR)/${BUILD_TREE}/lib/${BIGLOO_RELEASE} \
		LD_LIBRARY_PATH=$(CURDIR)/${BUILD_TREE}/lib/${BIGLOO_RELEASE}:${LD_LIBRARY_PATH} \
		PATH=$(CURDIR)/${BUILD_TREE}/bin:$$PATH \
			$(MAKE) -C $(BUILD_TREE) compile-bee

	# do not fail if the tests do not pass, since we know the jvm mmap test fails
	-$(MAKE) -C $(BUILD_TREE) test

	touch $(STAMP_DIR)/build-arch

build-indep: $(STAMP_DIR)/build-indep
$(STAMP_DIR)/build-indep: $(STAMP_DIR)/build-arch
	dh_testdir

	$(MAKE) -C $(BUILD_TREE)/manuals info info-bee # html

	touch $(STAMP_DIR)/build-indep

clean:
	dh_testdir
	dh_testroot
	rm -rf $(STAMP_DIR) $(SOURCE_DIR)
	perl $(SCRIPT_DIR)/dbs_split clean
	rm -f debian/shlibs.local
	dh_clean

install-indep: DH_OPTIONS=-i
install-indep: install-arch build-indep
	dh_testdir
	dh_testroot
	dh_clean -k

	mkdir -p debian/tmp/usr/share/info
	$(MAKE) -C $(BUILD_TREE) install-docs \
		DESTDIR=$(CURDIR)/debian/tmp \
		DOCDIR=$(CURDIR)/debian/tmp/usr/share/doc/bigloo-doc
	$(MAKE) -C $(BUILD_TREE)/manuals install-info-bee \
		DESTDIR=$(CURDIR)/debian/tmp
	cp $(BUILD_TREE)/manuals/bdb*html debian/tmp/usr/share/doc/bigloo-doc
	$(MAKE) -C $(BUILD_TREE)/manuals uninstall-man \
		DESTDIR=$(CURDIR)/debian/tmp
	rm -f debian/tmp/usr/share/info/dir

 # fixes
#	cd debian/tmp/usr/share/info && rm -f dir dir.old
	rm debian/tmp/usr/share/doc/bigloo-doc/r5rs*
	rm -f debian/tmp/usr/share/doc/bigloo-doc/*.sui

 # source for emacs environment
  # install source
	for f in `cd $(BUILD_TREE)/bmacs && find -name '*.el'`; do \
		mkdir -p debian/tmp${ELISPDIR}/`dirname $$f`; \
		cp $(BUILD_TREE)/bmacs/$$f debian/tmp${ELISPDIR}/$$f; \
	done
	cp -a $(BUILD_TREE)/bmacs/Makefile $(BUILD_TREE)/bmacs/etc debian/tmp${ELISPDIR}/
  # neutralize that rule to avoid refering to BOOTDIR at install-time
	perl -pi -e 's/^bmacs-config.el/xbmacs-config.el/' \
		debian/tmp${ELISPDIR}/Makefile

	dh_movefiles
	test -z "`find debian/tmp -not -type d | tee /dev/fd/2`"

  # create symlinks to easily locate the doc
	mkdir -p debian/bigloo-doc/usr/share/doc/bigloo
	ln -s ../bigloo-doc debian/bigloo-doc/usr/share/doc/bigloo/manuals
	mkdir -p debian/bigloo-doc/usr/share/doc/bigloo-devtools
	ln -s ../bigloo-doc debian/bigloo-doc/usr/share/doc/bigloo-devtools/manuals
	mkdir -p debian/bigloo-examples/usr/share/doc/bigloo
	ln -s ../bigloo-examples debian/bigloo-examples/usr/share/doc/bigloo/examples

install-arch: DH_OPTIONS=-a
install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	mkdir debian/tmp
	cd debian/tmp && mkdir -p usr/bin usr/lib usr/share/info

	$(MAKE) -C $(BUILD_TREE) install-progs install-bee DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C $(BUILD_TREE)/manuals install-man install-bee DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C $(BUILD_TREE)/manuals uninstall-info-bee DESTDIR=$(CURDIR)/debian/tmp

 # fixes
	rm -f debian/tmp/usr/bin/jas
	chmod 644 debian/tmp/usr/lib/bigloo/$(BIGLOO_RELEASE)/*
	find debian/tmp -name '*.css' | xargs chmod 644

	cd debian/tmp/usr/lib && \
	  for f in *.so ; do \
	    rm $$f && ln -s bigloo/$(BIGLOO_RELEASE)/$$f $$f ; \
	  done

 # create a symlink binary named "bigloo" - upstream makes a hardlink
	rm -f debian/tmp/usr/bin/bigloo
	cd debian/tmp/usr/bin && ln -s bigloo$(BIGLOO_RELEASE) bigloo

 # removes info files, they're for arch-indep
	rm -f debian/tmp/usr/info/bigloo.info*

	dh_movefiles

ifneq ($(GCJ),) 
 # java program built natively
	DH_OPTIONS= dh_install -pbigloo-devtools $(BUILD_TREE)/bin/jigloo usr/bin
endif

	test -z "`find debian/tmp ! -type d | tee /dev/fd/2`"

 # make an easy-to-locate symlink for use by the ude package
	 ln -s $(BIGLOO_RELEASE)/Makefile.config \
		debian/bigloo/usr/lib/bigloo/Makefile.config

	(cd debian/libbigloo${BIGLOO_RELEASE}/usr/lib/bigloo/$(BIGLOO_RELEASE) && \
	  for f in *-${BIGLOO_RELEASE}.so ; do \
		ln -s $$f $$(echo $$f | sed "s/-${BIGLOO_RELEASE}//") ; \
	  done )


binary-indep: DH_OPTIONS=-i
binary-indep: install-indep
	dh_testdir
	dh_testroot
	dh_installchangelogs $(BUILD_TREE)/ChangeLog
	dh_installdocs
	dh_buildinfo

	dh_installexamples
 # have Makefiles use the right location for Makefile.config
	find debian/bigloo-examples/usr/share/doc/bigloo-examples -name Makefile |\
	  xargs perl -pi -e 's,^include [./]*Makefile.config$$,/usr/lib/bigloo/Makefile.config,; s/BOOTBINDIR/BINDIR/g; s/BOOTLIBDIR/FILDIR/g;'
	cp -a $(BUILD_TREE)/bdl/example debian/bigloo-examples/usr/share/doc/bigloo-examples/examples/bdl
	cp -a $(BUILD_TREE)/api/fthread/examples debian/bigloo-examples/usr/share/doc/bigloo-examples/examples/fthread

#	dh_installdebconf	
	dh_installemacsen
	dh_installinfo
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: DH_OPTIONS=-a $(NO_PACKAGES)
binary-arch: install-arch
	dh_testdir
	dh_testroot
	dh_installchangelogs $(BUILD_TREE)/ChangeLog
	dh_installdocs
	for i in jigloo srfi; do \
	  cp -p $(BUILD_TREE)/$$i/README debian/bigloo/usr/share/doc/bigloo/README.$$i; done
	for i in fthread pthread; do \
	  cp -p $(BUILD_TREE)/api/$$i/README debian/bigloo/usr/share/doc/bigloo/README.api-$$i; done
	dh_buildinfo
	dh_installmenu
#	dh_installdebconf	
	dh_strip
	dh_installinfo
	dh_link usr/share/man/man1/bigloo.1.gz \
		usr/share/man/man1/bigloo${BIGLOO_RELEASE}.1.gz

	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb

	rm -f debian/shlibs.local
	for lib in `cd $(BUILD_TREE)/lib/$(BIGLOO_RELEASE) && echo *.so`; do \
	  echo "$${lib%-${BIGLOO_RELEASE}.so} $(BIGLOO_RELEASE) libbigloo$(BIGLOO_RELEASE) (= $(PKG_VERSION))" \
		>> debian/shlibs.local; \
	done

	dh_shlibdeps -ldebian/libbigloo${BIGLOO_RELEASE}/usr/lib \
		-L libbigloo${BIGLOO_RELEASE}
	echo >>debian/bigloo.substvars "bigloo:Runtime=libbigloo${BIGLOO_RELEASE}"
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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