PACKAGE = nodeunit
NODEJS = $(if $(shell test -f /usr/bin/nodejs && echo "true"),nodejs,node)

PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
DATADIR ?= $(PREFIX)/share
MANDIR ?= $(PREFIX)/share/man
LIBDIR ?= $(PREFIX)/lib
NODEJSLIBDIR ?= $(LIBDIR)/$(NODEJS)

BUILDDIR = dist

DOCS = $(shell find doc -name '*.md' \
				|sed 's|.md|.1|g' \
				|sed 's|doc/|man1/|g' \
				)


$(shell if [ ! -d $(BUILDDIR) ]; then mkdir $(BUILDDIR); fi)

all: build doc

browser:
	# super hacky build script for browser version!
	mkdir -p $(BUILDDIR)/browser
	rm -rf $(BUILDDIR)/browser/*
	# build browser version of nodeunit.js
	cat share/license.js >> $(BUILDDIR)/browser/nodeunit.js
	echo "nodeunit = (function(){" >> $(BUILDDIR)/browser/nodeunit.js
	cat deps/json2.js >> $(BUILDDIR)/browser/nodeunit.js
	# make assert global
	echo "var assert = this.assert = {};" >> $(BUILDDIR)/browser/nodeunit.js
	echo "var types = {};" >> $(BUILDDIR)/browser/nodeunit.js
	echo "var core = {};" >> $(BUILDDIR)/browser/nodeunit.js
	echo "var nodeunit = {};" >> $(BUILDDIR)/browser/nodeunit.js
	echo "var reporter = {};" >> $(BUILDDIR)/browser/nodeunit.js
	cat deps/async.js >> $(BUILDDIR)/browser/nodeunit.js
	echo "(function(exports){" >> $(BUILDDIR)/browser/nodeunit.js
	cat lib/assert.js >> $(BUILDDIR)/browser/nodeunit.js
	echo "})(assert);" >> $(BUILDDIR)/browser/nodeunit.js
	echo "(function(exports){" >> $(BUILDDIR)/browser/nodeunit.js
	cat lib/types.js >> $(BUILDDIR)/browser/nodeunit.js
	echo "})(types);" >> $(BUILDDIR)/browser/nodeunit.js
	echo "(function(exports){" >> $(BUILDDIR)/browser/nodeunit.js
	cat lib/core.js >> $(BUILDDIR)/browser/nodeunit.js
	echo "})(core);" >> $(BUILDDIR)/browser/nodeunit.js
	echo "(function(exports){" >> $(BUILDDIR)/browser/nodeunit.js
	cat lib/reporters/browser.js >> $(BUILDDIR)/browser/nodeunit.js
	echo "})(reporter);" >> $(BUILDDIR)/browser/nodeunit.js
	echo "nodeunit = core;" >> $(BUILDDIR)/browser/nodeunit.js
	echo "nodeunit.assert = assert;" >> $(BUILDDIR)/browser/nodeunit.js
	echo "nodeunit.reporter = reporter;" >> $(BUILDDIR)/browser/nodeunit.js
	echo "nodeunit.run = reporter.run;" >> $(BUILDDIR)/browser/nodeunit.js
	echo "return nodeunit; })();" >> $(BUILDDIR)/browser/nodeunit.js
	cp $(BUILDDIR)/browser/nodeunit.js $(BUILDDIR)/browser/.nodeunit.js
	sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.nodeunit.js > $(BUILDDIR)/browser/nodeunit.js
	rm $(BUILDDIR)/browser/.nodeunit.js
	# copy nodeunit.css
	cp share/nodeunit.css $(BUILDDIR)/browser/nodeunit.css
	# create nodeunit.min.js
	node_modules/uglify-js/bin/uglifyjs $(BUILDDIR)/browser/nodeunit.js > $(BUILDDIR)/browser/nodeunit.min.js
	# create test scripts
	mkdir -p $(BUILDDIR)/browser/test
	cp test/test.html $(BUILDDIR)/browser/test/test.html
	# test-base.js
	echo "(function (exports) {" > $(BUILDDIR)/browser/test/test-base.js
	cat test/test-base.js >> $(BUILDDIR)/browser/test/test-base.js
	echo "})(this.test_base = {});" >> $(BUILDDIR)/browser/test/test-base.js
	cp $(BUILDDIR)/browser/test/test-base.js $(BUILDDIR)/browser/.test-base.js
	sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.test-base.js > $(BUILDDIR)/browser/test/test-base.js
	rm $(BUILDDIR)/browser/.test-base.js
	# test-runmodule.js
	echo "(function (exports) {" > $(BUILDDIR)/browser/test/test-runmodule.js
	cat test/test-runmodule.js >> $(BUILDDIR)/browser/test/test-runmodule.js
	echo "})(this.test_runmodule = {});" >> $(BUILDDIR)/browser/test/test-runmodule.js
	cp $(BUILDDIR)/browser/test/test-runmodule.js $(BUILDDIR)/browser/.test-runmodule.js
	sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.test-runmodule.js > $(BUILDDIR)/browser/test/test-runmodule.js
	rm $(BUILDDIR)/browser/.test-runmodule.js
	# test-runtest.js
	echo "(function (exports) {" > $(BUILDDIR)/browser/test/test-runtest.js
	cat test/test-runtest.js >> $(BUILDDIR)/browser/test/test-runtest.js
	echo "})(this.test_runtest = {});" >> $(BUILDDIR)/browser/test/test-runtest.js
	cp $(BUILDDIR)/browser/test/test-runtest.js $(BUILDDIR)/browser/.test-runtest.js
	sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.test-runtest.js > $(BUILDDIR)/browser/test/test-runtest.js
	rm $(BUILDDIR)/browser/.test-runtest.js
	# test-testcase.js
	echo "(function (exports) {" > $(BUILDDIR)/browser/test/test-testcase.js
	cat test/test-testcase.js >> $(BUILDDIR)/browser/test/test-testcase.js
	echo "})(this.test_testcase = {});" >> $(BUILDDIR)/browser/test/test-testcase.js
	cp $(BUILDDIR)/browser/test/test-testcase.js $(BUILDDIR)/browser/.test-testcase.js
	sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.test-testcase.js > $(BUILDDIR)/browser/test/test-testcase.js
	rm $(BUILDDIR)/browser/.test-testcase.js
	# test-testcase-legacy.js
	echo "(function (exports) {" > $(BUILDDIR)/browser/test/test-testcase-legacy.js
	cat test/test-testcase-legacy.js >> $(BUILDDIR)/browser/test/test-testcase-legacy.js
	echo "})(this.test_testcase_legacy = {});" >> $(BUILDDIR)/browser/test/test-testcase-legacy.js
	cp $(BUILDDIR)/browser/test/test-testcase-legacy.js $(BUILDDIR)/browser/.test-testcase-legacy.js
	sed "/\@REMOVE_LINE_FOR_BROWSER/d" <$(BUILDDIR)/browser/.test-testcase-legacy.js > $(BUILDDIR)/browser/test/test-testcase-legacy.js
	rm $(BUILDDIR)/browser/.test-testcase-legacy.js
	# copy nodeunit.js to dist/browser/test to make it easier for me to host and
	# run on windows VMs with IE
	cp $(BUILDDIR)/browser/nodeunit.js $(BUILDDIR)/browser/test/nodeunit.js
	cp $(BUILDDIR)/browser/nodeunit.css $(BUILDDIR)/browser/test/nodeunit.css

commonjs:
	# super hacky build script for browser commonjs version!
	##### make commonjs browser module ######
	mkdir -p $(BUILDDIR)/commonjs
	rm -rf $(BUILDDIR)/commonjs/*
	mkdir -p $(BUILDDIR)/commonjs/deps
	cp deps/json2.js $(BUILDDIR)/commonjs/deps
	cp deps/async.js $(BUILDDIR)/commonjs/deps
	echo "var async = require('async');" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "var assert = {};" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "var types = {};" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "var core = {};" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "var nodeunit = {};" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "var reporter = {};" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "(function(exports){" >> $(BUILDDIR)/commonjs/nodeunit.js
	cat lib/assert.js >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "})(assert);" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "(function(exports){" >> $(BUILDDIR)/commonjs/nodeunit.js
	cat lib/types.js >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "})(types);" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "(function(exports){" >> $(BUILDDIR)/commonjs/nodeunit.js
	cat lib/core.js >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "})(core);" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "module.exports = core;" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "(function(exports, nodeunit){" >> $(BUILDDIR)/commonjs/nodeunit.js
	cat lib/reporters/browser.js >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "})(reporter, module.exports);" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "module.exports.assert = assert;" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "module.exports.reporter = reporter;" >> $(BUILDDIR)/commonjs/nodeunit.js
	echo "module.exports.run = reporter.run;" >> $(BUILDDIR)/commonjs/nodeunit.js
	sed -i "/\@REMOVE_LINE_FOR_BROWSER/d" $(BUILDDIR)/commonjs/nodeunit.js
	sed -i "/\@REMOVE_LINE_FOR_COMMONJS/d" $(BUILDDIR)/commonjs/nodeunit.js
	##### end of commonjs browser module #####

build: stamp-build

stamp-build: $(wildcard  deps/* lib/*.js)
	touch $@;
	mkdir -p $(BUILDDIR)/nodeunit
	cp -R bin node_modules deps index.js lib package.json share $(BUILDDIR)/nodeunit
	printf '#!/bin/sh\n$(NODEJS) $(NODEJSLIBDIR)/$(PACKAGE)/bin/nodeunit $$@' > $(BUILDDIR)/nodeunit.sh

test:
	$(NODEJS) ./bin/nodeunit test

install: build
	install -d $(NODEJSLIBDIR)
	cp -a $(BUILDDIR)/nodeunit $(NODEJSLIBDIR)
	install -m 0755 $(BUILDDIR)/nodeunit.sh $(BINDIR)/nodeunit
	install -d $(MANDIR)/man1/
	cp -a man1/nodeunit.1 $(MANDIR)/man1/

uninstall:
	rm -rf $(NODEJSLIBDIR)/nodeunit $(NODEJSLIBDIR)/nodeunit.js $(BINDIR)/nodeunit
	rm -rf $(MANDIR)/man1/nodeunit.1

clean:
	rm -rf $(BUILDDIR) stamp-build

lint:
	nodelint --config nodelint.cfg ./index.js ./bin/nodeunit ./bin/nodeunit.json ./lib/*.js ./lib/reporters/*.js ./test/*.js

doc: man1 $(DOCS)
	@true

man1:
	@if ! test -d man1 ; then mkdir -p man1 ; fi

# use `npm install ronn` for this to work.
man1/%.1: doc/%.md
	ronn --roff $< > $@

.PHONY: browser test install uninstall build all
