#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# 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
EXT_IMID="{e4a8a97b-f2ed-450b-b12d-ee082ba24781}"

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 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
optimize_add += -march=armv5t
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

config.status: configure
	dh_testdir
	# Add here commands to configure the package.
	#CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr $(configopts)

configure: configure-stamp
configure-stamp:
	dh_testdir
	
	# Add here commands to configure the package.
	#./autogen.sh --prefix=/usr
 
	touch configure-stamp

build: build-stamp
build-stamp:  config.status
	dh_testdir
	# Add here commands to compile the package.
	#$(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.
	#-$(MAKE) clean
	#-$(MAKE) distclean
	rm -f src/Makefile.in
	rm -f aclocal.m4 compile config.guess config.h.in config.sub configure depcomp install-sh ltmain.sh missing Makefile.in
	rm -rf extensions
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	# Add here commands to install the package into debian/tmp
	#$(MAKE) $(NJOBS) install prefix=$(CURDIR)/debian/tmp/usr
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/browser/extensions
	rm -rf extensions
	mkdir extensions
	cp -rf $(CURDIR)/develop/* $(CURDIR)/extensions/
	cp -rf $(CURDIR)/extensions/* $(CURDIR)/debian/tmp/usr/lib/browser/extensions/
	cd $(CURDIR)/extensions/$(EXT_IMID)/ && zip -r ../../greasemonkey-webaddon_0.8.20090920.2-1maemo1.xpi ./*
	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=greasemonkey-webaddon
	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
