#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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


CFLAGS = -Wall -g

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


configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	tar xvf node-v0.8.11.tar.gz

	#credits: http://krispypen.github.com/blog/2012/10/07/compile-nodejs-for-your-n900/
	patch -p0 < debian/patches/maemo5_armv5.diff

	#with old python (2.5)
	patch -p0 < debian/patches/maemo5_python25.diff

	#use python2.5 with every python file
	cd node-v0.8.11 ; find . -type f -print0 | xargs -0 sed -i 's/\/usr\/bin\/env python/\/usr\/bin\/env python2.5/g'

	#use python2.5	
	sed -i 's/python/python2.5/g' node-v0.8.11/Makefile
	sed -i 's/python/python2.5/g' node-v0.8.11/node.gyp
	sed -i 's/python/python2.5/g' node-v0.8.11/tools/build-changelog.sh
	sed -i 's/python/python2.5/g' node-v0.8.11/tools/test.py
	sed -i 's/python/python2.5/g' node-v0.8.11/deps/v8/DEPS
	sed -i 's/python/python2.5/g' node-v0.8.11/deps/v8/Makefile
	sed -i 's/python/python2.5/g' node-v0.8.11/test/pummel/test-child-process-spawn-loop.js
	sed -i 's/python/python2.5/g' node-v0.8.11/test/pummel/test-exec.js
	sed -i 's/python/python2.5/g' node-v0.8.11/test/simple/test-child-process-set-blocking.js
	sed -i 's/python/python2.5/g' node-v0.8.11/deps/v8/src/d8.gyp
	sed -i 's/python/python2.5/g' node-v0.8.11/deps/v8/src/macros.py
	sed -i 's/python/python2.5/g' node-v0.8.11/deps/v8/tools/fuzz-harness.sh
	sed -i 's/python/python2.5/g' node-v0.8.11/deps/v8/tools/presubmit.py
	sed -i 's/python/python2.5/g' node-v0.8.11/deps/v8/tools/test.py
	sed -i 's/python/python2.5/g' node-v0.8.11/deps/v8/tools/test-wrapper-gypbuild.py
	sed -i 's/python/python2.5/g' node-v0.8.11/deps/v8/tools/gyp/v8.gyp
	#sed -i 's/python\\/python2.5\\/g' node-v0.8.11/tools/js2c.py

	#same file (different version)
	sed -i 's/raise/None #raise/g' node-v0.8.11/tools/js2c.py
	sed -i 's/raise ("I/None #raise ("I/g' node-v0.8.11/deps/v8/tools/js2c.py

	
	#remove bat-files (just in case)
	find . -type f -name "*.bat" -exec rm {} \;

	#LINK is needed, why?
	cd node-v0.8.11 ; export LINK=g++ ; ./configure --prefix=/opt/node --without-snapshot

	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp 
	dh_testdir

	# Add here commands to compile the package.
	cd node-v0.8.11 ; export LINK=g++ ; make
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	rm -rf node-v0.8.11
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	cd node-v0.8.11 ; export LINK=g++ ; make DESTDIR=$(CURDIR)/debian/nodejs install

# 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_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_install
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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