#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

PKGVERSION      := $(shell dpkg-parsechangelog | grep ^Version:.* | cut -d ' ' -f 2)
PKGNAME         := $(shell dpkg-parsechangelog | grep ^Source:.* | cut -d ' ' -f 2)

configopts += $(ADD_OPTIONS)
optimize_add =

static:=n
debug:=n
openssl:=y
gnomevfs:=n
logging:=n
gtkmozembed:=n
tests:=n

GCCVER=.gcc$(shell gcc --version | grep gcc | cut -d ' ' -f 3)

ifneq (,$(findstring CodeSourcery,$(shell gcc --version)))
GCCENV:=.cs
endif

ifneq (,$(findstring parallel,$(DEB_BUILD_OPTIONS)))
	PARALLEL_JOBS := $(shell echo $(DEB_BUILD_OPTIONS) | \
	sed -e 's/.*parallel=\([0-9]\+\).*/\1/')
ifeq ($(DEB_BUILD_OPTIONS),$(PARALLEL_JOBS))
	PARALLEL_JOBS := $(shell if [ -f /proc/cpuinfo ]; \
	then echo `cat /proc/cpuinfo | grep '^processor.*:' | wc -l`; \
	else echo 1; fi)
endif
NJOBS:=-j$(PARALLEL_JOBS)
endif

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

ifneq (,$(findstring static,$(DEB_BUILD_OPTIONS)))
static:=y
else
static:=$(static)
endif

ifneq (,$(findstring cflagsopt,$(DEB_BUILD_OPTIONS)))
optimize_add = $(CFLAGS)
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
debug:=y
else
debug:=$(debug)
endif

ifneq (,$(findstring noopenssl,$(DEB_BUILD_OPTIONS)))
openssl:=y
else
openssl:=$(openssl)
endif


ifneq (,$(findstring nognomevfs,$(DEB_BUILD_OPTIONS)))
gnomevfs:=y
else
gnomevfs:=$(gnomevfs)
endif


ifneq (,$(findstring logging,$(DEB_BUILD_OPTIONS)))
logging:=y
else
logging:=$(logging)
endif

ifneq (,$(findstring tests,$(DEB_BUILD_OPTIONS)))
tests:=y
else
tests:=n
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

ifeq ($(static),y)
	configopts += --disable-shared --enable-static
else
	configopts += --disable-static --enable-shared
endif

ifneq (,$(findstring $(DEB_HOST_ARCH),arm armel))
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
#	optimize_add += -mthumb
endif
endif

ifeq ($(logging),y)
	configopts += --enable-logging
else
	configopts += --disable-logging
endif

ifeq ($(debug),y)
	configopts += --disable-strip --enable-debug
	optimize_add += -O1 -g3
ifeq ($(logging),y)
	configopts += --enable-logging
endif
else
	configopts += --enable-strip --disable-debug
	optimize_add += -O2 -g -Wall
endif

ifeq ($(openssl),y)
	configopts += --enable-openssl=yes
else
	configopts += --enable-openssl=no
endif


ifeq ($(gnomevfs),y)
	configopts += --enable-gnomevfs=yes
else
	configopts += --enable-gnomevfs=no
endif

ifeq ($(tests),y)
	configopts += --enable-tests=yes
else
	configopts += --enable-tests=no
endif

ifeq (,$(findstring cflagsopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS = $(optimize_add)
	CXXFLAGS = $(optimize_add)
else
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
	CXXFLAGS += -O0
else
	CFLAGS += -O2 
	CXXFLAGS += -O2
endif
endif

CFLAGS += -fshort-wchar
CXXFLAGS += -fshort-wchar
ifneq ($(GCCENV),.cs)
ifneq (,$(findstring $(DEB_HOST_ARCH),arm armel))
	CFLAGS += -fno-short-wchar
	CXXFLAGS += -fno-short-wchar
endif
endif

ifeq ($(GCCENV),.cs)
	CFLAGS += -fno-unit-at-a-time
	CXXFLAGS += -fno-unit-at-a-time
endif

objdebdir:=obj-$(DEB_BUILD_GNU_TYPE)

$(objdebdir)/config.status:
	dh_testdir
	./autogen.sh --prefix=/usr

	for f in `find debian/ -name "[^c]*.in"`; do \
        	sed -e "s/@PACKAGEVERSION@/${PKGVERSION}/g" -e "s/@PACKAGENAME@/${PKGNAME}/g"  $$f > `echo $$f | sed -e "s/\.in//"`; \
        done
	
	mkdir -p $(objdebdir)
	cd $(objdebdir) && CUSTOM_VERSION="$(PKGVERSION)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" ../configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr $(configopts)

build: build-stamp
build-stamp: $(objdebdir)/config.status
	dh_testdir
	# Add here commands to compile the package.
	cd $(objdebdir) && $(MAKE) $(NJOBS)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	rm -f configure-stamp 
	# Add here commands to clean up after the build process.
#	-cd $(objdebdir) && $(MAKE) clean
#	-cd $(objdebdir) && $(MAKE) distclean
	rm -rf $(objdebdir)
	rm -f include/gmozillaenginemarshal.h src/gmozillaenginemarshal.c Makefile.in src/Makefile.in src/gecko/Makefile.in
	rm -f compile aclocal.m4 config.guess config.h.in config.sub configure depcomp install-sh ltmain.sh missing
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	# Add here commands to install the package into debian/tmp
	cd $(objdebdir) && $(MAKE) $(NJOBS) install DESTDIR=$(CURDIR)/debian/tmp
	cp -rf $(objdebdir)/debian/* $(CURDIR)/debian/
	dh_install --sourcedir=debian/tmp

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_link
	dh_strip --dbg-package=microb-eal
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps
#lintiandh_gencontrol  -- -v$(PKGVERSION)$(GCCVER)$(VERSUFFIX) 
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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