APP=hgen
BIN=build/$(APP)
DC=ldc2
SRC=$(shell find source -name "*.d")
STYLES=$(shell find strings -name "*")
DESTDIR=
PREFIX=usr/local

PHOBOS_LINKING = dynamic
EXTLIB_LINKING = dynamic

RELEASE_OPTS  := $(shell ./mh.sh $(DC) release_options)
DEBUG_OPTS    := $(shell ./mh.sh $(DC) debug_options)
PHOBOS_OPTS   := $(shell ./mh.sh $(DC) phobos_options $(PHOBOS_LINKING))
DPARSE_OPTS   := $(shell ./mh.sh $(DC) dparse_options $(EXTLIB_LINKING))
LIB_OPTS      := $(PHOBOS_OPTS) $(DPARSE_OPTS)
OTHER_OPTS    := $(shell ./mh.sh $(DC) other_options)

BUILD_MODE    ?= debug
ifeq ($(BUILD_MODE),release)
	BUILD_OPTS := $(RELEASE_OPTS) $(OTHER_OPTS) $(LIB_OPTS)
else
	BUILD_OPTS := $(DEBUG_OPTS) $(OTHER_OPTS) $(LIB_OPTS)
endif

OUT = -of
ifeq ($(DC),gdc)
	OUT = -o 
endif

.PHONY: all doc unittests install uninstall clean dub

debug:
	$(MAKE) BUILD_MODE=debug all

release:
	$(MAKE) BUILD_MODE=release all

all: $(BIN)

$(BIN): $(SRC) source/version $(STYLES)
	mkdir -p build/
	@echo MODE=$(BUILD_MODE)
	$(DC) $(SRC) $(BUILD_OPTS) $(OUT)$@

dub:
	dub build --compiler=$(DC) --force -v

doc: $(BIN)
	$(BIN) source/

unittests:
	dub test --compiler=$(DC) -v

install:
	mkdir -p $(DESTDIR)/$(PREFIX)/bin/
	install $(BIN) $(DESTDIR)/$(PREFIX)/bin/

uninstall:
	rm -f $(DESTDIR)/$(PREFIX)/bin/$(APP)

clean:
	rm -rf build/ doc/

