#!/usr/bin/make -f
#-*- makefile -*-

#export DH_VERBOSE=1

DESTDIR=$(CURDIR)/debian/tmp

DDEBUG=`if (echo $(DEB_BUILD_OPTIONS) | grep -q debug) then \
		echo "--enable-debug"; \
	else \
		echo ""; \
	fi`

export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE) --with-tcl=/usr/lib/tcl8.5
  export CROSS_BUILDING=no
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE) --with-tcl=/usr/$(DEB_HOST_GNU_TYPE)/lib/tcl8.5 LDFLAGS=-L/usr/$(DEB_HOST_GNU_TYPE)/lib 
  export CROSS_BUILDING=yes
endif


#export DEB_CFLAGS_MAINT_APPEND = -O2 -fno-strict-aliasing
export CFLAGS += -O2 -fno-strict-aliasing \
	-DSQLITE_SECURE_DELETE -DSQLITE_ENABLE_COLUMN_METADATA \
	-DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE=1 -DSQLITE_SOUNDEX=1 \
	-DSQLITE_ENABLE_UNLOCK_NOTIFY \
	-DSQLITE_OMIT_LOOKASIDE=1

configure: configure-stamp
configure-stamp:
	dh_testdir
	autoconf
	./configure --prefix=/usr --mandir="/usr/share/man" \
	  $(confflags) --enable-threadsafe \
	  --enable-load-extension \
	  --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
	  --libexecdir=\$${libdir}/sqlite3 \
	  TCLLIBDIR=/usr/lib/tcltk/sqlite3 \
	  $(DDEBUG)

	touch $@

build-arch: build
build-indep: build

build: build-stamp
build-stamp: configure
	dh_testdir
	$(MAKE)
ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	$(MAKE) lemon
endif

	touch $@

clean:
	dh_testdir
	dh_testroot
	echo $(DESTDIR)
	rm -f configure-stamp build-stamp
	rm -f config.log config.h pkgIndex.tcl configure
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.h
	dh_clean

install: build
	dh_testdir
	dh_testroot

	$(MAKE) install DESTDIR=$(DESTDIR)
	chrpath -d $(DESTDIR)/usr/bin/sqlite3
	chrpath -d $(DESTDIR)/usr/lib/tcltk/sqlite3/libtclsqlite3.so
	install -m 0664 libtclsqlite3.la $(DESTDIR)/usr/lib/tcltk/sqlite3/
	install -d $(DESTDIR)/usr/share/lemon
	install -m 0664 tool/lempar.c $(DESTDIR)/usr/share/lemon/
	install -m 0775 lemon $(DESTDIR)/usr/bin

	# Remove *.la files per policy 3.9.1.0
#	find $(DESTDIR) -name \*.la | xargs rm -f
	sed -i "/dependency_libs/ s/'.*'/''/" `find $(DESTDIR) -name '*.la'`

	# Create the pkgIndex.tcl file for the Tcl module. This generated file
	# actually turns out to be relocatable.
	echo "pkg_mkIndex -verbose $(DESTDIR)/usr/lib/tcltk/sqlite3" | tclsh8.5

binary-indep: build install
	dh_testdir
	dh_testroot

	dh_install -i --sourcedir=$(DESTDIR)
	dh_installdocs -i
	dh_installchangelogs -i www/changes.html
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir
	dh_testroot

	dh_install -a --sourcedir=$(DESTDIR)
	dh_installman -a
	dh_installdocs -a
	dh_installchangelogs -a www/changes.html
	dh_strip -a --dbg-package=libsqlite3-0-dbg
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a -Ldebian/libsqlite3-0/usr/lib/$(DEB_HOST_MULTIARCH)
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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