#===============================================================================
NAME       := vlkb-search
VERSION    := $(shell git describe)
WEBAPP_WAR := $(NAME)-$(VERSION).war
CONTEXT_ROOT ?= vlkb/datasets/vlkb_search
#===============================================================================
LIB_DIR     ?= ../java-libs/lib
CLASS_DIR   := target/classes
INSTALL_DIR ?= target/webapp
AUTH_DIR    := ../auth
#===============================================================================
# all sources
#IA2CONVFILTER = $(AUTH_DIR)/src/main/java/IA2TokenConvFilter.java
AUTHFILTERS  = $(wildcard $(AUTH_DIR)/src/main/java/*Filter.java) $(AUTH_DIR)/src/main/java/AuthPolicy.java
SRC_DIR  = src/main/java/vlkb/:src/main/java/vlkb/common:src/main/java/vlkb/output:src/main/java/vlkb/search:src/main/java/vlkb/webapi:$(AUTH_DIR)/src/main/java
VOSI     = src/main/java/vlkb/vosi/VlkbServletFile.java
FILTERS  = $(wildcard src/main/java/vlkb/webapi/*Filter.java)
SERVLETS = $(wildcard src/main/java/vlkb/webapi/*Servlet.java)
#===============================================================================
JFLAGS = -g
CLASSPATH = $(LIB_DIR)/*
#===============================================================================

.PHONY: build
build:
	echo "class Version { static String asString = \"$(VERSION)\";}" > src/main/java/vlkb/Version.java
	javac $(JFLAGS) -cp :$(CLASSPATH) -sourcepath $(SRC_DIR) -d $(CLASS_DIR) $(SERVLETS) $(FILTERS) $(AUTHFILTERS) $(VOSI)

.PHONY: clean
clean : 
	rm -fr src/main/java/vlkb/Version.java target


.PHONY: install
install:
	mkdir -p $(INSTALL_DIR)
	cp -r src/main/webapp/* $(INSTALL_DIR)
	cp -r $(CLASS_DIR) $(LIB_DIR) $(INSTALL_DIR)/WEB-INF/
	cp ../java-libs/jjwt-*0.11.2.jar $(INSTALL_DIR)/WEB-INF/lib/
	cp src/main/resources/*.properties $(INSTALL_DIR)/WEB-INF/classes/
	cp $(AUTH_DIR)/resources/*.properties $(INSTALL_DIR)/WEB-INF/classes/

.PHONY: uninstall
uninstall:
	rm -rf $(INSTALL_DIR)


.PHONY: war
war:
	@jar -cf target/$(WEBAPP_WAR) -C $(INSTALL_DIR) index.html \
			  $(INSTALL_DIR)/*.xsl \
			  $(INSTALL_DIR)/*.js \
			  $(INSTALL_DIR)/WEB-INF/*.xml \
			  $(INSTALL_DIR)/WEB-INF/classes/* \
			  $(INSTALL_DIR)/WEB-INF/lib/*.jar

.PHONY:
create-war: clean build install war


# vlkb-devel host local:

.PHONY: vlkb-devel
vlkb-devel: stop uninstall clean build install config war start

.PHONY: config
config:
	cp config/*.ini $(INSTALL_DIR)/WEB-INF/classes
	cp config/*.properties $(INSTALL_DIR)/WEB-INF/classes
	cp config/web.xml $(INSTALL_DIR)/WEB-INF/


.PHONY: start
start:
	curl -T target/$(WEBAPP_WAR) -u admin:IA2lbt09 'http://localhost:8080/manager/text/deploy?path=/$(CONTEXT_ROOT)&update=true'

.PHONY: stop
stop:
	-@curl -u  admin:IA2lbt09 'http://localhost:8080/manager/text/undeploy?path=/$(CONTEXT_ROOT)'

.PHONY: status 
status:
	curl localhost:8080/manager/text/list -u admin:IA2lbt09

.PHONY: reload
reload:
	curl -u  admin:IA2lbt09  'http://localhost:8080/manager/text/reload?path=/$(CONTEXT_ROOT)'

