-- Version: 1.0.2512.3044
-- Overwrite rollout function:
--     you can specify a function variable in your SDK specific lua file to replace the default rollout mechanism
--     Your sdk file should look like this:
--
--     function yourSdkRolloutFunction(updateInformation, decideJsonTable)
--        ...do sth and return nil or a table like this (in case api_version = 1.0)...
--        if doRollout ~= true then 
--           return nil -- this will lead that the stable version is used
--
--        return {
--              ["skip_update"] = false,
--              ["update_version"] = decideJsonTable["rollout_version"]
--            }
--     end
-- -- !!!! this is the important step as the function has to be returned only than the lua is working !!!! ...
--     sdkSpecificRolloutFunctionV_1_0 = yourSdkRolloutFunction
--
-- Extend decision logic function:
--     you can specify a function variable in your SDK specific lua file to extend the decision logic 
--    (this function is called before rollout = stable check and before the rollout decision logic)
--     This allows to also install another version than rollout or stable e.g. when you need to update 
--     old versions first to a specific version. e.g. jump kit
--     Your sdk file should look like this:
--
--     function yourSdkExtensionFunction(updateInformation, decideJsonTable)
--        ...do sth and return nil or a table like this (in case api_version = 1.0)...
--        local jumpKit = "1.2.3.4"
--        if compareVersions(currentVersion, jumpKit) < 0 then
--            return {
--                ["skip_update"] = false,
--                ["update_version"] = jumpKit
--                }
--        end
--        return nil
--    end
--
-- -- !!!! this is the important step as the function has to be returned only than the lua is working !!!! ...
--     extensionFunction = yourSdkExtensionFunction
--
-- require "decision_logic"
loadstring = load -- replace deprecated loadstring function used in json4lua with its replacement load
-- local jsonLibrary = require "share/lua/5.4/json" vvvvvv
-----------------------------------------------------------------------------
-- JSON4Lua: JSON encoding / decoding support for the Lua language.
-- json Module.
-- Author: Craig Mason-Jones
-- Homepage: http://json.luaforge.net/
-- Version: 0.9.40
-- This module is released under the MIT License (MIT).
-- Please see LICENCE.txt for details.
--
-- USAGE:
-- This module exposes two functions:
--   json.encode(o)
--     Returns the table / string / boolean / number / nil / json.null value as a JSON-encoded string.
--   json.decode(json_string)
--     Returns a Lua object populated with the data encoded in the JSON string json_string.
--
-- REQUIREMENTS:
--   compat-5.1 if using Lua 5.0
--
-- CHANGELOG
--   0.9.20 Introduction of local Lua functions for private functions (removed _ function prefix). 
--          Fixed Lua 5.1 compatibility issues.
--   		Introduced json.null to have null values in associative arrays.
--          json.encode() performance improvement (more than 50%) through table.concat rather than ..
--          Introduced decode ability to ignore /**/ comments in the JSON string.
--   0.9.10 Fix to array encoding / decoding to correctly manage nil/null values in arrays.
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- Imports and dependencies
-----------------------------------------------------------------------------
local math = require('math')
local string = require("string")
local table = require("table")
-----------------------------------------------------------------------------
-- Module declaration
-----------------------------------------------------------------------------
local json = {}             -- Public namespace
local json_private = {}     -- Private namespace
-- Public functions
-- Private functions
local decode_scanArray
local decode_scanComment
local decode_scanConstant
local decode_scanNumber
local decode_scanObject
local decode_scanString
local decode_scanWhitespace
local encodeString
local isArray
local isEncodable
-----------------------------------------------------------------------------
-- PUBLIC FUNCTIONS
-----------------------------------------------------------------------------
--- Encodes an arbitrary Lua object / variable.
-- @param v The Lua object / variable to be JSON encoded.
-- @return String containing the JSON encoding in internal Lua string format (i.e. not unicode)
function json.encode (v)
  -- Handle nil values
  if v==nil then
    return "null"
  end
  
  local vtype = type(v)
  -- Handle strings
  if vtype=='string' then    
    return '"' .. json_private.encodeString(v) .. '"'	    -- Need to handle encoding in string
  end
  
  -- Handle booleans
  if vtype=='number' or vtype=='boolean' then
    return tostring(v)
  end
  
  -- Handle tables
  if vtype=='table' then
    local rval = {}
    -- Consider arrays separately
    local bArray, maxCount = isArray(v)
    if bArray then
      for i = 1,maxCount do
        table.insert(rval, json.encode(v[i]))
      end
    else	-- An object, not an array
      for i,j in pairs(v) do
        if isEncodable(i) and isEncodable(j) then
          table.insert(rval, '"' .. json_private.encodeString(i) .. '":' .. json.encode(j))
        end
      end
    end
    if bArray then
      return '[' .. table.concat(rval,',') ..']'
    else
      return '{' .. table.concat(rval,',') .. '}'
    end
  end
  
  -- Handle null values
  if vtype=='function' and v==null then
    return 'null'
  end
  
  assert(false,'encode attempt to encode unsupported type ' .. vtype .. ':' .. tostring(v))
end
--- Decodes a JSON string and returns the decoded value as a Lua data structure / value.
-- @param s The string to scan.
-- @param [startPos] Optional starting position where the JSON string is located. Defaults to 1.
-- @param Lua object, number The object that was scanned, as a Lua table / string / number / boolean or nil,
-- and the position of the first character after
-- the scanned JSON object.
function json.decode(s, startPos)
  startPos = startPos and startPos or 1
  startPos = decode_scanWhitespace(s,startPos)
  assert(startPos<=string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']')
  local curChar = string.sub(s,startPos,startPos)
  -- Object
  if curChar=='{' then
    return decode_scanObject(s,startPos)
  end
  -- Array
  if curChar=='[' then
    return decode_scanArray(s,startPos)
  end
  -- Number
  if string.find("+-0123456789.e", curChar, 1, true) then
    return decode_scanNumber(s,startPos)
  end
  -- String
  if curChar==[["]] or curChar==[[']] then
    return decode_scanString(s,startPos)
  end
  if string.sub(s,startPos,startPos+1)=='/*' then
    return decode(s, decode_scanComment(s,startPos))
  end
  -- Otherwise, it must be a constant
  return decode_scanConstant(s,startPos)
end
--- The null function allows one to specify a null value in an associative array (which is otherwise
-- discarded if you set the value with 'nil' in Lua. Simply set t = { first=json.null }
function null()
  return null -- so json.null() will also return null ;-)
end
-----------------------------------------------------------------------------
-- Internal, PRIVATE functions.
-- Following a Python-like convention, I have prefixed all these 'PRIVATE'
-- functions with an underscore.
-----------------------------------------------------------------------------
--- Scans an array from JSON into a Lua object
-- startPos begins at the start of the array.
-- Returns the array and the next starting position
-- @param s The string being scanned.
-- @param startPos The starting position for the scan.
-- @return table, int The scanned array as a table, and the position of the next character to scan.
function decode_scanArray(s,startPos)
  local array = {}	-- The return value
  local stringLen = string.len(s)
  assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\n'..s )
  startPos = startPos + 1
  -- Infinite loop for array elements
  repeat
    startPos = decode_scanWhitespace(s,startPos)
    assert(startPos<=stringLen,'JSON String ended unexpectedly scanning array.')
    local curChar = string.sub(s,startPos,startPos)
    if (curChar==']') then
      return array, startPos+1
    end
    if (curChar==',') then
      startPos = decode_scanWhitespace(s,startPos+1)
    end
    assert(startPos<=stringLen, 'JSON String ended unexpectedly scanning array.')
    object, startPos = json.decode(s,startPos)
    table.insert(array,object)
  until false
end
--- Scans a comment and discards the comment.
-- Returns the position of the next character following the comment.
-- @param string s The JSON string to scan.
-- @param int startPos The starting position of the comment
function decode_scanComment(s, startPos)
  assert( string.sub(s,startPos,startPos+1)=='/*', "decode_scanComment called but comment does not start at position " .. startPos)
  local endPos = string.find(s,'*/',startPos+2)
  assert(endPos~=nil, "Unterminated comment in string at " .. startPos)
  return endPos+2  
end
--- Scans for given constants: true, false or null
-- Returns the appropriate Lua type, and the position of the next character to read.
-- @param s The string being scanned.
-- @param startPos The position in the string at which to start scanning.
-- @return object, int The object (true, false or nil) and the position at which the next character should be 
-- scanned.
function decode_scanConstant(s, startPos)
  local consts = { ["true"] = true, ["false"] = false, ["null"] = nil }
  local constNames = {"true","false","null"}
  for i,k in pairs(constNames) do
    if string.sub(s,startPos, startPos + string.len(k) -1 )==k then
      return consts[k], startPos + string.len(k)
    end
  end
  assert(nil, 'Failed to scan constant from string ' .. s .. ' at starting position ' .. startPos)
end
--- Scans a number from the JSON encoded string.
-- (in fact, also is able to scan numeric +- eqns, which is not
-- in the JSON spec.)
-- Returns the number, and the position of the next character
-- after the number.
-- @param s The string being scanned.
-- @param startPos The position at which to start scanning.
-- @return number, int The extracted number and the position of the next character to scan.
function decode_scanNumber(s,startPos)
  local endPos = startPos+1
  local stringLen = string.len(s)
  local acceptableChars = "+-0123456789.e"
  while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true)
	and endPos<=stringLen
	) do
    endPos = endPos + 1
  end
  local stringValue = 'return ' .. string.sub(s,startPos, endPos-1)
  local stringEval = loadstring(stringValue)
  assert(stringEval, 'Failed to scan number [ ' .. stringValue .. '] in JSON string at position ' .. startPos .. ' : ' .. endPos)
  return stringEval(), endPos
end
--- Scans a JSON object into a Lua object.
-- startPos begins at the start of the object.
-- Returns the object and the next starting position.
-- @param s The string being scanned.
-- @param startPos The starting position of the scan.
-- @return table, int The scanned object as a table and the position of the next character to scan.
function decode_scanObject(s,startPos)
  local object = {}
  local stringLen = string.len(s)
  local key, value
  assert(string.sub(s,startPos,startPos)=='{','decode_scanObject called but object does not start at position ' .. startPos .. ' in string:\n' .. s)
  startPos = startPos + 1
  repeat
    startPos = decode_scanWhitespace(s,startPos)
    assert(startPos<=stringLen, 'JSON string ended unexpectedly while scanning object.')
    local curChar = string.sub(s,startPos,startPos)
    if (curChar=='}') then
      return object,startPos+1
    end
    if (curChar==',') then
      startPos = decode_scanWhitespace(s,startPos+1)
    end
    assert(startPos<=stringLen, 'JSON string ended unexpectedly scanning object.')
    -- Scan the key
    key, startPos = json.decode(s,startPos)
    assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
    startPos = decode_scanWhitespace(s,startPos)
    assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
    assert(string.sub(s,startPos,startPos)==':','JSON object key-value assignment mal-formed at ' .. startPos)
    startPos = decode_scanWhitespace(s,startPos+1)
    assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key)
    value, startPos = json.decode(s,startPos)
    object[key]=value
  until false	-- infinite loop while key-value pairs are found
end
-- START SoniEx2
-- Initialize some things used by decode_scanString
-- You know, for efficiency
local escapeSequences = {
  ["\\t"] = "\t",
  ["\\f"] = "\f",
  ["\\r"] = "\r",
  ["\\n"] = "\n",
  ["\\b"] = "\b"
}
setmetatable(escapeSequences, {__index = function(t,k)
  -- skip "\" aka strip escape
  return string.sub(k,2)
end})
-- END SoniEx2
--- Scans a JSON string from the opening inverted comma or single quote to the
-- end of the string.
-- Returns the string extracted as a Lua string,
-- and the position of the next non-string character
-- (after the closing inverted comma or single quote).
-- @param s The string being scanned.
-- @param startPos The starting position of the scan.
-- @return string, int The extracted string as a Lua string, and the next character to parse.
function decode_scanString(s,startPos)
  assert(startPos, 'decode_scanString(..) called without start position')
  local startChar = string.sub(s,startPos,startPos)
  -- START SoniEx2
  -- PS: I don't think single quotes are valid JSON
  assert(startChar == [["]] or startChar == [[']],'decode_scanString called for a non-string')
  --assert(startPos, "String decoding failed: missing closing " .. startChar .. " for string at position " .. oldStart)
  local t = {}
  local i,j = startPos,startPos
  while string.find(s, startChar, j+1) ~= j+1 do
    local oldj = j
    i,j = string.find(s, "\\.", j+1)
    local x,y = string.find(s, startChar, oldj+1)
    if not i or x < i then
      i,j = x,y-1
    end
    table.insert(t, string.sub(s, oldj+1, i-1))
    if string.sub(s, i, j) == "\\u" then
      local a = string.sub(s,j+1,j+4)
      j = j + 4
      local n = tonumber(a, 16)
      assert(n, "String decoding failed: bad Unicode escape " .. a .. " at position " .. i .. " : " .. j)
      -- math.floor(x/2^y) == lazy right shift
      -- a % 2^b == bitwise_and(a, (2^b)-1)
      -- 64 = 2^6
      -- 4096 = 2^12 (or 2^6 * 2^6)
      local x
      if n < 0x80 then
        x = string.char(n % 0x80)
      elseif n < 0x800 then
        -- [110x xxxx] [10xx xxxx]
        x = string.char(0xC0 + (math.floor(n/64) % 0x20), 0x80 + (n % 0x40))
      else
        -- [1110 xxxx] [10xx xxxx] [10xx xxxx]
        x = string.char(0xE0 + (math.floor(n/4096) % 0x10), 0x80 + (math.floor(n/64) % 0x40), 0x80 + (n % 0x40))
      end
      table.insert(t, x)
    else
      table.insert(t, escapeSequences[string.sub(s, i, j)])
    end
  end
  table.insert(t,string.sub(j, j+1))
  assert(string.find(s, startChar, j+1), "String decoding failed: missing closing " .. startChar .. " at position " .. j .. "(for string at position " .. startPos .. ")")
  return table.concat(t,""), j+2
  -- END SoniEx2
end
--- Scans a JSON string skipping all whitespace from the current start position.
-- Returns the position of the first non-whitespace character, or nil if the whole end of string is reached.
-- @param s The string being scanned
-- @param startPos The starting position where we should begin removing whitespace.
-- @return int The first position where non-whitespace was encountered, or string.len(s)+1 if the end of string
-- was reached.
function decode_scanWhitespace(s,startPos)
  local whitespace=" \n\r\t"
  local stringLen = string.len(s)
  while ( string.find(whitespace, string.sub(s,startPos,startPos), 1, true)  and startPos <= stringLen) do
    startPos = startPos + 1
  end
  return startPos
end
--- Encodes a string to be JSON-compatible.
-- This just involves back-quoting inverted commas, back-quotes and newlines, I think ;-)
-- @param s The string to return as a JSON encoded (i.e. backquoted string)
-- @return The string appropriately escaped.
local escapeList = {
    ['"']  = '\\"',
    ['\\'] = '\\\\',
    ['/']  = '\\/', 
    ['\b'] = '\\b',
    ['\f'] = '\\f',
    ['\n'] = '\\n',
    ['\r'] = '\\r',
    ['\t'] = '\\t'
}
function json_private.encodeString(s)
 local s = tostring(s)
 return s:gsub(".", function(c) return escapeList[c] end) -- SoniEx2: 5.0 compat
end
-- Determines whether the given Lua type is an array or a table / dictionary.
-- We consider any table an array if it has indexes 1..n for its n items, and no
-- other data in the table.
-- I think this method is currently a little 'flaky', but can't think of a good way around it yet...
-- @param t The table to evaluate as an array
-- @return boolean, number True if the table can be represented as an array, false otherwise. If true,
-- the second returned value is the maximum
-- number of indexed elements in the array. 
function isArray(t)
  -- Next we count all the elements, ensuring that any non-indexed elements are not-encodable 
  -- (with the possible exception of 'n')
  local maxIndex = 0
  for k,v in pairs(t) do
    if (type(k)=='number' and math.floor(k)==k and 1<=k) then	-- k,v is an indexed pair
      if (not isEncodable(v)) then return false end	-- All array elements must be encodable
      maxIndex = math.max(maxIndex,k)
    else
      if (k=='n') then
        if v ~= table.getn(t) then return false end  -- False if n does not hold the number of elements
      else -- Else of (k=='n')
        if isEncodable(v) then return false end
      end  -- End of (k~='n')
    end -- End of k,v not an indexed pair
  end  -- End of loop across all pairs
  return true, maxIndex
end
--- Determines whether the given Lua object / table / variable can be JSON encoded. The only
-- types that are JSON encodable are: string, boolean, number, nil, table and json.null.
-- In this implementation, all other types are ignored.
-- @param o The object to examine.
-- @return boolean True if the object should be JSON encoded, false if it should be ignored.
function isEncodable(o)
  local t = type(o)
  return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null) 
end
local jsonLibrary = json
-- local jsonLibrary = require "share/lua/5.4/json" ^^^^^
function LogPrint(message)
  try(1, function()
    if Logger ~= nil and Logger.LogDebug ~= nil then
      Logger.LogDebug(Logger, message)
    end
  end, function(e)
    try(1, function()
      print("Exception in execute log: " .. e)
    end, function(e)
    end)
  end)
end
function try(count, func, catch_f)
  local status, result = pcall(func, count) -- if status is false result contains the exception message
  if not status then
    return catch_f(result)
  end
  return result
end
function decodeDecideJson(updateInformation)
  return try(1, function()
    if updateInformation == nil then
      LogPrint("updateInformation not set")
      return nil
    end
    if updateInformation["decision_json"] == nil then
      LogPrint("decideJsonContent not set")
      return nil
    end
    if updateInformation["decision_json"] == "" then
      LogPrint("decideJsonContent is empty")
      return nil
    end
    LogPrint("decideJsonContent: " .. updateInformation["decision_json"])
    decideJsonTable = jsonLibrary.decode(updateInformation["decision_json"])
    if decideJsonTable == nil then
      LogPrint("decideJsonTable not set")
      return nil
    end
    return decideJsonTable
  end, function(e)
    LogPrint("Exception in decode decision json: " .. e)
    return nil
  end)
end
function toString(nValue)
  if nValue == nil then
    return "[nil]"
  end
  if type(nValue) == "string" then
    return "\"" .. nValue .. "\""
  end
  if type(nValue) == "boolean" then
    return nValue and "[true]" or "[false]"
  end
  if type(nValue) ~= "number" then
    return "[invalid type:" .. type(nValue) .. "]"
  end
  return "type=" .. type(nValue) .. " " .. nValue
end
function parseVersion(versionStr)
  versionTable = {0, 0, 0, 0}
  if versionStr == nil then
    LogPrint("Parse version called with nil value")
    return versionTable
  end
  if type(versionStr) ~= "string" then
    LogPrint("Parse version called with " .. type(versionStr)
               .. " type but string is expected")
    return versionTable
  end
  i = 1
  for match in (versionStr .. "."):gmatch("(.-)%.") do
    versionTable[i] = tonumber(match)
    i = i + 1
  end
  return versionTable
end
function compareVersions(versionStr1, versionStr2)
  version1 = parseVersion(versionStr1)
  version2 = parseVersion(versionStr2)
  if #version1 < #version2 then
    return -1
  end
  if #version1 > #version2 then
    return 1
  end
  local maxVersionLength = 4
  for i = 1, maxVersionLength do
    val1 = #version1 >= i and version1[i] or 0
    val2 = #version2 >= i and version2[i] or 0
    if val1 < val2 then
      return -1
    end
    if val1 > val2 then
      return 1
    end
  end
  return 0
end
function useVersion(data, version_name, skip_update)
  if data == nil then
    LogPrint("updateInformation not set")
    return nil
  end
  if data[version_name] == nil then
    LogPrint("No " .. version_name .. " is defined")
    return nil
  end
  -- check for string type
  if data[version_name] == "" then
    LogPrint(version_name .. " is empty")
    return nil
  end
  LogPrint("Use " .. version_name .. " = " .. data[version_name]
             .. ", skip_update = " .. (skip_update and "true" or "false"))
  return {
    ["skip_update"] = skip_update,
    ["update_version"] = data[version_name]
  }
end
function isExternalUpdateServer(updateInformation)
  server = updateInformation["server"]
  if server == nil or type(server) ~= "string" then
    return false
  end
  if string.find(string.lower(server), "://[%w-]+%.avira%.com/") ~= nil
    or string.find(string.lower(server),
                   "://[%w-]+%.fakeserver%.build%.avira%.org/") ~= nil then
    return false
  end
  return true
end
function shouldDowngradeBeSkippedBecauseCurrentVersionIsGreaterOrEqualRolloutVersion(
  updateInformation)
  currentVersion = updateInformation["current_version"]
  stableVersion = decideJsonTable["stable_version"]
  rolloutVersion = decideJsonTable["rollout_version"]
  if compareVersions(currentVersion, rolloutVersion) == 0
    and compareVersions(stableVersion, rolloutVersion) ~= 0 then
    LogPrint("Decision logic should be skipped. Current version "
               .. currentVersion .. " is greater or equal to rollout version "
               .. rolloutVersion)
    return true
  end
  return false
end
function shouldDowngradeBeSkippedBecauseCurrentVersionIsBetweenStableAndRolloutVersion(
  updateInformation)
  currentVersion = updateInformation["current_version"]
  stableVersion = decideJsonTable["stable_version"]
  rolloutVersion = decideJsonTable["rollout_version"]
  if compareVersions(currentVersion, rolloutVersion) ~= 0
    and compareVersions(currentVersion, stableVersion) > 0 then
    LogPrint("Prevent downgrade. Current version " .. currentVersion
               .. " is greater or equal to stable version " .. stableVersion
               .. " but smaller than rollout version " .. rolloutVersion)
    return true
  end
  return false
end
function shouldMirrorDowngradeBeSkipped(updateInformation)
  currentVersion = updateInformation["current_version"]
  stableVersion = decideJsonTable["stable_version"]
  rolloutVersion = decideJsonTable["rollout_version"]
  if isExternalUpdateServer(updateInformation)
    and compareVersions(currentVersion, rolloutVersion) > 0 then
    LogPrint("Mirror server update should be skipped. Current version "
               .. currentVersion .. " is greater or equal to rollout version "
               .. rolloutVersion)
    return true
  end
  return false
end
function shouldStableBeUsed(updateInformation)
  stableVersion = decideJsonTable["stable_version"]
  rolloutVersion = decideJsonTable["rollout_version"]
  if compareVersions(stableVersion, rolloutVersion) == 0 then
    LogPrint("Rollout and stable are equal. Current version " .. currentVersion
               .. ". Returning " .. stableVersion)
    return true
  end
  return false
end
function decideUpdate(updateInformation, rolloutFunction)
  decideJsonTable = decodeDecideJson(updateInformation)
  if decideJsonTable == nil then
    LogPrint("No decision json is available")
    return useVersion(updateInformation, "current_version", false)
  end
  if shouldDowngradeBeSkippedBecauseCurrentVersionIsGreaterOrEqualRolloutVersion(
    updateInformation) then
    -- allow self-repair by returning current_version
    return useVersion(updateInformation, "current_version", false)
  end
  if shouldMirrorDowngradeBeSkipped(updateInformation) then
    return useVersion(updateInformation, "current_version", true)
  end
  if extensionFunction ~= nil then
    extensionFunctionDecision = extensionFunction(updateInformation,
                                                  decideJsonTable)
    if extensionFunctionDecision ~= nil then
      return extensionFunctionDecision
    end
  end
  if shouldStableBeUsed(updateInformation) then
    return useVersion(decideJsonTable, "stable_version", false)
  end
  if rolloutFunction ~= nil then
    rolloutFunctionDecision =
      rolloutFunction(updateInformation, decideJsonTable)
    if rolloutFunctionDecision ~= nil then
      return rolloutFunctionDecision
    end
  end
  if decideJsonTable["stable_version"] == nil then
    LogPrint("No stable version is available")
    return useVersion(updateInformation, "current_version", false)
  end
  -- check for string type
  if decideJsonTable["stable_version"] == "" then
    LogPrint("Stable version is empty")
    return useVersion(updateInformation, "current_version", false)
  end
  if shouldDowngradeBeSkippedBecauseCurrentVersionIsBetweenStableAndRolloutVersion(
    updateInformation) then
    -- allow self-repair by returning current_version
    return useVersion(updateInformation, "current_version", false)
  end
  LogPrint("Use stable version")
  return useVersion(decideJsonTable, "stable_version", false)
end
function decideUseNewestVersion(updateInformation,
                                decideJsonTable)
  if decideJsonTable["rollout_percentage"] == nil then
    LogPrint("Rollout percentage is not available")
    return nil
  end
  if updateInformation["rollout_random_value"] == nil then
    LogPrint("Rollout value is not available")
    return nil
  end
  LogPrint("rollout_random_value: " .. updateInformation["rollout_random_value"])
  LogPrint("rollout_percentage: " .. decideJsonTable["rollout_percentage"])
  LogPrint("current_version: " .. toString(updateInformation["current_version"]))
  LogPrint("api_version: " .. toString(updateInformation["api_version"]))
  if updateInformation["rollout_random_value"]
    >= decideJsonTable["rollout_percentage"] then
    LogPrint("The rollout version should not be used")
    return nil
  end
  LogPrint("Use rollout version: " .. decideJsonTable["rollout_version"])
  return useVersion(decideJsonTable, "rollout_version", false)
end
if sdkSpecificRolloutFunctionV_1_0 == nil then
  sdkSpecificRolloutFunctionV_1_0 = decideUseNewestVersion
end
function decide(updateInformation)
  return try(1, function()
    return decideUpdate(updateInformation, sdkSpecificRolloutFunctionV_1_0)
  end, function(e)
    return try(1, function()
      LogPrint("Exception in execute decideUpdate: " .. e)
      return decideUpdate(updateInformation, nil)
    end, function(e1)
      LogPrint("Exception in execute fallback decideUpdate: " .. e1)
      return nil
    end)
  end)
end
