################################################################################
# args
WEBAPP_WAR ?= vlkb-datasetssearch.war
AUTH ?=
VERSION ?= $(shell git describe)
################################################################################
# target
################################################################################
# insternal
JSRC = java/vlkb
ROOT = webapp
AUTH = ../../../auth
#META_DIR  = webapp/META-INF
#CLASS_DIR = webapp/WEB-INF/classes
MVN_REPO = ~/.m2/repository
################################################################################

all: build

libs-m2:
	mkdir -p $(JSRC)/lib
	cp $(MVN_REPO)/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar $(JSRC)/lib
	cp $(MVN_REPO)/com/opencsv/opencsv/5.7.1/opencsv-5.7.1.jar $(JSRC)/lib
	cp $(MVN_REPO)/it/inaf/ia2/auth-lib/2.0.0-SNAPSHOT/auth-lib-2.0.0-SNAPSHOT.jar $(JSRC)/lib
	cp $(MVN_REPO)/it/inaf/ia2/rap-client/1.0-SNAPSHOT/rap-client-1.0-SNAPSHOT.jar $(JSRC)/lib
	cp $(MVN_REPO)/org/postgresql/postgresql/42.6.0/postgresql-42.6.0.jar $(JSRC)/lib
	cp $(MVN_REPO)/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar $(JSRC)/lib
	cp $(MVN_REPO)/org/slf4j/slf4j-simple/1.7.36/slf4j-simple-1.7.36.jar $(JSRC)/lib
	cp $(MVN_REPO)/ch/qos/logback/logback-core/1.4.7/logback-core-1.4.7.jar  $(JSRC)/lib
	cp $(MVN_REPO)/ch/qos/logback/logback-classic/1.4.7/logback-classic-1.4.7.jar $(JSRC)/lib
	cp $(MVN_REPO)/com/fasterxml/jackson/core/jackson-databind/2.15.1/jackson-databind-2.15.1.jar $(JSRC)/lib
	cp $(MVN_REPO)/com/fasterxml/jackson/core/jackson-annotations/2.15.1/jackson-annotations-2.15.1.jar $(JSRC)/lib
	cp $(MVN_REPO)/com/fasterxml/jackson/core/jackson-core/2.15.1/jackson-core-2.15.1.jar $(JSRC)/lib


libs:
	mkdir -p $(JSRC)/lib
	cp ../../../java-libs/lib/*.jar $(JSRC)/lib
	cp $(AUTH)/lib/*.jar $(JSRC)/lib

build-auth:
	make -C $(AUTH) build

clean-auth:
	make -C $(AUTH) clean


clean-libs: clean-auth
	-rm -fr $(JSRC)/lib


.PHONY: build
build: build-auth libs
	make -C $(JSRC) VERSION=$(VERSION)
	-rm -fr $(ROOT)/WEB-INF/classes
	-rm -fr $(ROOT)/WEB-INF/lib
	cp -r $(JSRC)/classes $(ROOT)/WEB-INF/
	cp -r $(JSRC)/lib $(ROOT)/WEB-INF/

clean: clean-libs
	make -C $(JSRC) clean
	rm -fr $(ROOT)/WEB-INF/classes
	rm -fr $(ROOT)/WEB-INF/lib




.PHONY: war
war:
	cat resources/web.xml-begining resources/web.xml-format-filter resources/web.xml-$(AUTH)-filter  resources/web.xml-authorization-filter resources/web.xml-servlets resources/web.xml-ending > $(ROOT)/WEB-INF/web.xml
	#cat resources/web.xml-begining resources/web.xml-servlets resources/web.xml-ending > $(ROOT)/WEB-INF/web.xml
	@jar -cf $(WEBAPP_WAR) -C $(ROOT) index.html \
      $(ROOT)/*.xsl \
      $(ROOT)/*.js \
      $(ROOT)/WEB-INF/*.xml \
      $(ROOT)/WEB-INF/classes/* \
      $(ROOT)/WEB-INF/lib/*.jar
	@ls -l $(WEBAPP_WAR)


.PHONY: uninstall
uninstall : 
	rm -f $(ROOT)/WEB-INF/*.xml* $(WEBAPP_WAR) 


.PHONY: test
test:
	@echo "WEBAPP_WAR : "$(WEBAPP_WAR)
	@echo "ROOT       : "$(ROOT)

.PHONY: testwar
testwar:
	jar -tf $(WEBAPP_WAR)
