#!/bin/bash

# Place this script into an empty directory
# Unzip list of servers to test also in that directory, e.g.:
# $ rm -fr jboss-eap-7.* && unzip ~/Downloads/jboss-eap-7.2.9.zip && unzip ~/Downloads/jboss-eap-7.3.2.zip && unzip ~/Downloads/jboss-eap-7.4.0.CD20-CR2.zip
# or
# rm -fr wildfly-* && unzip ~/Downloads/wildfly-14.0.0.Final.zip && unzip ~/Downloads/wildfly-15.0.0.Final.zip && unzip ~/Downloads/wildfly-16.0.0.Final.zip && unzip ~/Downloads/wildfly-17.0.0.Final.zip && unzip ~/Downloads/wildfly-18.0.0.Final.zip && unzip ~/Downloads/wildfly-19.0.0.Final.zip && unzip ~/Downloads/wildfly-20.0.0.Final.zip

function killServer() {
    local pid=$(ps -aux | grep "java -D\[Standalone" | cut -d ' ' -f 2)

    kill -9 "${pid}"
}

function main() {
    local server_dirs=$(ls)

    local server
    for server in ${server_dirs}; do
	test -d "${server}" || continue

	echo "################### PROCESSING '${server}' #############################"

	echo "Starting server on background..."
	./${server}/bin/standalone.sh >/dev/null &
        sleep 5

	echo "Adding dir-context..."
	./${server}/bin/jboss-cli.sh -c "/subsystem=elytron/dir-context=foo:add(url='file:///tmp')"

	echo "Listing dir-context 'refferal-mode' attribute default value via CLI"
	./${server}/bin/jboss-cli.sh -c "/subsystem=elytron/dir-context=foo:read-attribute(name=referral-mode)"

	echo "Printing dir-context 'refferal-mode' attribute default value in raw xml configuration file"
	grep "referral-mode" ./${server}/standalone/configuration/standalone.xml

	echo "Set custom value to dir-context 'referral-mode' attribute via CLI"
	./${server}/bin/jboss-cli.sh -c "/subsystem=elytron/dir-context=foo:write-attribute(name=referral-mode,value=\"THROW\")"

	echo "Performing server reload"
	./${server}/bin/jboss-cli.sh -c "reload"

	echo "Listing dir-context 'refferal-mode' attribute current value via CLI"
	./${server}/bin/jboss-cli.sh -c "/subsystem=elytron/dir-context=foo:read-attribute(name=referral-mode)"

	echo "Printing dir-context 'refferal-mode' attribute current value in raw xml configuration file"
	grep "referral-mode" ./${server}/standalone/configuration/standalone.xml

	killServer
	echo ""
    done
}

# main part of the script
main
