###
# ~~~~~~licensing~~~~~~
# entaxy-hawtio
# ==========
# Copyright (C) 2020 - 2024 EmDev LLC
# ==========
# You may not use this file except in accordance with the License Terms of the Copyright
# Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
# rights to the Software and any copies are the property of the Copyright Holder. Unless
# it is explicitly allowed the Copyright Holder, the User is prohibited from using the
# Software for commercial purposes to provide services to third parties.
# 
# The Copyright Holder hereby declares that the Software is provided on an "AS IS".
# Under no circumstances does the Copyright Holder guarantee or promise that the
# Software provided by him will be suitable or not suitable for the specific purposes
# of the User, that the Software will meet all commercial and personal subjective
# expectations of the User, that the Software will work properly, without technical
# errors, quickly and uninterruptedly.
# 
# Under no circumstances shall the Copyright Holder or its Affiliates is not liable
# to the User for any direct or indirect losses of the User, his expenses or actual
# damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
# or damage to data, property, etc.
# ~~~~~~/licensing~~~~~~
###
echo 
echo
echo "************"
echo "Welcome to Entaxy UI installation script"
echo "************"

echo
echo "** Checking feature repository: ru.entaxy.esb.ui/entaxy-hawtio/${project.version}/xml/features ..."

_repo = (feature:repo-list | grep ru.entaxy.esb/ui)
_repo_length = ($_repo length)
if { $_repo_length'' equals "0" } {
	echo "\>\> Installing Entaxy UI repository..."
	feature:repo-add mvn:ru.entaxy.esb/ui/${project.version}/xml/features
	echo "\>\> ... installed"
} else {
	echo "\>\> Entaxy UI repository is already installed"
}

echo
echo "** Checking entaxy-underlying-configuration feature ..."

service = (service:get org.apache.karaf.features.FeaturesService)

_config_feature = ($service getFeature entaxy-underlying-configuration)
_config_id = $_config_feature getId
_status = ($service getState $_config_id) toString

_in_shutdown = "0"

if { $_status equals "Uninstalled" } {
	echo "\>\> Installing feature..."
	feature:install entaxy-underlying-configuration
	echo "\>\> ... installed"
	echo
	if { $1'' equals "skipReboot" } {
		echo "** the system will skip reboot for applying JRE exports"	
		echo " \( see etc/jre.properties \)"
		echo " due to 'skipReboot argument'"
		echo "** If further installation will fail with errors please reboot"
	} else {
		echo "** the system will reboot now to apply JRE exports"
		echo " \( see etc/jre.properties \)"
		echo
		echo "** PLEASE RUN THE SCRIPT AGAIN AFTER RESTART **"
		_in_shutdown = "1"
		shutdown -r -f
	}
} else {
	echo "\>\> Entaxy configuration is already installed"
}

# shutdown goes async so karaf can begin next installation while rebooting
# so we use marker to prevent it
if { $_in_shutdown equals "0" } {

	echo
	_status = ( ($service getState $_config_id) toString )

	if { NOT { $_status equals "Started" } } {
		echo "** Current configuration feature status is " $_status
		echo "** Please clean the container and run installation again "
	} else {
		echo
		echo "** Installing Entaxy UI components in verbose mode..."

		_ui_feature =  ($service getFeature entaxy-ui)
		_ui_id = $_ui_feature getId
		_status = ( ($service getState $_ui_id) toString )

		if { $_status equals "Uninstalled" } {
			feature:install -v -r entaxy-ui
			echo
			echo "\>\> ... installed"
		} else {
			echo "\>\> Entaxy UI is already installed"
		}
	}
	
	echo
	echo "** Thank you!"

}
