#!/bin/bash

#source $(dirname $0)/common_parameters.inc.sh

# Common Parameters

# EAP_VERSION
# EAP_ZIP_URL
# EAP_MAVEN_REPO_URL
# QUICKSTARTS_ZIP_URL

MAVEN_HOME=/opt/maven-3.3.9
JAVA_HOME=/opt/java/java-1.8.0-openjdk

EAP_VERSION=7.1.0.DR11
EAP_ZIP_URL=http://download.devel.redhat.com/devel/candidates/JBEAP/JBEAP-${EAP_VERSION}/jboss-eap-${EAP_VERSION}.zip
#EAP_ZIP_URL=file:///home/development/artifacts/JBEAP-${EAP_VERSION}/jboss-eap-${EAP_VERSION}.zip
EAP_MAVEN_REPO_URL=http://download.devel.redhat.com/devel/candidates/JBEAP/JBEAP-${EAP_VERSION}/jboss-eap-${EAP_VERSION}-maven-repository.zip
#EAP_MAVEN_REPO_URL=file:///home/development/artifacts/JBEAP-${EAP_VERSION}/jboss-eap-${EAP_VERSION}-maven-repository.zip
QUICKSTARTS_ZIP_URL=http://download.devel.redhat.com/devel/candidates/JBEAP/JBEAP-${EAP_VERSION}/jboss-eap-${EAP_VERSION}-quickstarts.zip
#QUICKSTARTS_ZIP_URL=file:///home/development/artifacts/JBEAP-${EAP_VERSION}/jboss-eap-${EAP_VERSION}-quickstarts.zip
#SKIP_CLEAN_WORKSPACE=1

#source $(dirname $0)/common.inc.sh

assert_workspace_is_defined() {
	local WORKSPACE_CLASSIFIER=$1
	
	if [ "${WORKSPACE}" == "" ]; then
		WORKSPACE=$(pwd)/tmp${WORKSPACE_CLASSIFIER}
	fi
	if [ ! -d ${WORKSPACE} ]; then
		mkdir -p ${WORKSPACE}
	fi
	echo Using workspace ${WORKSPACE}
	cd ${WORKSPACE}
}

clean_workspace() {
	if [[ "x${SKIP_CLEAN_WORKSPACE}" == "x1" || "x${SKIP_CLEAN_WORKSPACE}" == "xtrue" ]] ; then
		echo Skipping cleaning workspace ${WORKSPACE}
		return
	fi
	assert_folder_is_empty ${WORKSPACE} "Cleaning workspace ${WORKSPACE}"
	cd $WORKSPACE
}

assert_folder_is_empty() {
	local FOLDER=$1
	local MESSAGE=$2

	if [[ ! "x${MESSAGE}" == "x" ]]; then
		echo $MESSAGE
	fi

	rm -rf ${FOLDER}
	mkdir -p ${FOLDER}
}
download_file() {
	local MESSAGE=$1
	local URL=$2
	local TARGET_FILE=$3

	echo $MESSAGE
	if [ ${URL:0:7} == "file://" ]; then
		local FILE=${URL:7:1000}
		cp $FILE $TARGET_FILE
	else
		wget -q -O $TARGET_FILE $URL
	fi
}

assert_workspace_is_defined "-verify-quickstarts-managed"
clean_workspace

setup_maven_environment() {
	export JAVA_HOME=${JAVA_HOME}
	export M2_HOME=${MAVEN_HOME}
	mvn -v
}

unzip_quickstarts() {
	echo "Unzip quickstarts"
	FILE=${WORKSPACE}/jboss-eap-${EAP_VERSION}-quickstarts.zip
	download_file "Download maven repo zip file jboss-eap-${EAP_VERSION}-quickstarts.zip from ${QUICKSTARTS_ZIP_URL}" ${QUICKSTARTS_ZIP_URL} ${FILE}
	unzip -q $FILE -d ${WORKSPACE}
}

unzip_maven_repository() {
	echo "Unzip maven repository"
	FILE=${WORKSPACE}/jboss-eap-${EAP_VERSION}-maven-repository.zip
	download_file "Download maven repo zip file jboss-eap-${EAP_VERSION}-maven-repository.zip from ${EAP_MAVEN_REPO_URL}" ${EAP_MAVEN_REPO_URL} ${FILE}
	unzip -q $FILE -d ${WORKSPACE}
}

unzip_server() {
	echo "Unzip server"
	FILE=${WORKSPACE}/jboss-eap-${EAP_VERSION}.zip
	download_file "Download server zip file jboss-eap-${EAP_VERSION}.zip from ${EAP_ZIP_URL}" ${EAP_ZIP_URL} ${FILE}
	unzip -q $FILE -d ${WORKSPACE}
}

start_server() {
	echo "Start server with arguments\"$@\""
	${WORKSPACE}/jboss-eap-7.1/bin/standalone.sh $@ &
	sleep 4
}

stop_server() {
	echo "Shutdown server"
	kill $(jps | grep modules.jar | awk -F' ' '{print $1}')
}

run_test() {
	EAP_MAVEN_REPO_PATH=${WORKSPACE}/jboss-eap-7.1.0.Alpha1-maven-repository/maven-repository
	MAVEN_SETTINGS=${WORKSPACE}/settings.xml
	MAVEN_REPO_LOCAL=${WORKSPACE}/local-repository

	QS_DIR_NAME=helloworld-jms
	QS_DIR=${WORKSPACE}/jboss-eap-7.1.0.Alpha1-quickstarts/${QS_DIR_NAME}

	echo "Create maven settings"
	cp ${EAP_MAVEN_REPO_PATH}/../example-settings.xml ${MAVEN_SETTINGS} 
	sed -i 's#file:[^<]*#file://'${EAP_MAVEN_REPO_PATH}'#gi' ${MAVEN_SETTINGS}

	echo "Setup arquillian test environment"
	export JBOSS_HOME=${WORKSPACE}/jboss-eap-7.1

	EAP7_HOME=${WORKSPACE}/jboss-eap-7.1

	echo "Create quickstart user"
	$EAP7_HOME/bin/add-user.sh -a -u 'quickstartUser' -p 'quickstartPwd1!' -g 'guest'

	start_server -c standalone-full-ha.xml
	echo "Configure quickstart jms"
	$EAP7_HOME/bin/jboss-cli.sh --connect --file=${QS_DIR}/configure-jms.cli

	#echo "mvn -f ${QS_DIR}/pom.xml -s ${MAVEN_SETTINGS} -B -Dmaven.repo.local=${MAVEN_REPO_LOCAL} clean test -Parq-wildfly-managed"
	#mvn -f ${QS_DIR}/pom.xml -s ${MAVEN_SETTINGS} -B -Dmaven.repo.local=${MAVEN_REPO_LOCAL} clean test -Parq-wildfly-managed
	echo "mvn -f ${QS_DIR}/pom.xml -s ${MAVEN_SETTINGS} -B -Dmaven.repo.local=${MAVEN_REPO_LOCAL} clean compile exec:java"
	mvn -f ${QS_DIR}/pom.xml -s ${MAVEN_SETTINGS} -B -Dmaven.repo.local=${MAVEN_REPO_LOCAL} clean compile exec:java

	stop_server
}

setup_maven_environment
#rm -rf ${WORKSPACE}/jboss-eap-7.1.0.Alpha1-quickstarts
unzip_quickstarts
#rm -rf ${WORKSPACE}/jboss-eap-7.1.0.Alpha1-maven-repository
unzip_maven_repository
#rm -rf ${WORKSPACE}/jboss-eap-7.1
unzip_server
run_test
#	stop_server
#	start_server -c standalone-full.xml
