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

config.status:
	dh_testdir
	PREFIX="/usr" ./configure --debug --system
	touch $@

build: build-stamp

build-stamp: config.status
	dh_testdir
	$(MAKE)

clean:
	rm -f node
	rm -f node_g
	rm -f doc/nodejs.1
	rm -f .lock-wscript
	dh_testdir
	dh_testroot
	rm -f build-stamp
	$(MAKE) clean || true
	# first make fails, and creates *.pyc files
	find . -name "*.pyc" -delete
	rm -rf build
	rm -f config.status
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep  
	dh_installdirs
	# Rename man page to NODEJS
	# rename binaries to nodejs, nodejs-waf, nodejs-repl
	# escape single quotes at the start of lines, and false macros starting with a dot
	sed \
		-e 's/"NODE"/"NODEJS"/g' \
		-e "s/^'/\\\'/g" \
		-e "s/^\.\.\./ \\\.\\\.\\\./g" \
		-e "s/^\.\./ \\\.\\\./g" \
		-e "s/^\.node/\\\.node/g" \
		-e "s/\\\'node\\\'/\\\'nodejs\\\'/g" \
		-e 's/node\\-waf/nodejs\\-waf/g' \
		-e 's/node\\-repl/nodejs\\-repl/g' \
		-e 's/> node/> nodejs/g' \
		-e 's/node \\- evented/nodejs \\- evented/g' \
		doc/node.1 > doc/nodejs.1
	
	# Add here commands to install the package
	DESTDIR=$(CURDIR)/debian/nodejs $(MAKE) install
	# rename and install binaries in nodejs, nodejs-dbg
	mv $(CURDIR)/debian/nodejs/usr/bin/node $(CURDIR)/debian/nodejs/usr/bin/nodejs
	mv $(CURDIR)/debian/nodejs/usr/bin/node_g $(CURDIR)/debian/nodejs-dbg/usr/lib/debug/usr/bin/nodejs
	# headers for nodejs-dev
	mv $(CURDIR)/debian/nodejs/usr/include/node/* $(CURDIR)/debian/nodejs-dev/usr/include/nodejs/
	rm -rf $(CURDIR)/debian/nodejs/usr/include
	# waf for nodejs-dev
	mv $(CURDIR)/debian/nodejs/usr/bin/node-waf $(CURDIR)/debian/nodejs-dev/usr/bin/nodejs-waf
	mv $(CURDIR)/debian/nodejs/usr/share/nodejs/wafadmin $(CURDIR)/debian/nodejs-dev/usr/share/nodejs/
	# nodejs-repl (rlwrap version) for nodejs
	mv $(CURDIR)/debian/nodejs/usr/bin/node-repl $(CURDIR)/debian/nodejs/usr/share/nodejs/
	cp $(CURDIR)/debian/nodejs-repl $(CURDIR)/debian/nodejs/usr/bin/
	chmod 755 $(CURDIR)/debian/nodejs/usr/bin/nodejs-repl

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_installman
	dh_link
	dh_strip --dbg-package=nodejs-dbg
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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