#!/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)

configopts += $(ADD_OPTIONS)
optimize_add =

static:=n
debug:=n
openssl:=y
logging:=n
libwidgetutils:=n
src_subdir:=src

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

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

ifneq (,$(findstring sbox,$(shell gcc --version)))
MAEMO:=.maemo
endif

APPVERSION      := $(shell echo $(PKGVERSION) | cut -d '-' -f 1 | cut -d ':' -f 2 )

export QUILT_PATCHES=$(CURDIR)/debian/patches
QUILT_PATCHES:=$(CURDIR)/debian/patches
STAMP_DIR:=$(CURDIR)/stampdir
STAMPDIR_LOG:=$(CURDIR)/stampdir/log
STAMPDIR_PATCH:=$(CURDIR)/stampdir/log/patch
builddir:=$(CURDIR)/libwidgetutils
objdebdir:=obj-$(DEB_BUILD_GNU_TYPE)

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 logging,$(DEB_BUILD_OPTIONS)))
logging:=y
else
logging:=$(logging)
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
else
ifeq ($(MAEMO),.maemo)
	configopts += --enable-maemo
else
	configopts += --disable-maemo
endif
endif

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

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

ifeq ($(openssl),y)
	configopts += --enable-openssl=yes
else
	configopts += --enable-openssl=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

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

ifneq ($(GCCENV),.cs)
endif

unpack: unpack-stamp
unpack-stamp:
	for str in `find -name src -type d | sed '/.hg/d'`; do cd $$str && ln -s ../../srctools/export.map && ln -s ../../srctools/Makefile.am; done
	for str in `find -name src -type d | sed '/.hg/d'`; do ln -s $$str .; done
	touch unpack-stamp

$(objdebdir)/config.status:
	dh_testdir
	#cp -rf $(CURDIR)/src/Makefile.in Makefile.in.1
	./autogen.sh --prefix=/usr
	mkdir -p $(objdebdir)
	cd $(objdebdir) && CUSTOM_VERSION="$(APPVERSION)" CFLAGS="$(CFLAGS) -fPIC -DPIC" CXXFLAGS="$(CXXFLAGS) -fPIC -DPIC" ../configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr $(configopts)
	# Add here commands to configure the package.

build: unpack-stamp patch-stamp 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 -rf obj-*
	rm -f .pc *-stamp patches Makefile.in *.m4 compile config.guess config.h.in config.sub configure depcomp install-sh ltmain.sh missing 
	rm -rf stampdir
	rm -f widgetutils/src/export.map
	rm -f widgetutils/src/Makefile.am
	rm -rf src
	dh_clean 

# patch stuff
stampdir_targets+=patch
patch: patch-stamp
patch-stamp:
	mkdir -p stampdir/patches
	echo 2 > stampdir/patches/.version
	mkdir -p stampdir/log
	rm -f .pc
	  rm -f patches
	ln -s debian/patches
	ln -s stampdir/patches .pc
	if ! [ `which quilt` ]; then \
	    echo "Couldn't find quilt. Please install it or add it to the build-depends for this package."; \
	    exit 1; \
	fi; \
	if quilt next; then \
	  echo -n "Applying patches... "; \
	    if cd $(builddir) && rm -f .pc && rm -f patches && ln -s ../.pc .pc && ln -s ../patches patches \
	       && quilt push -a -v >  $(STAMPDIR_PATCH); then \
	    echo "successful."; \
	  else \
	    cd $(CURDIR) && quilt next; \
	    echo "failed! (check $(STAMP_DIR)/log/patch for details)";cat $(STAMP_DIR)/log/patch; echo ""; \
	    echo -n "try: perl debian/tools/fixreject.pl "; quilt next; \
	    echo " and sh debian/tools/refresh_patches.sh "; \
	    exit 1; \
	  fi; \
	else \
	  echo "No patches to apply"; \
	fi; \
	>$@

unpatch:
	rm -f $(STAMP_DIR)/patch
	@echo -n "Unapplying patches..."; \
	if [ -e $(STAMP_DIR)/patches/applied-patches ]; then \
	  if test -d $(builddir); then \
	  if cd $(builddir) && quilt pop -a -v > $(STAMPDIR_LOG)/unpatch; then \
	    echo "successful."; \
	  else \
	    echo "failed! (check $(STAMP_DIR)/log/unpatch for details)"; \
	    exit 1; \
	  fi; \
	  fi; \
	else \
	  echo "nothing to do."; \
	fi

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 prefix=$(CURDIR)/debian/tmp/usr
#	mkdir -p $(CURDIR)/debian/tmp/usr/lib/browser/plugins/
#	cp -f $(CURDIR)/debian/flash_temp/$(DEB_HOST_ARCH)/*.so $(CURDIR)/debian/tmp/usr/lib/browser/plugins/
	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=libwidgetutils
	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
