# source scripts/updtae/utils
# source scripts/updtae/operations

__EXEC_PLAN = {

	_thePlan = $1

	each $_thePlan {

		_data = $it

		# echo "DATA :: ${_data}"

		_rebootVal = ($_data get "reboot")
		_reboot = ($_rebootVal equals "true")
		_forceReboot = ($_rebootVal equals "force")

		_sleep = ($_data get "sleep")

		_operation = $_data get "operation"

		__RESULT__ = [error=false]

		_checkReboot = (__FALSE__)

		echo
		echo "\-\-\-"
		echo "    Perfoming operation: ${_operation}"

		_result = __exec_operation ${_operation} ${_data}


		if { $_operation equals "feature" } {

			_st = $__RESULT__ get "status"
			echo "\>\> ...${_st}"

			if { NOT { $_result } } {
				echo "\>\> Failed upgrading \[${_featureName}\] from repo \[${_repo}\]"
				$__RESULT__ put "error" true
				$__RESULT__ put "status" "FAILED"
				break
			}

			if { $_st equals "ALREADY_INSTALLED" } {
				echo "\>\> No changes made for \[${_featureName}\] from repo \[${_repo}\]"
				continue
			}

			if { $_st equals "INSTALLED" } {
				echo "\>\> Successfully installed ${_featureName} from repo ${_repo}"
				_checkReboot = (__TRUE__)
			}
		}
	
		# end of operation
		echo

		# ensure we have status after last operation
		sz = $installPlan size
		_lastData = $installPlan get %(sz-1)
		if { $_lastData equals $_data } {
			_st = $__RESULT__ get "status"
			if { "${_st}" equals "" } {
				$__RESULT__ put "status" "INSTALLED"
			}
		}

		# sleep - needed for refreshed initializers to complete
		if { NOT { $_sleep equals "" } } {
			echo "Sleeping for ${_sleep} seconds"
			shell:sleep $_sleep
		}

		# process reboot
		if { $_checkReboot } {
			if { $_reboot } {
				echo "~~~~~~~~"
				echo "System must be rebooted"
				$__RESULT__ put "reboot" "true"
				$__RESULT__ put "status" "INSTALLING"
				break
			}
			if { $_forceReboot } {
				echo "~~~~~~~~"
				echo "System must be rebooted"
				$__RESULT__ put "reboot" "force"
				$__RESULT__ put "status" "INSTALLING"
				break
			}
		}


	}	

}