
source scripts/update/utils

# Installs (if not present) feature repo and upgrades (if not present) feature
# $1 - map of params
# return true if succeded
__check_upgrade_feature = {

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

	_params = $1

	# echo " :: __check_upgrade_feature :: "

	_repoToInstall = ($_params get "repo")
	_featureName = ($_params get "feature")
	_upgrade = ($_params get "upgrade")

	_u = ""

	if { $_upgrade } {
		_u = "-u"
	}

	_res = (__TRUE__)

	echo "... process repo: ${_repoToInstall} ..."

	_repoResult = __utils_check_install_repo $_repoToInstall
	if { NOT { $_repoResult } } {
		__RESULT__ = [error=true message="Failed installing repository: ${_repoToInstall}" status="FAILED"]
		_res = (__FALSE__)
	} else {
		echo "... process feature: ${_featureName} ..."
	
		_feature = ($featuresService getFeature $_featureName $_version)
		if { $_feature equals '' } {
			__RESULT__ = [error=true message="Feature not found: ${_featureName}" status="FAILED"]
			_res = (__FALSE__)
		} else {
			_featureId = $_feature getId
			_state = ($featuresService getState $_featureId) toString

			if { NOT { $_state equals "Started" } } {
				echo "\>\> Installing feature ${_featureName}/${_version} ..."
				try { 
					feature:install -v "${_u}" "${_featureName}/${_version}" 
					__RESULT__ = [error=false status="INSTALLED"]
				} catch {
					__RESULT__ = [error=true message="Error installing feature: ${_featureName}" status="FAILED"]
					echo "\>\> ... failed"
					_res = (__FALSE__)
				}
				if { $_res } {
					echo "\>\> ... installed"
				
					_state = ($featuresService getState $_featureId) toString

					if { NOT { $_state equals "Started" } } {
						__RESULT__ = [error=true message="Error installing feature: ${_featureName}" status="FAILED"]
						_res = (__FALSE__)
					} 

				}
			} else {
				__RESULT__ = [error=false status="ALREADY_INSTALLED"]
			}


		}

	}

	_s = $__RESULT__ get "status"
#	echo "Return status: ${_s}"
	
	$_res
}

__feature_post = {
	_res = $1
	_st = $__RESULT__ get "status"
	echo "\>\> ...${_st}"
	_toReturn = []

	if { NOT { $_res } } {
		echo "\>\> Failed upgrading \[${_featureName}\] from repo \[${_repo}\]"
		$__RESULT__ put "error" true
		$__RESULT__ put "status" "FAILED"
		_toReturn = [break=(__TRUE__) checkReboot = (__FALSE__)]
	}

	if { $_st equals "ALREADY_INSTALLED" } {
		echo "\>\> No changes made for \[${_featureName}\] from repo \[${_repo}\]"
		_toReturn = [continue=(__TRUE__) checkReboot = (__FALSE__)]
	}

	if { $_st equals "INSTALLED" } {
		echo "\>\> Successfully installed ${_featureName} from repo ${_repo}"
		_toReturn = [checkReboot = (__TRUE__)]
	}

#	echo "_toReturn :: "
#	echo ${_toReturn}

	$_toReturn
}

commonsIoId = [ ((list -s -t 0 | grep "org.apache.commons.io") split ' ') ] get 0
# echo "!!!! COMMONS-IO :: ${commonsIoId}"

__backup = {
	_params = $1

	_action = ($_params get "action")
	_origin = ($_params get "source")
	_bak = "${_origin}.bak"

	echo "\>\> EXEC BACKUP \[$_action\]"

	if { $_action equals "create" } {
		_overwrite = ($_params get "overwrite")
		_from = new (($.context bundle) loadClass java.io.File) $_origin
		_to = new (($.context bundle) loadClass java.io.File) "$_bak"
		if { $_overwrite } {
			_exists = ($_to exists)
			if { NOT { $_exists } } {
				_r = ((($.context bundle %(commonsIoId+0)) loadClass org.apache.commons.io.FileUtils) copyFile $_from $_to)
			} else {
				echo "\>\> .. skipping: backup already exists"		
			}
		} else {
			_r = ((($.context bundle %(commonsIoId+0)) loadClass org.apache.commons.io.FileUtils) copyFile $_from $_to)
		}
	}
	if { $_action equals "restore" } {
		_to = new (($.context bundle) loadClass java.io.File) $_origin
		_from = new (($.context bundle) loadClass java.io.File) "$_bak"
		_r = ((($.context bundle %(commonsIoId+0)) loadClass org.apache.commons.io.FileUtils) copyFile $_from $_to)
	}	
	if { $_action equals "clean" } {
		_from = new (($.context bundle) loadClass java.io.File) "$_bak"
		_exists = ($_from exists)
		if { $_exists } {
			_r = ((($.context bundle %(commonsIoId+0)) loadClass org.apache.commons.io.FileUtils) forceDelete $_from)
		}
	}
}

__OPERATIONS = new java.util.HashMap

__add_operation = {
	$__OPERATIONS put $1 $2
}

__exec_operation = {
	_oper = $1
	_data = $2
#	echo "__exec_operation ${_oper} ${_data}"
#	echo ($__OPERATIONS get $_oper)
	if { $__OPERATIONS containsKey $_oper } {
		_toExec = $__OPERATIONS get $_oper
#		echo $_toExec
		_execString = "${_toExec} \${_data}"
#		echo $_execString
		eval $_execString
	} else {
		"OPERATION_NOT_FOUND"
	}
}

__exec_operation_post = {
	_oper = $1
	_mainResult = $2
	echo "__exec_operation ${_oper}-post ${_mainResult}"
	echo ($__OPERATIONS get "${_oper}-post")
	if { $__OPERATIONS containsKey "${_oper}-post" } {
		_toExec = $__OPERATIONS get "${_oper}-post"
		echo $_toExec
		_execString = "${_toExec} \${_mainResult}"
		echo $_execString
		eval $_execString
	} else {
		"NOT_FOUND"
	} 
}

__add_operation "feature" __check_upgrade_feature
__add_operation "feature-post" __feature_post
__add_operation "backup" __backup