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

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

.PHONY: clean
clean : 
	rm -fr src/main/java/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)/META-INF/*.xml \
			  $(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 config/*.properties config/cutout.properties target/webapp/WEB-INF/classes
	cp config/context.xml       target/webapp/META-INF
	cp config/web.xml           target/webapp/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)'







