#!/usr/bin/env bash -c make

SRC=./timestamp.js
TESTS=*.json ./test/*.js
TARGET=./dist
JSDEST=./dist/timestamp.min.js
JSGZIP=./dist/timestamp.min.js.gz

DOCS_DIR=./docs/typedoc
DOC_SRC=./timestamp.d.ts
DOC_HTML=./docs/typedoc/classes/timestamp.html
DOCS_CSS_SRC=./assets/jsdoc.css
DOCS_CSS_DEST=./docs/jsdoc/styles/jsdoc-default.css

all: test $(TARGET) $(JSGZIP) typedoc

clean:
	rm -fr $(JSDEST) $(JSGZIP) $(DOCS_DIR)

$(TARGET):
	mkdir -p $(TARGET)

$(JSDEST): $(SRC)
	./node_modules/.bin/uglifyjs $(SRC) -c -m -o $(JSDEST)

$(JSGZIP): $(JSDEST)
	gzip -9 < $(JSDEST) > $(JSGZIP)
	ls -l $(JSDEST) $(JSGZIP)

test:
	@if [ "x$(BROWSER)" = "x" ]; then make test-node; else make test-browser; fi

test-node: jshint mocha

test-browser:
	./node_modules/.bin/zuul -- $(TESTS)

test-browser-local:
	node -e 'process.exit(process.platform === "darwin" ? 0 : 1)' && sleep 1 && open http://localhost:4000/__zuul &
	./node_modules/.bin/zuul --local 4000 -- $(TESTS)

mocha:
	./node_modules/.bin/mocha -R spec $(TESTS)

jshint:
	./node_modules/.bin/jshint .

typedoc: $(DOC_HTML)

$(DOC_HTML): $(DOC_SRC)
	# TODO: exclude node_modules
	./node_modules/.bin/typedoc --out $(DOCS_DIR) --includeDeclarations --readme /dev/null --mode file timestamp.d.ts
	perl -i -pe 's/<li>Defined in <a.*//;' $(DOC_HTML)

.PHONY: all clean test jshint mocha
