INSTALL = install
DESTDIR = 
INSTALL_PROGRAM = $(INSTALL)
INSTALL_DATA = $(INSTALL) -m 644
prefix = /usr/local
exec_prefix = $(prefix)
bindir = $(exec_prefix)/bin
datarootdir = $(prefix)/share
datadir = $(datarootdir)

AUDIENCE_SRC := $(shell find audience -name *.hx)
PROMPTER_SRC := $(shell find prompter -name *.hx)
SHARED_SRC := $(shell find s -name *.hx)
STAGE_SRC := $(shell find stage -name *.hx)

.PHONY: all

dev: \
	dist/audience.js \
	dist/prompter.js \
	dist/stage.hl

all: \
	dev \
	bin/stage

dist/audience.js: $(AUDIENCE_SRC) $(SHARED_SRC)
dist/prompter.js: $(PROMPTER_SRC) $(SHARED_SRC)
dist/stage.hl bin/stage.c: $(STAGE_SRC) $(SHARED_SRC)

dist/%.js:
	haxe $(HAXEFLAGS) --js $@ --define js-es=6 --class-path s --class-path $* --main Main

dist/%.hl:
	haxe $(HAXEFLAGS) --hl $@ --class-path s --class-path $* --main Main

bin/%: bin/%.c
	$(LINK.c) -Ibin $^ $(LOADLIBES) $(LDLIBS) -l:fmt.hdll -l:uv.hdll -luv -lhl -o $@

bin/%.c:
	haxe $(HAXEFLAGS) --hl $@ --class-path s --class-path $* --main Main

.PHONY: watch

watch:
	while true; do \
		$(MAKE); \
		inotifywait --recursive --exclude '.*.swp' --event modify s audience stage prompter Makefile; \
	done

.PHONY: install

install:
	$(INSTALL) -d "$(DESTDIR)$(bindir)"
	$(INSTALL_PROGRAM) bin/stage "$(DESTDIR)$(bindir)/teatre-ws"
	$(INSTALL) -d "$(DESTDIR)$(datadir)/teatre-ws"
	$(INSTALL_DATA) dist/*.svg "$(DESTDIR)$(datadir)/teatre-ws"
	$(INSTALL) -d "$(DESTDIR)$(datadir)/teatre-www"
	$(INSTALL_DATA) dist/audience.html dist/audience.js "$(DESTDIR)$(datadir)/teatre-www"
	$(INSTALL_DATA) dist/prompter.html dist/prompter.js "$(DESTDIR)$(datadir)/teatre-www"
