MODS = $(wildcard src/*.erl)
BEAMS = $(MODS:src/%.erl=build/ebin/%.beam)
SCSS = $(wildcard www/css/*.scss)
CSS_FILES = $(SCSS:www/css/%.scss=build/www/css/%.css)
TEST_MODS = $(wildcard test/*.erl)
TEST_BEAMS = $(TEST_MODS:test/%.erl=build/test/%.beam)
TS_ROOT=/usr/local/var/yaws/timestamper.jdb-labs.com
TS_ROOT_DEV=/home/jdbernard/temp/timestamper.jdb-labs.com
BUILD_SERVER=dev.jdb-labs.com
BUILD_SOURCE=/~jdbernard/projects/timestamper/web-app
CWD = `pwd`

default: build

all : compile test

compile : init $(BEAMS) $(CSS_FILES)

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 build/ebin --pa build/test --id test_inst

run-test : compile compile-test config-yaws-dev
	@erl -pa ./build/ebin -pa ./build/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 build*

init:
	-mkdir -p build/ebin
	-mkdir -p build/www/css
	-mkdir -p build/www/js
	-mkdir -p build/www/img

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

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

build/www/css/%.css : www/css/%.scss
	scss $< $@

build: compile
	-mkdir -p build/include
	cp -r www/js build/www/
	cp -r www/img build/www/
	cp -r www/*.* build/www/
	cp lib/* build/ebin
	cp src/ts_db_records.hrl build/include
	cp yaws.prod.conf build/yaws.conf
	tar czf timestamper-web.build.tar.gz build

deploy: build
	@service yaws stop
	@echo Removing existing artifacts.
	- @rm -r "$(TS_ROOT)"
	@echo Copying current artifacts.
	@cp -r build "$(TS_ROOT)"
	@service yaws start
	@echo Done.

deploy-dev: build
	@echo Removing existing artifacts.
	- rm -r $(TS_ROOT_DEV)
	@echo Copying current artifacts.
	cp -r build $(TS_ROOT_DEV)
	@echo Altering configuration for DEV.
	sed -i 's@$(TS_ROOT)@$(TS_ROOT_DEV)@' $(TS_ROOT_DEV)/yaws.conf
#	mv "$(TS_ROOT_DEV)/www" "$(TS_ROOT_DEV)/timestamper"
#	mkdir "$(TS_ROOT_DEV)/www"
#	mv "$(TS_ROOT_DEV)/timestamper" "$(TS_ROOT_DEV)/www/timestamper"
	@echo Done.


