MODS = $(wildcard src/*.erl)
BEAMS = $(MODS:src/%.erl=ebin/%.beam)
TEST_MODS = $(wildcard test/*.erl)
TEST_BEAMS = $(TEST_MODS:test/%.erl=test/%.beam)
TS_ROOT=/usr/lib/yaws/jdb-labs/timestamper
CWD = `pwd`

default: compile

all : compile test

compile : init $(BEAMS)

compile-test : init $(TEST_BEAMS)

test : start-test-server run-test stop-test-server

test-shell : compile compile-test config-yaws-dev
	@echo Starting an interactive YAWS shell with test paths loaded.
	@yaws -i --pa test --id test_inst

run-test : compile compile-test config-yaws-dev
	@erl -pa ./ebin -pa ./test -run timestamper_api_tests test -run init stop -noshell

start-test-server :
	@yaws -D --id test_inst

stop-test-server :
	@yaws --stop --id test_inst

clean:
	rm -rf ebin/* erl_crash.dump test/*.beam

init:
	-mkdir ebin

ebin/%.beam : src/%.erl
	erlc -W -o ebin $<

test/%.beam : test/%.erl
	@echo Compiling sources...
	erlc -W -o test $<

deploy: compile
	@service yaws stop
	@echo Removing existing artifacts.
	- @rm -r $(TS_ROOT)
	@echo Copying current artifacts.
	@mkdir -p $(TS_ROOT)
	@cp -r www $(TS_ROOT)
	@cp -r ebin $(TS_ROOT)
	@mkdir $(TS_ROOT)/include
	@cp -r src/ts_db_records.hrl $(TS_ROOT)/include/.
	@cp yaws.prod.conf $(TS_ROOT)/yaws.conf
	@service yaws start
	@echo Done.