#!/usr/bin/make -f

include /usr/share/dpatch/dpatch.make

export PLARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

PKG=""

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
  CONFFLAGS += --build $(DEB_HOST_GNU_TYPE) \
               --enable-shared \
               --without-clib \
               --disable-gmp --disable-readline --disable-curses \
               --without-chr --without-clpqr --without-cpp --without-db \
               --without-mp --without-nlp --without-ssl --without-odbc \
               --without-pldoc --without-plunit --without-zlib \
               --without-sgml --without-semweb --without-table \
               --without-http --without-xpce
else
  CONFFLAGS += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE) \
               --enable-shared \
               --without-clib \
               --disable-gmp --disable-readline --disable-curses \
               --without-chr --without-clpqr --without-cpp --without-db \
               --without-mp --without-nlp --without-ssl --without-odbc \
               --without-pldoc --without-plunit --without-zlib \
               --without-sgml --without-semweb --without-table \
               --without-http --without-xpce
endif

CFLAGS=
# Fix FTBFS on ARM
ifeq ($(PLARCH),arm)
	CFLAGS += -O0
endif

build: patch build-stamp
build-stamp:
	dh_testdir

#       COFLAGS="-Os -fno-strict-aliasing"
	ARCH=$(PLARCH) PKG="$(PKG)" CFLAGS="$(CFLAGS)" PL="swipl" \
            ./configure $(CONFFLAGS) --with-world \
		--prefix=/usr '--mandir=$${prefix}/share/man'

	$(MAKE) ARCH=$(PLARCH) PLARCH=$(PLARCH)

	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp

        # Upstream's clean targets are too clean
	for f in packages/xpce/src/h/names.i[hc]; do \
	    [ ! -f $$f ] || mv $$f debian; \
	done

        # Upstream's clean targets are not clean enough
	rm -f packages/xpce/man/reference/index.obj
	rm -f packages/xpce/lib/amd64
	rm -f .pkg-config

	[ ! -f src/Makefile ] || \
	    $(MAKE) ARCH=$(PLARCH) PLARCH=$(PLARCH) clean distclean

	for f in debian/names.i[hc]; do \
	    [ ! -f $$f ] || mv $$f packages/xpce/src/h; \
	done
	dh_clean

	for DIR in src/ packages/jpl; do \
		cp -f /usr/share/misc/config.sub $$DIR/config.sub; \
		cp -f /usr/share/misc/config.guess $$DIR/config.guess; \
	done

install:
install: build
	dh_testdir
	dh_testroot
	dh_clean -k

        # Perform a minimal installation, then separate eveything extra into
        # subpackages. This whole thing is a huge hack... yuck.

	$(MAKE) prefix=`pwd`/debian/swi-prolog-tmp/usr \
		ARCH=$(PLARCH) install-lite

       # Relocate Manual to /usr/share to avoid lintian warnings.
	MANUAL=`find debian/swi-prolog-tmp/usr/lib -type d -name Manual` && \
        [ -n "$$MANUAL" ] && \
        RELOCATED=`echo $$MANUAL | sed 's#/usr/lib/#/usr/share/#'` && \
        mkdir -p $$RELOCATED && cp -rvp $$MANUAL $$RELOCATED && \
        rm -fr $$MANUAL
	rm -f debian/*.install

        # Generate file list for the main package.
	cd debian/swi-prolog-tmp && find . -type f | \
            grep -v '/library/' | grep -v '/boot/' | \
            grep -v '/boot32.prc$$' | \
            grep -v '/Manual/' | grep -v '/man/' | grep -v '/MANUAL$$' | \
            grep -v '/include/' | grep -v '/libpl.a$$' | \
            grep -v '/demo/' | grep -v '/dotfiles/' | \
            grep -v '/plld$$' | grep -v '/plrc$$' \
            > ../swi-prolog.install

	cd debian/swi-prolog-tmp && find . -type l | \
            grep -v '/library/' | grep -v '/boot/' | grep -v 'boot32.prc' | \
            grep -v '/Manual/' | grep -v '/man/' | grep -v '/MANUAL$$' | \
            grep -v '/include/' | grep -v '/libpl.a$$' | \
            grep -v '/demo/' | grep -v '/dotfiles/' | \
            grep -v '/plld$$' | grep -v '/plrc$$' \
            >> ../swi-prolog.install

        # Generate file list for the lib-core subpackage.
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/library/shlib.pl$$' > ../swi-prolog-lib-core.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/library/error.pl$$' >> ../swi-prolog-lib-core.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/library/lists.pl$$' >>../swi-prolog-lib-core.install || :
        # Generate library index with only our minimal core libs.
	switop=`pwd`/debian/swi-prolog-tmp/usr/lib/swipl-5.6.50 && \
	  cd $$switop/library && \
	  rm -f INDEX.pl INDEX-core.pl && \
	  LD_LIBRARY_PATH=$$switop/lib/$(PLARCH) \
	  $$switop/bin/$(PLARCH)/swipl --home=$$switop -g true -t \
              "make_library_index('.', ['shlib.pl','error.pl','lists.pl'])" && \
	    mv INDEX.pl INDEX-core.pl
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/library/INDEX-core.pl$$' >>../swi-prolog-lib-core.install || :
        # Generate file list for the lib subpackage.
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/library/' | \
            grep -v '/MANUAL$$' | \
	    grep -v 'INDEX.pl$$' | \
	    grep -v 'INDEX-core.pl$$' | \
	    grep -v 'shlib.pl$$' | \
	    grep -v 'error.pl$$' | \
	    grep -v 'lists.pl$$' > ../swi-prolog-lib.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/boot/' >> ../swi-prolog-lib.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep 'boot32.prc$$' >> ../swi-prolog-lib.install || :
        # Generate library index excluding what's in lib-core already.
	switop=`pwd`/debian/swi-prolog-tmp/usr/lib/swipl-5.6.50 && \
	  cd $$switop/library && \
	  rm -f INDEX.pl INDEX-lib.pl && \
	  LD_LIBRARY_PATH=$$switop/lib/$(PLARCH) \
	  $$switop/bin/$(PLARCH)/swipl --home=$$switop -g true -t \
              "make_library_index('.')" && \
	    cat INDEX.pl | \
                grep -v 'shlib, shlib' | \
                grep -v 'error, error' | \
                grep -v 'lists, lists' > INDEX-lib.pl
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/library/INDEX-lib.pl$$' >>../swi-prolog-lib.install || :

        # Generate file list for the dev subpackage.
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/include/' > ../swi-prolog-dev.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/libpl.a$$' >> ../swi-prolog-dev.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/demo/' | grep '/dotfiles/' >> ../swi-prolog-dev.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/dotfiles/' >> ../swi-prolog-dev.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/plld$$' >> ../swi-prolog-dev.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/plrc$$' >> ../swi-prolog-dev.install || :

	cd debian/swi-prolog-tmp && find . -type l | \
	    grep '/include/' >> ../swi-prolog-dev.install || :
	cd debian/swi-prolog-tmp && find . -type l | \
	    grep '/libpl.a$$' >> ../swi-prolog-dev.install || :
	cd debian/swi-prolog-tmp && find . -type l | \
	    grep '/demo/' | grep '/dotfiles/' >> ../swi-prolog-dev.install || :
	cd debian/swi-prolog-tmp && find . -type l | \
	    grep '/dotfiles/' >> ../swi-prolog-dev.install || :
	cd debian/swi-prolog-tmp && find . -type l | \
	    grep '/plld$$' >> ../swi-prolog-dev.install || :
	cd debian/swi-prolog-tmp && find . -type l | \
	    grep '/plrc$$' >> ../swi-prolog-dev.install || :

        # Generate file list for the doc subpackage.
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/Manual/' > ../swi-prolog-doc.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/MANUAL$$' >> ../swi-prolog-doc.install || :
	cd debian/swi-prolog-tmp && find . -type f | \
	    grep '/man/' >> ../swi-prolog-doc.install || :

	cd debian/swi-prolog-tmp && find . -type l | \
	    grep '/Manual/' >> ../swi-prolog-doc.install || :
	cd debian/swi-prolog-tmp && find . -type l | \
	    grep '/MANUAL$$' >> ../swi-prolog-doc.install || :
	cd debian/swi-prolog-tmp && find . -type l | \
	    grep '/man/' >> ../swi-prolog-doc.install || :

        # Seperate to subpackages.
	dh_install -p swi-prolog --sourcedir=debian/swi-prolog-tmp
	dh_install -p swi-prolog-lib-core --sourcedir=debian/swi-prolog-tmp
	dh_install -p swi-prolog-lib --sourcedir=debian/swi-prolog-tmp
	dh_install -p swi-prolog-dev --sourcedir=debian/swi-prolog-tmp
	dh_install -p swi-prolog-doc --sourcedir=debian/swi-prolog-tmp

       # Install lintian-overrides.
	for PKG in swi-prolog; do \
		mkdir -p debian/$$PKG/usr/share/lintian/overrides; \
		cp debian/$$PKG.lintian.override debian/$$PKG/usr/share/lintian/overrides/$$PKG; \
	done

        # Remove empty directories
	find debian/ -depth -type d -empty -exec rm -rfv {} \;

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installman
	dh_strip --dbg-package=swi-prolog-dbg
	dh_link
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

binary-%: build install
	make -f debian/rules binary-common DH_OPTIONS=-p$*

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary-common binary install patch unpatch

