_version = $1

echo
echo "************"
echo Preparing update with version $_version
echo "************"
echo

_versionFormatted = ($_version toString) replaceAll "-" "."

_pid = "entaxy.update.$_versionFormatted"
_configExists = config:exists $_pid

featuresService = (service:get org.apache.karaf.features.FeaturesService)
configService = (service:get org.osgi.service.cm.ConfigurationAdmin)
configuration = ($configService getConfiguration $_pid)
properties = ($configuration getProperties)

_found = (new java.util.HashMap)

if { $_configExists } {
	_components = ( config:property-get -p $_pid "components") toString
	_componentsList = $_components split ","	

	totalCount = 0
	installedCount = 0

	each $_componentsList {
		_component = (${it}) trim
		_componentFeatureNameProp = "${_component}\.feature"
#		echo "${_componentFeatureNameProp}"
		_componentFeatureName = ( config:property-get -p $_pid ${_componentFeatureNameProp} )		
#		echo $_componentFeatureName
		_features = ($featuresService getFeatures $_componentFeatureName )

		each ($_features) {
			_v = ($it getVersion)
			_status = ($featuresService getState $it) toString
#			echo $_v 
#			echo ($it getId ) : ($it getName ) $_sqbr$_v']' '-' $_status
			if { $_status equals "Started" } {
				$_found put $_component $it

				_versionsProp = "${_component}\.versions"
#				echo ":${_versionsProp}:${_pid}"
				_allowedVersionsValues = ( ( config:property-get -p $_pid ${_versionsProp}) toString )
#				echo $_allowedVersionsValues
				_allowedVersionsValues = $_allowedVersionsValues split ","
				_allowedVersions = new ArrayList
				each $_allowedVersionsValues {
					$_allowedVersions add (($it trim) replaceAll "-" ".")
				}
#				echo "ALLOWED VERSIONS: ${_allowedVersions}"	
				_allowed = $_allowedVersions contains $_v
				_installed = $_versionFormatted equals $_v
#				echo "ALLOWED:${_allowed}"

				_updateRepo = ($properties get "${_component}.update.repo")

				config:edit $_pid


				config:property-set "${_component}.current" ($it getId)
				config:property-set "${_component}.current.repo" ($it getRepositoryUrl)
				config:property-set "${_component}.current.version" $_v
				config:property-set "${_component}.current.allowed" $_allowed
				if { $_allowed } {
					config:property-set "${_component}.status" "PREPARED"
				} else {
					if { $_installed } {
						%(installedCount+=1)	
						config:property-set "${_component}.status" "VERSION_MISMATCH"
					} else {
						config:property-set "${_component}.status" "INSTALLED"
					}
				}
				
				if { ($_updateRepo equals '') } {
#					echo "SET REPO"
					_currentRepoSplitted =  ((($.context bundle) loadClass java.util.Arrays) asList (($it getRepositoryUrl) split "/")) 
#					echo $_currentRepoSplitted
					_group = (${_currentRepoSplitted} get 0)
					_artifact = (${_currentRepoSplitted} get 1)
					_updateRepo = "${_group}/${_artifact}/${_version}/xml\/update"
#					echo $_updateRepo
					config:property-set "${_component}.update.repo" $_updateRepo
				}	

				config:update	 
				%(totalCount+=1)	

#				echo "FOUND:"
#				echo ($it getId)
			} 
		}
		
	}

	_configFeature = ($featuresService getFeature "entaxy-update-configuration" $_version)

	config:edit $_pid

	_updateId = ($properties get "update.id")
	if { $_updateId equals '' } {
		config:property-set "update.id" "entaxy-update"
	}

	_updateVersion = ($properties get "update.version")
	if { $_updateVersion equals '' } {
		config:property-set "update.version" $1
	}

	_updateType = ($properties get "update.type")
	if { $_updateType equals '' } {
		config:property-set "update.type" "VERSION"
	}

	if { $installedCount = 0 } {
		config:property-set "update.status" "PREPARED"
	} else {
		if { $installedCount = $totalCount } {
			config:property-set "update.status" "INSTALLED"
		} else {
			config:property-set "update.status" "PARTIAL"
		}
	}
	config:property-set "update.repo" ($_configFeature getRepositoryUrl)

	config:update	 

	source scripts/update/update-info $_version
} else {
	echo "!!!"
	echo "Update configuration for version $_version NOT found"
	echo "Please run \'entaxy:get-update "${_version}"\'"
}