-- subversionVersionChecker
-----------------------------------
-- CREATED:
-- 05.04.2008
--
-- DESCRIPTION:
--             this behavior just checks the URL, which it finds in its member comments field
--             and compares it with its own content.
--             If the new number it gets from the URL is higher, it just displays an alert.
--
-- REQUIRES:
-- (Prerequisites) NONE
--
-- USAGE:
--             The URL entered in the comments field should yield an integer
--             The way I use it is, that on subversion commit (using my "HandlerMenu" tool)
--             the field in this movie gets updated as well as the value retrieved by the online URL.
--             I use a perl script myself, which can update the revision number on the one hand
--             and which can be queried to return the revision number on the other hand
--             So all I do is using my "HandlerMenu" tool to commit and automatically refresh
--             the revision number and the URL in the comments field
--             When working with external castlibs, each castlib can have its own version field
--             in that case this behavior gets the highest number on beginsprite
-----------------------------------

property pNetId, pSecurityTimeout

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on beginSprite me
  myMem = sprite(me.spritenum).member
  myrev = mGetMyRevNumber(me, myMem)
  if ilk(myrev) <> #integer then myrev = 0
  repeat with n = the number of castlibs down to 1
    themember = member("subversion_version_field", n)
    if ilk(themember) = #member then
      theRev = mGetMyRevNumber(me, themember)
      if integerP(theRev) then
        if theRev > myrev then myrev = theRev
      end if
    end if
  end repeat
  if [#field, #text].getPos(myMem) > 0 then myMem.text = "r" & myrev
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mouseEnter me
  cursor 280
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mouseLeave me
  cursor -1
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mouseUp me
  mCheckSubversionRevision me
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mCheckSubversionRevision me
  mem = sprite(me.spritenum).member
  theUrl = string(mem.comments.line[1])
  if length(theUrl) < 1 then exit
  pNetID = getnettext(theUrl)
  pSecurityTimeout = the milliseconds + 20000
  (the actorlist).add(me)
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mGetMyRevNumber me, theMember
  if [#field, #text].getPos(theMember.type) < 1 then return void
  myrev = theMember.text
  if myrev starts "r" then delete char 1 of myrev
  return integer(myrev)
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on stepframe me
  if netdone(pNetId) then
    (the actorlist).deleteOne(me)
    if neterror(pNetId) = "OK" then
      theResult = string(nettextresult(pNetId))
      if length(theResult) then
        theResult = integer(theResult)
        if ilk(theResult) <> #integer then exit
        myrev = mGetMyRevNumber(me, sprite(me.spritenum).member)
        if ilk(myrev) <> #integer then myrev = 0
        if myrev < theResult then
          alert "There is a newer version available!" & RETURN & "Current version:" && theResult & RETURN & "Your version:" && myrev
        else
          alert "Your version" && myrev && "is up to date."
        end if
      end if
    end if
  else
    if the milliseconds > pSecurityTimeout then (the actorlist).deleteOne(me)
  end if
end

