# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# protect the default target for this file from the targets in Makefile.global

include ../common.mk

default: stage-notest

PXF_ROOT_DIR := $(shell cd .. && pwd)
PXF_VERSION ?= $(shell cat $(PXF_ROOT_DIR)/version)
PXF_API_VERSION ?= $(shell cat $(PXF_ROOT_DIR)/api_version)

PXF_GRADLE_PROPERTIES = -Pversion=$(PXF_VERSION) -PapiVersion=$(PXF_API_VERSION)

# Gradle can start on a JRE, but compiling PXF needs a JDK. Detect a JRE-only
# installation here so the build fails with an actionable message instead of
# Gradle's "does not provide the required capabilities: [JAVA_COMPILER]".
JAVAC := $(if $(JAVA_HOME),$(JAVA_HOME)/bin/javac,$(shell command -v javac 2>/dev/null))

.PHONY: check-jdk
check-jdk:
	@if [ ! -x "$(JAVAC)" ]; then \
		echo "ERROR: no Java compiler (javac) found -- a JDK is required to build PXF, a JRE is not enough."; \
		if [ -n "$(JAVA_HOME)" ]; then \
			echo "       JAVA_HOME=$(JAVA_HOME) has no bin/javac, so it points at a JRE."; \
		else \
			echo "       JAVA_HOME is not set and javac is not on PATH."; \
		fi; \
		echo "       Install a JDK and point JAVA_HOME at it, for example:"; \
		echo "         Debian/Ubuntu: sudo apt-get install -y openjdk-11-jdk"; \
		echo "         RHEL/Rocky:    sudo dnf install -y java-11-openjdk-devel"; \
		echo "         export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"; \
		echo "       If a build already failed here, also run './gradlew --stop'."; \
		echo "       See TROUBLESHOOTING.md for details."; \
		exit 2; \
	fi

.PHONY: prepare-gradle-wrapper
prepare-gradle-wrapper: check-jdk
	@APP_HOME="$(CURDIR)" bash ./gradlew-install.sh

help:
	@echo
	@echo   "Possible targets"
	@echo	"  - all (clean, compile, test, stage)"
	@echo	"  - clean - cleans the workspace of all produced artifacts"
	@echo	"  - compile - compiles all PXF modules and builds their JAR files"
	@echo	"  - test - runs unit tests for all PXF modules"
	@echo   "  - coverage - runs unit tests for all PXF modules and gives a coverage report"
	@echo	"  - install - setup PXF in the configured deployPath"
	@echo	"  - stage - build PXF server and stage resulting artifacts for packaging (no JDBC drivers in build/stage/lib)"
	@echo	"  - stage-notest - same as stage but skip tests (no JDBC drivers in build/stage/lib)"
	@echo	"  - stage-jdbc-drivers - copy JDBC driver JARs to build/stage/lib"
	@echo	"  - install-jdbc-drivers - setup PXF JDBC drivers in the configured deployPath"
	@echo	"  - doc - creates aggregate javadoc under docs"

all: prepare-gradle-wrapper
	./gradlew $(PXF_GRADLE_PROPERTIES) clean stage

compile: prepare-gradle-wrapper
	./gradlew $(PXF_GRADLE_PROPERTIES) compileJava

GRADLEW_TEST_PARAMS = test
ifneq "$(TEST)" ""
	# find test file, remove leading "./" and trailing ".java"
	TEST_FILE = $(subst ./,,$(subst .java,,$(shell find . -name $(TEST).java)))
	# parse out the gradle project by grabbing top-level dir name
	PROJECT = $(word 1,$(subst /, ,$(TEST_FILE)))
	# get java-style path with dots, starting at "org"
	TEST_PATH = $(subst /,.,$(subst $(PROJECT)/src/test/java/,,$(TEST_FILE)))
	GRADLEW_TEST_PARAMS = :$(PROJECT):test --rerun-tasks --tests $(TEST_PATH)
endif

test unittest: prepare-gradle-wrapper
	@if [ -n '$(TEST)' ] && [ -z '${TEST_FILE}' ]; then \
		echo 'Test $(TEST) was not found'; \
		exit 1; \
	fi
	./gradlew $(PXF_GRADLE_PROPERTIES) ${GRADLEW_TEST_PARAMS}

coverage: prepare-gradle-wrapper
	./gradlew $(PXF_GRADLE_PROPERTIES) ${GRADLEW_TEST_PARAMS} jacocoTestReport
	@echo
	@echo  "Coverage reports can be found within each server module under <module-name>/build/reports/jacoco"
	@echo  "For example, the pxf-service coverage report is located at $(shell pwd)/pxf-service/build/reports/jacoco/test/html/index.html"

.PHONY: stage
stage: prepare-gradle-wrapper
	./gradlew $(PXF_GRADLE_PROPERTIES) test stage
	install -m 744 -d "build/stage/lib"
	install -m 744 -d "build/stage/lib/native"
	install -m 744 -d "build/stage/servers"
	install -m 744 -d "build/stage/servers/default"
	install -m 700 -d "build/stage/logs"
	install -m 700 -d "build/stage/run"
	install -m 700 -d "build/stage/keytabs"

.PHONY: stage-notest
stage-notest: prepare-gradle-wrapper
	./gradlew $(PXF_GRADLE_PROPERTIES) stage -x test
	install -m 744 -d "build/stage/lib"
	install -m 744 -d "build/stage/lib/native"
	install -m 744 -d "build/stage/servers"
	install -m 744 -d "build/stage/servers/default"
	install -m 700 -d "build/stage/logs"
	install -m 700 -d "build/stage/run"
	install -m 700 -d "build/stage/keytabs"

.PHONY: stage-jdbc-drivers
stage-jdbc-drivers: prepare-gradle-wrapper
	rm -rf build/stage/lib
	./gradlew $(PXF_GRADLE_PROPERTIES) stageJdbcDrivers

.PHONY: install-jdbc-drivers
install-jdbc-drivers: stage-jdbc-drivers
	@if [ -z "$(PXF_HOME)" ]; then \
		echo "ERROR: PXF_HOME is not set"; exit 2; \
	fi
	mkdir -p "$(PXF_HOME)"/lib
	cp -R build/stage/lib/. "$(PXF_HOME)"/lib/

clean: prepare-gradle-wrapper
	./gradlew clean
	rm -rf build

.PHONY: clean-all
clean-all: clean

distclean maintainer-clean: clean

doc: prepare-gradle-wrapper
	./gradlew $(PXF_GRADLE_PROPERTIES) aggregateJavadoc

.PHONY: install
install: stage
	@if [ -z "$(PXF_HOME)" ]; then \
		echo "ERROR: PXF_HOME is not set"; exit 2; \
	fi
	mkdir -p "$(PXF_HOME)"
	cp -R build/stage/* "$(PXF_HOME)"

.PHONY: install-server
install-server: stage-notest
	@if [ -z "$(PXF_HOME)" ]; then \
		echo "ERROR: PXF_HOME is not set"; exit 2; \
	fi
	mkdir -p "$(PXF_HOME)"
	cp -R build/stage/* "$(PXF_HOME)"

.PHONY: version
version: prepare-gradle-wrapper
	@./gradlew -q version
