-- svn_Utilities
-----------------------------------
-- PROPERTIES:
--!memberProperties: [#name: "svn_Utilities", #scripttype: #parent, #scriptSyntax: #lingo, #comments: "~/Documents/Scripts/lingo/commonMovieScript.ls"]
--
-- DESCRIPTION:
--               This script externalizes all functions which are related to the use of subversion with director
--               
--
-- REQUIRES:
--               script "alexUtilities" -> ancestor -> basic function provider
--               most functionality is provided through the shell xtra and buddyApi xtra
--               although it should fail gracefully, if those are not present
--               not much will work without them
--               It also requires the svn command line client to be installed on the machine
--
-- USAGE:
--               This script is instantiated and used in the "tell the stage" block and one of the functions
--               which are defined in script "OSCmenu_Utilities" and which was selected by the user
--               will be executed in "stage scope". That means, that references are made form within the stage context
--               e.g. member("foo") will refer to a member in one of the stages castlibs
--               sprite(x) will refere to sprite(x) in the stages score and so on
--
--               Please see the descriptions of many of those handlers at: http://www.farbflash.org/trac/HandlerMenu/wiki/utilityHandlers
-----------------------------------

property ancestor

on new me
  ancestor = new(script "alexUtilities")
  return me
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
on _______________SVN_SCRIPT_TEXT me
end
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


on mCompareCurrentScriptToWorkingCopy me, convertLineBreaksToUnix
  
  if not(the platform contains "mac") then
    
    --    alert "This function only works with BBDiff, a command line tool provided by the OSX app BBEdit. I am sure, something similar exists for windows too, but you'll need to change this script yourself for windows"
    --    exit
    
    bbdiffpath = mGetSVNDiffBinaryPath(me)
    
    if length(bbdiffpath) < 1 then exit
    
    isMac = 0
  else
    isMac = 1
    bbdiffpath = mFindUnixAppInPath(me, "bbdiff")
    if length(bbdiffpath) < 1 then
      alert "This function only works with BBDiff, a command line tool provided by BBEdit. Consider installing BBEdits command line tools, it is really helpful."
      exit
    end if
    
  end if
  
  
  if  mCheckForXtra(me, "ff_shell") = 0 then
    if mCheckForXtra(me, "Shell") = 0 then
      mShellXtraMissing me
      exit
    end if
  end if
  
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      memref = member(mem,cl)
      
      memtype = memref.type
      if [#script, #field, #text].getPos(memtype) > 0 then
        
        case memtype of
            
            -----------------------
          #script:
            ext = ".ls"
            st = memref.scripttext
            
            -----------------------
          #field:
            ext = ".txt"
            st = memref.text
            
            -----------------------
          #text:
            ext = ".html"
            st = memref.html
            
            -----------------------
          otherwise:
            ext = ".txt"
            st = memref.text
            
        end case
        
        thePath = mFindWorkingCopyCounterpart(me, memref, ext)
        
        if length(thePath) < 1 then
          
          put "Corresponding file for member " & memref.name && "(" & memref & ") was not found"
          
          
        else
          
          tempSrcHFSPath = mGetTempFilePath(me, "temp_SVN_Diff_file1.ls")
          
          if isMac then
            tempSrcPath = mConvertHfs2unix(me, tempSrcHFSPath)
          else
            tempSrcPath = tempSrcHFSPath
          end if
          
          ------------------------------------------ trac subversion support works better with unix linebreaks...
          if voidP(convertLineBreaksToUnix) then convertLineBreaksToUnix = 1
          if convertLineBreaksToUnix = "" then convertLineBreaksToUnix = 1
          
          if convertLineBreaksToUnix = 1 then
            
            if mCheckForXtra(me, "Pregex") = 1 then
              
              scrTextLi = [st]
              pregex_replace(scrTextLi, "\x0D\x0A?", "g", "\x0A")
              st = scrTextLi[1]
              
            else
              
              fndStr = numToChar(13) & numToChar(10)
              offs = offset(fndStr, st)
              repeat while offs > 0
                delete char offs of st
                offs = offset(fndStr, st)
              end repeat
              
              fndStr = numToChar(13)
              offs = offset(fndStr, st)
              repeat while offs > 0
                put numToChar(10) into char offs of st
                offs = offset(fndStr, st)
              end repeat
              
            end if
          end if
          -----------------------------------------
          
          theResult = mSaveTextToTempFile(me, st, tempSrcHFSPath)
          ---------------------
          
          
          if theResult <> 0 then
            
            
            
            
            if isMac then
              
              comm = mConvertHFS2Unix(me, thePath)
              
              theResult = mDoShellCmd(me, QUOTE & bbdiffpath & QUOTE & " --ignore-curly-quotes --ignore-spaces --wait --resume " &QUOTE& tempSrcPath &QUOTE&&QUOTE& comm &QUOTE && "2>&1", RETURN, 0, 1)
              
              -- since we used the --wait and the --resume switch, we will only come to this line AFTER the diff process in bbedit is finished
              
              if count(theResult) > 0 then
                if theResult[1] contains "no such file or directory" then
                  put theResult[1]
                else
                  put "No differences found for member: " & memref.name && "(" & memref & ")"
                end if
                writeBack = 0
                
              else
                
                -------- now write the results of the BBEdit diff back into the script members
                scrText = mGetTextFromFile(me, tempSrcHFSPath)
                
                writeBack = 1
                
                
              end if
              
              
              ---------------------- windows
            else
              
              
              theResult = mDoShellCmd(me, QUOTE & bbdiffpath & QUOTE && QUOTE & tempSrcPath &QUOTE&&QUOTE& thePath &QUOTE, RETURN, 0, 0, 0)
              
              -------- now write the results of the BBEdit diff back into the script members
              scrText = mGetTextFromFile(me, tempSrcHFSPath)
              
              writeBack = 1
            end if
            
            
            if writeBack = 1 then
              
              if convertLineBreaksToUnix = 1 then
                ------------------------------------------ trac subversion support works better with unix linebreaks...
                if mCheckForXtra(me, "Pregex") = 1 then
                  
                  scrTextLi = [scrText]
                  ----------- always convert to mac linebreaks as director internally uses mac linebreaks
                  pregex_replace(scrTextLi, "\x0D?\x0A", "g", "\x0D")
                  scrText = scrTextLi[1]
                  
                else
                  
                  ----------- always convert to mac linebreaks as director internally uses mac linebreaks
                  -- if the platform contains "mac" then
                  
                  -- first convert windows to mac
                  fndStr = numToChar(13) & numToChar(10)
                  offs = offset(fndStr, scrText)
                  repeat while offs > 0
                    put numToChar(13) into char offs to offs+1 of scrText
                    offs = offset(fndStr, scrText)
                  end repeat
                  
                  -- now convert unix to mac
                  fndStr = numToChar(10)
                  offs = offset(fndStr, scrText)
                  repeat while offs > 0
                    put numToChar(13) into char offs of scrText
                    offs = offset(fndStr, scrText)
                  end repeat
                  
                  
                end if
                -----------------------------------------
              end if
              
              
              
              case memtype of
                  
                  -----------------------
                #script:
                  memref.scripttext = scrText
                  
                  -----------------------
                #field:
                  memref.text = scrText
                  
                  -----------------------
                #text:
                  memref.html = scrText
                  
                  -----------------------
                otherwise:
                  memref.text = scrText
                  
              end case
              
            end if
            
          end if
          
          -- exit -- we only proceed one script member, WHY ???
          
          
        end if
        
      end if -- corresponding file not found for this member
      
    end repeat
  end repeat
  
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- this can be used to open the corresponding script in bbedit and svn compare it to head

on mExportCurrentScriptToWorkingCopyAndOpen me, convertLineBreaksToUnix
  
  mExportScriptsToFolder me, 1, 1, convertLineBreaksToUnix
  
  cl = the activecastlib
  sel = the selection of castlib cl
  
  repeat with sub in sel
    repeat with mem = sub[1] to sub[2]
      memref = member(mem,cl)	
      memtype = memref.type
      if [#script, #field, #text].getPos(memtype) > 0 then
        
        case memtype of
            
            -----------------------
          #script:
            ext = ".ls"
            st = memref.scripttext
            
            -----------------------
          #field:
            ext = ".txt"
            st = memref.text
            
            -----------------------
          #text:
            ext = ".html"
            st = memref.html
            
            -----------------------
          otherwise:
            ext = ".txt"
            st = memref.text
            
        end case
        
        
        thePath = mFindWorkingCopyCounterpart(me, memref, ext)
        
        if length(thePath) < 1 then
          alert "Corresponding member was not found"
          exit  
        end if
        
        ------------------- export will now be done in "mExportScriptsToFolder"
        --        st = memref.scripttext
        --        mSaveToTextFile me, st, thePath
        
        if the platform contains "mac" then
          if (mCheckForXtra(me, "budapi") + (mCheckForXtra(me, "Shell") + mCheckForXtra(me, "ff_shell"))) > 1 then
            
            bbeditBinary = mFindUnixAppInPath(me, "bbedit")
            if length(bbeditBinary) > 0 then
              mDoShellCmd(me, QUOTE & bbeditBinary & QUOTE && QUOTE & mConvertHFS2Unix(me, thePath) & QUOTE)
              exit
            end if
            
          end if
        end if
        
        
        bbdiffpath = mGetSVNDiffBinaryPath(me)
        if length(bbdiffpath) then
          mDoShellCmd(me, QUOTE & bbdiffpath & QUOTE && QUOTE & thePath & QUOTE, RETURN, 0, 0, 0)
        else
          open thePath
        end if
        
        exit
        
      end if
      
    end repeat
  end repeat
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mFindWorkingCopyCounterpart me, memref, file_extension
  
  file_extension = string(file_extension)
  if length(file_extension) < 1 then file_extension = ".ls"
  
  workingCopies = value(getPref("svn_workingCopies_paths.txt"))
  if ilk(workingCopies) <> #proplist then workingCopies = [:]
  
  theFolder = mGetWorkingCopyPath(me, the moviepath & the moviename, 0, workingCopies, "No working copy is specified for this movies scripttext. Please select a working copy for the scripttext now.")
  --  put "mFindWorkingCopyCounterpart: theFolder:" && theFolder
  
  if length(theFolder) < 1 then return ""
  
  
  m = memref.castlibnum
  
  delim = the last char of the applicationpath
  olddelim = the itemdelimiter
  the itemdelimiter = delim
  theFoldername = theFolder
  if the last char of theFoldername = delim then delete the last char of theFoldername
  else put delim after theFolder
  theFoldername = the last item of theFoldername
  the itemdelimiter = olddelim
  
  infolist = 0
  
  if theFoldername <> "castlib" & m then
    
    newPath = theFolder & "castlib" & m & delim
    
    if baFolderExists(newPath) = 0 then
      
      infolistPath = theFolder & "memberInfo.xml"
      
      infolist = mReadXML_2_List(me, infolistPath)
      
      if ilk(infolist) <> #proplist then
        
        alert "The specified folder doesn't seem to contain info for this castlib: file memberInfo.xml missing."
        return ""
        
      else
        
        if count(infolist) < 1 then
          
          alert "The specified folder doesn't seem to contain info for this castlib: file memberInfo.xml missing."
          return ""
          
        else
          theFolder = newPath
        end if
        
      end if
      
    else
      
      theFolder = newPath
      
    end if
    
  end if
  
  
  if infolist = 0 then
    
    infolistPath = theFolder & "memberInfo.xml"
    
    infolist = mReadXML_2_List(me, infolistPath)
    
    if ilk(infolist) <> #proplist then
      
      alert "The specified folder doesn't seem to contain info for this castlib: file memberInfo.xml missing."
      return ""
      
    end if
    
  end if
  
  
  cnt = count(infolist)
  
  repeat with l = 1 to cnt
    li = infolist[l]
    
    if li.getaprop(#memberName) = memref.name then
      
      fname = string(infolist[l].getaprop(#fname))
      if length(fname) < 1 then fname = string(infolist.getpropat(l))
      fpath = theFolder & fname & file_extension
      
      if length(baShortFilename(fpath)) > 0 then return fpath
      
    end if
  end repeat
  
  
  return ""
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-- export all scripts into a folder (I use this to compare to versions of a movie, open movie 1, export all scripts, open movie 2 and export all scripts and then compare with bbdiff both folders)

on mExportScriptsToFolder me, skipIcons, selectionOnly, convertLineBreaksToUnix, chooseNewPath
  
  mExportAllScriptsToDiskWithFolder me, skipIcons, selectionOnly, convertLineBreaksToUnix, void, chooseNewPath
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mExportAllScriptsToDiskWithFolder me, skipIcons, selectionOnly, convertLineBreaksToUnix, theFolder, chooseNewPath
  
  
  theFolder = string(theFolder)
  if length(theFolder) < 1 then
    
    if chooseNewPath <> 1 then
      workingCopies = value(getPref("svn_workingCopies_paths.txt"))
      if ilk(workingCopies) <> #proplist then workingCopies = [:]
      
      theFolder = mGetWorkingCopyPath(me, the moviepath & the moviename, chooseNewPath, workingCopies, "No working copy is specified for this movies scripttext. Please select a working copy for the scripttext now.")
    end if
    
    if length(theFolder) < 1 then
      
      theFolder = mGetFolderPathFromUser(me)
      
    end if
    
  end if
  
  if length(theFolder) < 1 then exit
  
  if the last char of theFolder <> the last char of the moviepath then put the last char of the moviepath after theFolder
  
  if ilk(selectionOnly) = #castlib then
    firstCastNum = selectionOnly.number
    lastCastNum = firstCastNum
    selectionOnly = 0
    
  else
    if selectionOnly = 1 then
      cl = the activecastlib
      sel = the selection of castlib cl
      
      firstCastNum = cl
      lastCastNum = cl    
      
    else
      firstCastNum = 1
      lastCastNum = the number of castlibs
    end if
  end if
  
  
  DirectImageInstance = 0
  if skipIcons <> 1 then
    if mCheckForXtra(me, "SharpExport") = 1 then
      SharpExportInst = new(xtra "SharpExport")
    else
      skipIcons = 1
    end if
  end if
  
  
  isMac = (the platform contains "mac") -- we need the differentiation because of the dreaded 31 char limit
  
  -----------------------
  resetUNames = 0
  if isMac then
    if baSysFolder("prefs") starts "/" then
      baReturnUnixNames(0)
      resetUNames = 1
    end if
  end if
  -----------------------
  
  doit = 0
  
  statusSwitched = mSwitchToStatusMode(me, 1)
  
  repeat with m = firstCastNum to lastCastNum
    
    listOfAllFiles = ["memberinfo"]
    
    thisFolder = theFolder & "castlib" & m
    if baFolderExists(thisFolder) <> 1 then
      err = baCreateFolder(thisFolder)
      if err <> 1 then
        alert "Failed to create directory:" && thisFolder
        if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
        DirectImageInstance = 0
        exit
      end if
    end if
    
    thisFolder = thisFolder & the last char of the applicationpath
    
    
    doit = 0
    infolistPath = thisFolder & "memberInfo.xml"
    infolist = mReadXML_2_List(me, infolistPath)
    if ilk(infolist) <> #proplist then infolist = [:]
    
    
    if selectionOnly = 1 then
      sel = the selection of castlib m
    else
      cnt = the number of members of castlib m
      sel = [[1, cnt]]
    end if
    
    repeat with sub in sel
      repeat with n = sub[1] to sub[2]   
        
        memref = member(n, m)
        
        memtype = memref.type
        
        
        convertLineBreaksToUnixT = 0
        
        -- if [#script].getPos(memtype) > 0 then
        if [#script, #field, #text].getPos(memtype) > 0 then
          
          case memtype of
              
              -----------------------
            #script:
              ext = ".ls"
              scrText = memref.scripttext
              scrtype = memref.scripttype
              -----------------------
            #field:
              ext = ".txt"
              scrText = memref.text
              scrtype = #field
              -----------------------
            #text:
              ext = ".html"
              scrText = memref.html
              convertLineBreaksToUnixT = 0
              scrtype = #text
              -----------------------
            otherwise:
              ext = ".txt"
              scrText = memref.text
              scrtype = #field
          end case
          
          
          
          memname = memref.name
          
          
          mDisplayWaitStatusText me, "Processing member:" && memname
          
          
          -- scrText = memref.scripttext
          
          
          ------------- once I thought about adding the support for metadata directly to the script
          ------------- metadata like thumbnail path, script type etc. 
          ------------- but currently I stick to the memberInfo.xml approach to store the metadata
          ------------- so the following is disabled ... meanwhile...
          ------------- the "memberInfo.xml" approach is more svn like anyway
          
          
          --          ------------------------------ check for existing meta data
          --          
          --          export_meta_data = scrText&""
          --          offs = offset("-- -- -- export_meta_data", export_meta_data)
          --          if offs > 0 then
          --            delete char 1 to offs + 24 of export_meta_data
          --            offs = offset("-- -- -- // export_meta_data", export_meta_data)
          --            if offs > 0 then
          --              delete char offs to length(export_meta_data) of export_meta_data
          --            end if
          --            numlines = export_meta_data.line.count
          --            repeat with l = 1 to numlines
          --              thisLine = export_meta_data.line[l]
          --              
          --            end repeat
          --          end if
          --          
          --          ------------------------------
          
          
          theDefaultName = memname
          
          doit = 1
          
          if length(theDefaultName) < 1 then
            theDefaultName =  "member" & n & "_" & m
            memref.name = theDefaultName
            -- alert memref && "has no membername! Member without names will cause trouble when re-importing the scripts."
            put "renamed member" && memref && "to member(" & QUOTE & theDefaultName & QUOTE & ") as member without names are not supported"
            
          else
            
            if memref <> member(theDefaultName, m) then
              put "Member" && memref && "is duplicate. Member with duplicate names are not supported. (Can't have the same file twice in one folder.)"
              next repeat
            end if
            
          end if
          
          
          
          -- if offset(".", theDefaultName) < 1 then put ".ls" after theDefaultName
          
          theDefaultName = mStripFunnyCharsFromFileName(me, theDefaultName)
          
          
          origname = theDefaultName&""
          if length(theDefaultName) > 25 and isMac then
            
            ms = string(the milliseconds)
            mlen = length(ms)
            ms = char mlen - 6 to mlen of ms
            
            theDefaultName = char 1 to 18 of theDefaultName
            theDefaultName = theDefaultName & ms -- trim filenames as the mac version of fileio wants to deal with 31 chars
            
          end if
          
          
          
          ------------------------------------------ trac subversion support works better with unix linebreaks...
          if voidP(convertLineBreaksToUnix) then convertLineBreaksToUnix = 1
          if convertLineBreaksToUnix = "" then convertLineBreaksToUnix = 1
          
          if convertLineBreaksToUnix = 1 then
            if convertLineBreaksToUnixT <> 1 then
              if mCheckForXtra(me, "Pregex") = 1 then
                
                scrTextLi = [scrText]
                pregex_replace(scrTextLi, "\x0D\x0A?", "g", "\x0A")
                scrText = scrTextLi[1]
                
              else
                
                fndStr = numToChar(13) & numToChar(10)
                offs = offset(fndStr, scrText)
                repeat while offs > 0
                  delete char offs of scrText
                  offs = offset(fndStr, scrText)
                end repeat
                
                fndStr = numToChar(13)
                offs = offset(fndStr, scrText)
                repeat while offs > 0
                  put numToChar(10) into char offs of scrText
                  offs = offset(fndStr, scrText)
                end repeat
              end if 
            end if
          end if
          -----------------------------------------
          
          if theDefaultName <> origname then
            alterText = ""
            snam = baShortFileName(thisFolder & origname & ext)
            if length(snam) > 0 then
              -- alterText = mGetTextFromFile(me, thisFolder & origname & ext)
              alterText = mGetTextFromFile(me, snam)
            else
              alterText = mGetTextFromFile(me, thisFolder & origname & ext)
            end if
          else
            alterText = mGetTextFromFile(me, thisFolder & theDefaultName & ext)
          end if
          
          listOfAllFiles.add(mLowerCase(me, origname))
          
          if alterText <> scrText then
            
            --------------------- compare without whitespaces:
            noChanges = (mRemoveWhiteSpaces(me, scrText&"") = mRemoveWhiteSpaces(me, alterText&""))
            
            
            if noChanges = 0 then
              
              mSaveToTextFile me, scrText, thisFolder & theDefaultName & ext
              
              
              
              bmexp = 0
              if skipIcons <> 1 then
                
                thumbmember = new(#bitmap)
                thumbmember.picture = memref.thumbnail
                
                if thumbmember.width > 0 then
                  bmexp = 1
                  if baFileExists(thisFolder & theDefaultName & ".png") then baDeleteFile(thisFolder & theDefaultName & ".png")
                  
                  if objectP(DirectImageInstance) then
                    
                    DirectImageInstance.imageLoadFromMember(thumbmember)
                    DirectImageInstance.imageSaveToFile(thisFolder & theDefaultName & ".png", 100, 1,1)
                    
                  else if objectP(SharpExportInst) then
                    mExportBM me, thumbmember, thisFolder & theDefaultName & ".png", "png", 100, SharpExportInst
                    
                  end if
                  
                end if
                
                thumbmember.erase()
                
              end if
              
              
              if theDefaultName <> origname then
                
                snam = baShortFileName(thisFolder & origname & ext)
                if length(snam) > 0 then baDeleteFile(snam)
                erg = baRenameFile(thisFolder & theDefaultName & ext, thisFolder & origname & ext)
                if erg <> 1 then put "Couldn't rename file " & thisFolder & theDefaultName & ext & " to " & thisFolder & origname & ext
                if bmexp = 1 then
                  snam = baShortFileName(thisFolder & origname & ".png")
                  if length(snam) > 0 then baDeleteFile(snam)
                  erg = baRenameFile(thisFolder & theDefaultName & ".png", thisFolder & origname & ".png")
                  if erg <> 1 then put "Couldn't rename file " & thisFolder & theDefaultName & ".png to " & thisFolder & origname & ".png"
                end if
                
              end if
              
              if origname <> memname then
                put "Renamed member" && memref && "to" && origname & ". Scriptmember names must not be emoty or contain illegal characters (SPACE, /, :, \, &)"
              end if
              
            end if
          end if
          
          
          infolist.setaprop(symbol(origname), [#scripttype: scrtype, #comments: memref.comments, #membername: memname, #fname: origname])
          -- we need to store origname as string additionally to symbol, as the symbol may change the case (although only ufs is case sensitive
          -- I am more comfortable with not relying on case insensitivity of filesystems)
          
        end if
        
        
      end repeat
    end repeat
    
    if doit = 1 then
      --      mSaveList_2_XML me, infolist, infolistPath
      
      mRefreshMemberInfoList me, thisFolder, m, infolist
    end if
    
    
    -----------------------
    if isMac then baReturnUnixNames(1)
    -----------------------
    
    flist = baFileList(thisFolder, "*.*")
    
    
    --    put "flist =" && flist
    --    put "listOfAllFiles =" && listOfAllFiles
    repeat with thisFile in flist
      split = mSplitPath(me, thisFile)
      if listOfAllFiles.getPos(mLowerCase(me, split[#basename])) < 1 then
        if isMac then
          erg = baDeleteFile(mConvertHFS2Unix(me, thisFolder & thisFile))
        else
          erg = baDeleteFile(thisFolder & thisFile)
        end if
        if erg = 1 then
          put "Deleted file" && thisFolder & thisFile
        else
          put "Unable to delete file" && thisFolder & thisFile
        end if
      end if
    end repeat
    
    -----------------------
    if isMac then baReturnUnixNames(0)
    -----------------------
    
    
  end repeat
  
  if resetUNames = 1 then baReturnUnixNames(1)
  
  DirectImageInstance = 0
  SharpExportInst = 0
  
  if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mRefreshMemberInfoList me, thisFolder, theCastlibNumber, infolist
  
  infolistPath = thisFolder & "memberInfo.xml"
  
  if ilk(infolist) <> #proplist then infolist = mReadXML_2_List(me, infolistPath)
  if ilk(infolist) <> #proplist then infolist = [:]
  newInfolist = [:]
  
  isMac = (the platform contains "mac")
  
  cnt = the number of members of castlib theCastlibNumber
  
  repeat with n = 1 to cnt 
    
    memref = member(n, theCastlibNumber)
    
    memtype = memref.type
    
    -- if [#script].getPos(memtype) > 0 then
    if [#script, #field, #text].getPos(memtype) > 0 then
      
      case memtype of
          
          -----------------------
        #script:
          ext = ".ls"
          scrtype = memref.scripttype
          -----------------------
        #field:
          ext = ".txt"
          scrtype = #field
          -----------------------
        #text:
          ext = ".html"
          scrtype = #text
          -----------------------
        otherwise:
          ext = ".txt"
          scrtype = #field
          
      end case
      
      memname = memref.name
      
      theDefaultName = memname
      
      doit = 1
      
      if length(theDefaultName) < 1 then
        
        put "Member" && memref && "has no name, therefore skipped"
        
        --      if length(theDefaultName) < 1 then
        --        theDefaultName =  "member" & n & "_" & theCastlibNumber
        --        alert memref && "has no membername! Member without names will cause trouble when re-importing the scripts." 
        --      end if
        
      else
        
        if memref <> member(theDefaultName, theCastlibNumber) then
          put "Member" && memref && "is duplicate. Member with duplicate names are not supported"
          
        else
          theDefaultName = mStripFunnyCharsFromFileName(me, theDefaultName)
          
          
          origname = theDefaultName&""
          
          oldlist = infolist.getaprop(symbol(origname))
          if not(listP(oldlist)) then
            oldlist = [:]
            fname = origname
            snam = baShortFileName(thisFolder & origname & ext)
            if length(snam) < 1 then
              put "Something seems wrong here. baShortFileName for " & thisFolder & origname & ext && "returned false. The file doesn't seem to exist!"
            end if
            oldlist.setaprop(#fname, origname)
          end if
          oldlist.setaprop(#scripttype, scrtype)
          oldlist.setaprop(#comments, memref.comments)
          oldlist.setaprop(#membername, memname)
          
          
          newInfolist.setaprop(symbol(origname), oldlist)
          -- we need to store origname as string additionally to symbol, as the symbol may change the case (although only ufs is case sensitive
          -- I am more comfortable with not relying on case insensitivity of filesystems)
          
        end if
      end if
    end if
    
  end repeat
  
  if doit = 1 then mSaveList_2_XML me, newInfolist, infolistPath
  
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Update_And_Import_ScriptsFromFolder me, chooseNewPath, selectionOnly
  
  if selectionOnly = 1 then
    mUpdateSelectedScriptsFromFolder me, chooseNewPath
    exit
  end if
  
  
  workingCopies = value(getPref("svn_workingCopies_paths.txt"))
  if ilk(workingCopies) <> #proplist then workingCopies = [:]
  
  if chooseNewPath = 1 then
    thePath = ""
  else
    thePath = string(mGetWorkingCopyPath(me, the moviepath & the moviename, chooseNewPath, workingCopies, "No working copy is specified for this movies scripttext. Please select a working copy for the scripttext now."))
  end if
  
  
  if length(thePath) < 1 then thePath = mGetFolderPathFromUser(me)
  
  if length(thePath) < 1 then exit
  
  if mCheckForXtra(me, "budapi") then
    
    statusSwitched = mSwitchToStatusMode(me, 1)
    
    if baFileExists(thePath & "memberInfo.xml") then
      mUpdateScriptCastlibFromFolder me, thePath
    else
      folderlist = bafolderlist(thePath)
      delim = the last char of the applicationpath
      cnt = count(folderlist)
      repeat with n = 1 to cnt
        if folderlist[n] starts "castlib" then
          mUpdateScriptCastlibFromFolder me, thePath & folderlist[n] & delim
        end if
      end repeat
    end if
    
    if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
    
  else
    
    alert "Hey, buddyAPI not found! Please install this must-have xtra. And try again."
    
  end if
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mUpdateScriptCastlibFromFolder me, thePath
  
  infoList = mReadXML_2_List(me, thePath & "memberInfo.xml")
  
  if ilk(infoList) <> #proplist then
    alert thePath && "memberInfo.xml has no data! Can't update folder:" & thePath
    return false
  end if
  
  olddelim = the itemdelimiter
  delim = the last char of the applicationpath
  the itemdelimiter = delim
  fd = thePath
  if the last char of fd = delim then delete the last char of fd
  fd = the last item of fd
  the itemdelimiter = olddelim
  
  if not(fd starts "castlib") then
    alert thePath && "does not seem to be valid, as its name doesn't start with " & QUOTE & "castlib" & QUOTE
    return false
  end if
  
  delete char 1 to 7 of fd
  fd = integer(word 1 of fd)
  
  if not(integerP(fd)) then
    alert thePath && "does not seem to be valid, as its name doesn't yield a castlib number:" && fd
    return false
  end if
  
  
  if fd > the number of castlibs then
    alert thePath && "points to a non-existant castlib (castlib" && fd & "). Please create the castlib before improting the scripts."
    return false
  end if
  
  cnt = count(infoList)
  repeat with n = 1 to cnt
    thisMemberInfo = infoList[n]
    fname = string(thisMemberInfo.getaprop(#fname))
    if length(fname) < 1 then fname = string(infoList.getPropAt(n))
    fpath = thePath & fname
    
    scrtype = thisMemberInfo.getaprop(#scripttype)
    case scrtype of
      #field: ext = ".txt"
      #text: ext = ".html"
      otherwise: ext = ".ls"
    end case
    
    mImportScriptWithThumbnail me, fpath & ext, fd, 1, thisMemberInfo
    
  end repeat
  
  
  return true
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mUpdateSelectedScriptsFromFolder me, chooseNewPath
  
  workingCopies = value(getPref("svn_workingCopies_paths.txt"))
  if ilk(workingCopies) <> #proplist then workingCopies = [:]
  
  if chooseNewPath = 1 then
    thePath = ""
  else
    thePath = string(mGetWorkingCopyPath(me, the moviepath & the moviename, chooseNewPath, workingCopies, "No working copy is specified for this movies scripttext. Please select a working copy for the scripttext now."))
  end if
  
  
  if length(thePath) < 1 then thePath = mGetFolderPathFromUser(me)
  
  if length(thePath) < 1 then exit
  
  cl = the activecastlib
  
  olddelim = the itemdelimiter
  the itemdelimiter = the last char of the applicationpath
  fname = thePath
  delete the last item of fname
  fname = the last item of fname
  the itemdelimiter = olddelim
  
  infolist = 0
  
  if fname <> "castlib" & the number of castlib cl then
    
    newPath = thePath & "castlib" & the number of castlib cl & the last char of the applicationpath
    
    if baFolderExists(newPath) = 0 then
      
      infolistPath = thePath & "memberInfo.xml"
      
      infolist = mReadXML_2_List(me, infolistPath)
      
      if ilk(infolist) <> #proplist then
        
        alert "The specified folder doesn't seem to contain info for this castlib: file memberInfo.xml missing."
        return 0
        
      else
        
        thePath = newPath
        
      end if
      
    else
      
      thePath = newPath
      
    end if
    
  end if
  
  
  if infolist = 0 then
    
    infolistPath = thePath & "memberInfo.xml"
    infolist = mReadXML_2_List(me, infolistPath)
    
    if ilk(infolist) <> #proplist then
      
      alert "The specified folder (" & infolistPath & ") doesn't seem to contain info for this castlib: file memberInfo.xml missing."
      return 0
      
    end if
    
  end if
  
  
  if count(infolist) < 1 then
    
    alert "The specified folder (" & infolistPath & ") doesn't seem to contain info for this castlib: file memberInfo.xml missing."
    return 0
    
  end if
  
  
  cl = the activecastlib
  sel = the selection of castlib cl
  anz = sel.count
  cnt = count(infolist)
  
  --  put "mUpdateSelectedScriptsFromFolder: infolist = " & infolist
  
  repeat with n = 1 to anz
    repeat with m = sel[n][1] to sel[n][2]
      memref = member(m, cl)
      dertyp = memref.type
      
      if [#script, #text, #field].getPos(dertyp) > 0 then
        
        found = 0
        repeat with l = 1 to cnt
          li = infolist[l]
          if li.getaprop(#memberName) = memref.name then
            
            fname = string(infolist[l].getaprop(#fname))
            if length(fname) < 1 then fname = string(infolist.getpropat(l))
            
            case dertyp of
              #text: ext = ".html"
              #field: ext = ".txt"
              otherwise: ext = ".ls"    
            end case
            
            fpath = thePath & fname & ext
            
            found = 1
            
            if baFileExists(fpath) then
              mImportScriptWithThumbnail me, fpath, the number of castlib cl, 1, li
              
              put "mUpdateSelectedScriptsFromFolder: imported scripttext of" && memref && "from" && fpath
              
            else
              put "mUpdateSelectedScriptsFromFolder: file doesn't exist ?? fpath = " & fpath
              
            end if
            
          end if
        end repeat
        
        
        if found = 0 then
          put "Info for member" && memref && "was not found in memberinfo.xml"
        end if
        
      end if
    end repeat
  end repeat
  
  
  
  return 1
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mImportScriptWithThumbnail me, thePath, castlibNumber, dontWarn, infolist
  
  -- <castlibNumber> is optional and will replace an existing script, if it exists
  
  castlibNumber = integer(castlibNumber)
  if ilk(castlibNumber, #number) <> 1 then castlibNumber = 0
  if castlibNumber > the number of castlibs or castlibNumber = 0 then
    cl = the activecastlib
    castlibNumber = the number of castlib cl
  end if
  
  
  thePath = string(thePath)
  if length(thePath) < 1 then thePath = mGetFilePathFromUser(me)
  
  if length(thePath) < 1 then exit
  
  if mCheckForXtra(me, "budapi") then
    buddyApiPresent = 1
    spath = bashortfilename(thePath)
    if length(spath) < 1 then
      alert "file does not exist:" && spath
      exit
    end if
  else
    buddyApiPresent = 0
    spath = thePath
  end if
  
  
  mDisplayWaitStatusText me, "Importing file:" && spath
  
  
  scrtext = mGetTextFromFile(me, spath)
  
  if length(scrtext) < 1 then
    alert "file not found:" && spath
    return 0
  end if
  
  oldd = the itemdelimiter
  the itemdelimiter = the last char of the applicationpath
  fname = the last item of thePath
  delete the last item of thePath
  put the itemdelimiter after thePath
  
  splitpath = mSplitPath(me, fname)
  extension = splitpath[#extension]
  --  extension = ""
  --  repeat while length(fname)
  --    put the last char of fname before extension
  --    delete the last char of fname
  --    if char 1 of extension = "." then exit repeat
  --  end repeat
  --  if char 1 of extension <> "." then
  --    put extension after fname
  --    extension = ""
  --  end if
  
  if ilk(infolist) <> #proplist then
    
    offs = offset("--!memberProperties:", scrtext)
    
    if offs > 0 then
      infolist = scrtext.char[offs + 20 .. length(scrtext)]
      offs = offset("]", infolist)
      if offs > 0 then
        infolist = value(infolist.char[1 .. offs])
      end if
    end if
    
    if ilk(infolist) <> #proplist then
      infolistPath = thePath & "memberInfo.xml"
      infolist = mReadXML_2_List(me, infolistPath)
      if ilk(infolist) <> #proplist then infolist = [:]
      
      infolist = infolist.getaprop(symbol(fname))
      if ilk(infolist) <> #proplist then infolist = [:]
    end if
    
  end if
  
  scrtype = infolist.getaprop(#scripttype)
  if ilk(scrtype) <> #symbol then
    ident = "-- scripttype = "
    scrtype = offset(ident, scrtext)
    if scrtype > 0 then
      scrtype = scrtext.word[scrtext.char[1 .. scrtype+length(ident) - 1].word.count + 1]
      scrtype = value(scrtype)
      if ilk(scrtype) <> #symbol then scrtype = 0
    end if
  end if
  
  
  cmts = infolist.getaprop(#comments)
  if voidP(cmts) then
    ident = "-- comments = "
    cmts = offset(ident, scrtext)
    if cmts > 0 then
      cmts = scrtext.line[scrtext.char[1 .. cmts+length(ident) - 1].line.count]
      delete char 1 to length(ident) - 1 of cmts
    end if
  end if
  
  
  theIcon = thePath & splitpath[#basename] & ".png"
  
  --  if length(fname & ".png") > 31 and (the platform contains "mac") then
  --    
  --    unixname = baUnixName(theIcon)
  --    if length(unixname) > 0 then
  --      tempSrcPath = shell_cmd_list("mktemp -t temp_icon_file.XXXX")
  --      tempSrcPath = tempSrcPath[1] & ".png"
  --      shell_cmd_list("cp " & QUOTE & unixname & QUOTE && QUOTE& tempSrcPath &QUOTE)
  --      theIcon = baHFSName(tempSrcPath)
  --    else
  --      theIcon = ""
  --    end if
  --    
  --  end if
  
  if buddyApiPresent = 1 then theIcon = bashortfilename(theIcon)
  
  
  fio = new(xtra "fileio")
  if length(theIcon) > 0 then
    fio.openFile(theIcon, 1)
    if fio.status() = 0 then
      i = new(#bitmap)
      i.importFileInto(theIcon)
      theIcon = i.picture
      fio.closeFile()
    end if
  end if
  
  
  membername = infolist.getaprop(#membername)
  if voidP(membername) then
    scrname = fname
  else
    scrname = string(membername)
  end if
  
  
  case scrtype of
    #field, #text: memtype = scrtype
    otherwise: memtype = #script
  end case
  
  
  askreplace = 1
  scr = member(scrname, castlibNumber)
  if voidP(scr) then
    scr = new(memtype, castlib(castlibNumber))
    askreplace = 0
  end if
  if scr.type <> #script then
    scr = new(memtype, castlib(castlibNumber))
    askreplace = 0
  end if
  
  if dontWarn = 1 then askreplace = 0
  
  if askreplace = 1 then
    if buddyApiPresent = 1 then
      if baMsgBox("Replace member" && scrname, "Replace", "Yesno", "Warning", 1) = "No" then
        if ilk(i) = #member then i.erase()
        fio = 0
        return 0
      end if
    end if
  end if
  
  scr.name = scrname
  
  
  
  ------------------------------------------ trac subversion support works better with unix linebreaks...
  if mCheckForXtra(me, "Pregex") = 1 then
    
    scrTextLi = [scrText]
    if the platform contains "mac" then
      pregex_replace(scrTextLi, "\x0D?\x0A", "g", "\x0D")
    else
      pregex_replace(scrTextLi, "\x0D([^\x0A])", "g", "\x0D\x0A\1")
      pregex_replace(scrTextLi, "([^\x0D])\x0A", "g", "\1\x0D\x0A")
    end if
    scrText = scrTextLi[1]
    
  else
    
    if the platform contains "mac" then
      
      -- first convert windows to mac
      fndStr = numToChar(13) & numToChar(10)
      offs = offset(fndStr, scrText)
      repeat while offs > 0
        put numToChar(13) into char offs to offs+1 of scrText
        offs = offset(fndStr, scrText)
      end repeat
      
      -- now convert unix to mac
      fndStr = numToChar(10)
      offs = offset(fndStr, scrText)
      repeat while offs > 0
        put numToChar(13) into char offs of scrText
        offs = offset(fndStr, scrText)
      end repeat
      
    else
      
      
      --      fndStr = numToChar(13) & numToChar(10)
      --      offs = offset(fndStr, scrText)
      --      repeat while offs > 0
      --        delete char offs of scrText
      --        offs = offset(fndStr, scrText)
      --      end repeat
      
      fndStr = numToChar(13) & numToChar(10)
      offs = offset(fndStr, scrText)
      repeat while offs > 0
        put numToChar(13) into char offs+1 of scrText
        offs = offset(fndStr, scrText)
      end repeat
      
      
      fndStr = numToChar(10)
      offs = offset(fndStr, scrText)
      repeat while offs > 0
        put numToChar(13) into char offs of scrText
        offs = offset(fndStr, scrText)
      end repeat
      
    end if
    
  end if
  -----------------------------------------
  
  
  
  case scrtype of
    #field: scr.text = scrtext
    #text: scr.html = scrtext
    otherwise: scr.scripttext = scrtext
  end case
  
  
  if [#movie, #parent, #score].getPos(scrtype) > 0 then scr.scripttype = scrtype
  if cmts <> 0 then scr.comments = cmts
  if ilk(theIcon) = #picture then scr.thumbnail = theIcon
  
  if ilk(i) = #member then i.erase()
  fio = 0
  
  
  mDisplayWaitStatusText me, "Imported file:" && spath && "into member" && scr
  
  
  return 1
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
on _______________SVN_FIELD_TEXT me
end
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mExportTextAndFieldsToFolder me, skipIcons_optional, selectionOnly_optional, convertLineBreaksToUnix, chooseNewPath_optional
  mExportScriptsToFolder me, skipIcons_optional, selectionOnly_optional, convertLineBreaksToUnix, chooseNewPath_optional
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mImportTextAndFields me, thePath_optional, castlibNumber_optional, dontWarn_optional, infolist_optional
  mImportScriptWithThumbnail me, thePath_optional, castlibNumber_optional, dontWarn_optional, infolist_optional
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
on _______________SVN_SUPPORT me
end
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Update_Working_Copy_ScriptText me, revisionNumber, chooseNewPath, selectionList, dontShowAlert
  
  workingCopies = value(getPref("svn_workingCopies_paths.txt"))
  if ilk(workingCopies) <> #proplist then workingCopies = [:]
  
  svnBinary = mGetSVNBinaryPath(me, workingCopies)
  if length(svnBinary) < 1 then exit
  
  workingFolder = mGetWorkingCopyPath(me, the moviepath & the moviename, chooseNewPath, workingCopies, "No working copy is specified for this movies scripttext. Please select a working copy for the scripttext now.")
  if length(workingFolder) < 1 then exit
  
  if the platform contains "mac" then
    svnBinary = mConvertHFS2Unix(me, svnBinary)
    workingFolder = mConvertHFS2Unix(me, workingFolder)
  else
    if the last char of workingFolder = the last char of the applicationpath then delete the last char of workingFolder
  end if
  
  statusSwitched = mSwitchToStatusMode(me, 1)
  mDisplayWaitStatusText me, "Updating folder:" && workingFolder
  
  
  revisionNumber = getValidRevNumber(me, revisionNumber)
  
  castlibList = getCastlibList(me, selectionList)
  
  if count(castlibList) < 1 then
    theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "update" && QUOTE & workingFolder & QUOTE, RETURN, 0, 1)
    
  else
    
    theresult = []
    if the platform contains "mac" then pdelim = "/"
    else pdelim = the last char of the moviepath
    repeat with num in castlibList
      
      thePath = workingFolder & pdelim & "castlib" & num & pdelim
      
      ret = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "update" && "-r" && revisionNumber && QUOTE & thePath & QUOTE, RETURN, 0, 1)
      if count(ret) > 0 then theresult.add(ret[1])
      
    end repeat
    
  end if
  
  if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
  
  if count(theresult) > 0 then
    repeat with n = 1 to count(theresult)
      put theresult[n]
    end repeat
    if dontShowAlert <> 1 then
      alert "Done, see the results of the svn operation in the message window."
    end if
  else
    if dontShowAlert <> 1 then
      alert "svn application did not respond, something seems to have failed. :-("
    end if
  end if
  
  setPref("svn_workingCopies_paths.txt", string(workingCopies))
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on getValidRevNumber me, revisionNumber
  if integerP(integer(revisionNumber)) then return string(revisionNumber)
  
  revisionNumber = string(revisionNumber)
  if length(revisionNumber) < 1 then
    revisionNumber = "HEAD"
  else
    if char 1 of revisionNumber = "'" then delete char 1 of revisionNumber
    if the last char of revisionNumber = "'" then delete the last char of revisionNumber
  end if
  
  return QUOTE & revisionNumber & QUOTE
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Delete_Selected_Scripts me, chooseNewPath
  
  
  workingCopies = value(getPref("svn_workingCopies_paths.txt"))
  if ilk(workingCopies) <> #proplist then workingCopies = [:]
  
  svnBinary = mGetSVNBinaryPath(me, workingCopies)
  if length(svnBinary) < 1 then exit
  
  workingFolder = mGetWorkingCopyPath(me, the moviepath & the moviename, chooseNewPath, workingCopies, "No working copy is specified for this movies scripttext. Please select a working copy for the scripttext now.")
  if length(workingFolder) < 1 then exit
  
  
  if the platform contains "mac" then
    isMac = 1
    svnBinary = mConvertHFS2Unix(me, svnBinary)
    
  else
    isMac = 0
    
  end if
  
  
  thePath = workingFolder
  
  cl = the activecastlib
  
  pathdelim = the last char of the applicationpath
  olddelim = the itemdelimiter
  the itemdelimiter = pathdelim
  fname = thePath
  delete the last item of fname
  fname = the last item of fname
  the itemdelimiter = olddelim
  
  infolist = 0
  
  if fname <> "castlib" & the number of castlib cl then
    
    newPath = thePath & "castlib" & the number of castlib cl
    
    if baFolderExists(newPath) = 0 then
      
      infolistPath = thePath & pathdelim & "memberInfo.xml"
      
      infolist = mReadXML_2_List(me, infolistPath)
      
      if ilk(infolist) <> #proplist then
        
        alert "The specified folder doesn't seem to contain info for this castlib: file memberInfo.xml missing."
        return 0
        
      else
        
        thePath = newPath & pathdelim
        
      end if
      
    else
      
      thePath = newPath & pathdelim
      
    end if
  end if
  
  
  if infolist = 0 then
    
    infolistPath = thePath & "memberInfo.xml"
    infolist = mReadXML_2_List(me, infolistPath)
    
    if ilk(infolist) <> #proplist then
      
      alert "The specified folder doesn't seem to contain info for this castlib: file memberInfo.xml missing."
      return 0
      
    end if
    
  end if
  
  if ilk(infolist) <> #proplist then
    
    alert "The specified folder doesn't seem to contain info for this castlib: file memberInfo.xml missing."
    return 0
    
  end if
  
  if count(infolist) < 1 then
    
    alert "The specified folder doesn't seem to contain info for this castlib: file memberInfo.xml missing."
    return 0
    
  end if
  
  doCommit = 0
  saveRequired = 0
  
  cl = the activecastlib
  sel = the selection of castlib cl
  anz = sel.count
  cnt = count(infolist)
  repeat with n = 1 to anz
    repeat with m = sel[n][1] to sel[n][2]
      memref = member(m, cl)
      dertyp = memref.type
      
      put "processing member" && memref
      
      if [#script, #field, #text].getPos(dertyp) then
        
        putstr = ""
        repeat with l = 1 to cnt
          li = infolist[l]
          
          -- put "listref  =" && li
          
          if li.getaprop(#memberName) = memref.name then
            
            theFilename = string(li.getaprop(#fname))
            if length(theFilename) < 1 then theFilename = string(infolist.getpropat(l))
            
            scrtype = li.getaprop(#scripttype)
            case scrtype of
              #field: ext = ".txt"
              #text: ext = ".html"
              otherwise: ext = ".ls"
            end case
            
            fpath = thePath & theFilename & ext
            
            put "checking for file" && fpath
            
            if isMac = 1 then
              sname = baShortFileName(fpath)
            else
              sname = fpath
            end if
            
            if baFileExists(fpath) then
              
              if isMac = 1 then
                ufpath =  mConvertHFS2Unix(me, fpath)
              else
                ufpath =  fpath
              end if
              
              theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "delete" && QUOTE & ufpath & QUOTE, RETURN, 0, 1)
              if count(theresult) < 1 then
                put "failed to svn delete member " & memref & " no result from svn?!"
              else
                put theresult[1]
                doCommit = 1
              end if
              
              infolist.deleteAt(l)
              memref.erase()
              saveRequired = 1
              put "Erase member:" && memref.name
              
              putstr = ""
              
              exit repeat
              
            else
              
              putstr = "No corresponding file o disk was found for member" && memref.name && "(" & memref & ") erase the member manually:" & RETURN & memref & ".erase()" & RETURN
              
            end if
            
          end if
          
        end repeat
        
        if length(putstr) then put putstr
        
      end if
    end repeat
  end repeat
  
  
  if saveRequired = 1 then
    
    if (baMsgBoxEx("The movie has been changed. Save changes to this movie?", "Save changes?", "Save", "Don't save", "", "Question", 1, "left", "Arial", 14, 4, -2, -2) = "Save") then
      saveMovie()
    end if
    
    mRefreshMemberInfoList me, thePath, castlib(cl).number
    
  end if
  
  
  if doCommit = 1 then
    
    msg = baPrompt("Commit message", "Enter commit message", "Commitmessage", 0, -2, -2)
    
    if length(msg) < 1 then
      alert "You must enter a message for the commit action."
      exit
    end if
    
    if isMac = 1 then
      workingFolder = mConvertHFS2Unix(me, workingFolder)
    else
      if the last char of workingFolder = the last char of the applicationpath then delete the last char of workingFolder
    end if
    
    theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "ci --message" && QUOTE & msg & QUOTE && QUOTE & workingFolder & QUOTE, RETURN, 0, 1)
    
    if count(theresult) > 0 then
      put theresult[1]
      alert "Done, see the results of the svn operation in the message window."
    else
      alert "svn application did not respond, something seems to have failed. :-("
    end if
    
  else
    
    alert "Nothing has been committed!"
    
  end if
  
  return 1
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Commit_CurrentMovie_Binaries_AND_Scripttext me, chooseNewPath, commitMsg
  
  commitMsg = string(commitMsg)
  if length(commitMsg) < 1 then
    commitMsg = baPrompt("Commit message", "Enter commit message", "Commitmessage", 0, -2, -2)
    
    if length(commitMsg) < 1 then
      alert "You must enter a message for the commit action."
      exit
    end if
  end if
  
  
  theResult = mSVN_Commit_CurrentScripts_ScriptText(me, 0, chooseNewPath, commitMsg, 1, void, 2)
  
  if theResult = 0 then exit
  
  theResult = mSVN_Commit_CurrentMovie_Binaries(me, chooseNewPath, commitMsg, 1)
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mGetCurrentRevision me, svnBinary, WorkingFolder
  
  versionNumber = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && QUOTE & WorkingFolder & QUOTE, RETURN, 0, 1)
  if count(versionNumber) < 1 then return void
  versionNumber = versionNumber[1]
  
  offs = offset(":", versionNumber)
  if offs > 0 then delete char 1 to offs of versionNumber
  
  if length(versionNumber) < 1 then return void
  
  versionNumberInt = integer(versionNumber)
  repeat while not(integerP(versionNumberInt))
    if length(versionNumber) < 1 then return void
    delete the last char of versionNumber
    versionNumberInt = integer(versionNumber)
  end repeat
  
  return versionNumberInt
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Commit_SelectedCastlib_AND_Scripttext me, chooseNewPath, commitMsg, selectionList
  
  commitMsg = string(commitMsg)
  if length(commitMsg) < 1 then
    commitMsg = baPrompt("Commit message", "Enter commit message", "Commitmessage", 0, -2, -2)
    
    if length(commitMsg) < 1 then
      alert "You must enter a message for the commit action."
      exit
    end if
  end if
  
  
  theResult = mSVN_Commit_CurrentScripts_ScriptText(me, castlib(the activecastlib), chooseNewPath, commitMsg, 1, selectionList, 2)
  
  if theResult = 0 then exit
  
  theResult = mSVN_Commit_SelectedCastlib(me, chooseNewPath, commitMsg, 1, 0, selectionList)
  
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Commit_SelectedCastlib me, chooseNewPath, commitMsg, dontSave, dontShowAlert, selectionList
  
  
  castlibList = getCastlibList(me, selectionList, the activecastlib)
  
  mpname = the moviepath & the moviename
  isInternal = 0
  repeat with cl in castlibList
    if castlib(cl).filename = mpname then
      isInternal = 1
      exit repeat
    end if
  end repeat
  
  
  if count(castlibList) < 1 then
    alert "No castlib selected. The parameter didn't yield a valid list. Make sure, that you provide a linear list with integers, specifying a number of a castlib each."
    return 0
  end if
  
  
  
  put "Committing castlibs:" && castlibList
  
  
  workingCopies = value(getPref("svn_wCopies_paths_Binary.txt"))
  if ilk(workingCopies) <> #proplist then workingCopies = [:]
  
  svnBinary = mGetSVNBinaryPath(me, workingCopies)
  if length(svnBinary) < 1 then return 0
  
  workingFolder = mGetWorkingCopyPath(me, the moviepath & the moviename, chooseNewPath, workingCopies, "No working copy is specified for this movies binaries (director files). Please select a working copy of the director files.")
  if length(workingFolder) < 1 then return 0
  
  
  ---------------------------------------
  msg = string(commitMsg)
  if length(msg) < 1 then
    msg = baPrompt("Commit message", "Enter commit message", "Commitmessage", 0, -2, -2)
    
    if length(msg) < 1 then
      alert "You must enter a message for the commit action."
      return 0
    end if
  end if
  ---------------------------------------
  
  
  ---------------------------------------
  if dontSave <> 1 then
    -- this flag is only for the case, we come here from the combined commit call "mSVN_Commit_CurrentMovie_Binaries_AND_Scripttext"
    -- as the movie was already saved in "mSVN_Commit_CurrentScripts_ScriptText"
    
    if workingCopies.getaprop(#dontWarnWhenSaving) <> 1 then
      answer = baMsgBoxEx("The castlib will be saved before committing.", "Save castlib?", "Save", "Cancel", "Always save", "Question", 1, "left", "Arial", 14, 4, -2, -2)
      if answer = "Cancel" then return 0
      if answer = "Always save" then workingCopies.setaprop(#dontWarnWhenSaving, 1)
    end if
    
    --    if the moviepath & the moviename = castlib(actCL).filename then
    --      savemovie()
    --    else
    --      castlib(actCL).save()
    --   end if
    
    
    -------------------- get version number:
    if the platform contains "mac" then
      svnBin = mConvertHFS2Unix(me, svnBinary)
      workFolder = mConvertHFS2Unix(me, workingFolder)
    else
      workFolder = workingFolder
      if the last char of workFolder = the last char of the applicationpath then delete the last char of workFolder
    end if
    versionNumber = mGetCurrentRevision(me, svnBin & "version", workFolder)
    
    if integerP(versionNumber) then
      
      versionNumber = versionNumber + 2 -- the version will raise
      
      VersionCheckUrl = string(mGetVersionCheckUrl(me, the moviepath & the moviename, workingCopies, "Select an URL to use for version checking."))
      if length(VersionCheckUrl) > 0 then
        mscript = member("aleXtrasMovieScript")
        ident = ""
        if mGetMemType(me, mscript) = #script then ident = string(xscr().mGetThisMovieName())
        if length(ident) < 1 then ident = the moviename
        
        thePassword = ""
        placeholder = "%%password:"
        offs = offset(placeholder, VersionCheckUrl)
        if offs > 0 then
          thePassword = VersionCheckUrl.char[offs+length(placeholder) .. length(VersionCheckUrl)]
          thePassword = word 1 to thePassword.word.count of thePassword
          delete char offs to length(VersionCheckUrl) of VersionCheckUrl
        end if
        
        VersionCheckUrl = word 1 to VersionCheckUrl.word.count of VersionCheckUrl & "?movieName=" & ident
        
      end if
      
      updateurl = 0
      repeat with cl in castlibList
        versmem = member("subversion_version_field", cl)
        if [#field, #text].getPos(mGetMemType(me, versmem)) > 0 then
          if updateurl = 0 then updateurl = 1
          curr = versmem.text
          if char 1 of curr = "r" then delete char 1 of curr
          curr = integer(curr)
          if not(integerP(curr)) then curr = 0
          if curr < versionNumber then
            versmem.text = "r" & versionNumber
            if length(VersionCheckUrl) > 0 then
              versmem.comments = VersionCheckUrl
            end if
          else
            updateurl = 2
          end if
        end if
      end repeat
      
      if length(VersionCheckUrl) > 0 then
        if updateurl = 1 then
          if length(thePassword) then
            id = getnettext(VersionCheckUrl & "&version=" & thePassword & versionNumber)
          end if
        end if
      end if
      
    end if
    --------------------- // version number
    
    
    if isInternal = 1 then
      savemovie()
    else
      repeat with cl in castlibList
        castlib(cl).save()
      end repeat
    end if
    
  end if
  ----------------------------------------
  
  
  statusSwitched = mSwitchToStatusMode(me, 1)
  mDisplayWaitStatusText me, "Committing castlibs:" && castlibList
  
  
  
  --  fname = castlib(actCL).filename
  
  
  ---------------------------------------
  if workingFolder <> the moviepath then
    
    mp = the moviepath
    mplen = length(mp)
    olddelim = the itemdelimiter
    the itemdelimiter = mp
    
    
    castlibListCnt = count(castlibList)
    repeat with r = 1 to castlibListCnt
      cl = castlibList[r]
      
      fname = castlib(cl).filename
      
      if length(fname) > 0 then
        
        offs = offset(mp, fname)
        if offs <> 1 then
          alert "Castlib" && castlib(cl) && "is not within your current moviepath. This is not supported."
          if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
          return 0
        end if
        
        relpath = fname
        delete char 1 to mplen of relpath
        
        newpath = workingFolder & relpath
        newpathDir = newpath
        delete the last item of newpathDir
        if baFolderExists(newpathDir) <> 1 then baCreateFolder(newpathDir)
        baCopyFileProgress(fname, newpath, "Always", "Copying castlib " & cl & " to working folder", "", 33)
        
        castlibList[r] = [cl, fname, newpath, newpathDir]
        
      else
        
        -- must be something REALLY wrong, as the filename should ALWAYS be of any length (internal castlibs )
        if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
        return 0
        
      end if
      
    end repeat
    
    
    the itemdelimiter = olddelim
    
  else
    
    castlibListCnt = count(castlibList)
    repeat with r = 1 to castlibListCnt
      cl = castlibList[r]
      fname = castlib(cl).filename
      newpath = fname
      castlibList[r] = [cl, fname, fname]
    end repeat
    
  end if
  ---------------------------------------
  
  
  ---------------------------------------
  if the platform contains "mac" then
    svnBinary = mConvertHFS2Unix(me, svnBinary)
    workingFolder = mConvertHFS2Unix(me, workingFolder)
    
    castlibListCnt = count(castlibList)
    repeat with r = 1 to castlibListCnt
      castlibList[r][3] = mConvertHFS2Unix(me, castlibList[r][3])
    end repeat
    
  else
    if the last char of workingFolder = the last char of the applicationpath then delete the last char of workingFolder
  end if
  ---------------------------------------
  
  
  isChanged = 0
  castlibListCnt = count(castlibList)
  repeat with r = 1 to castlibListCnt
    --    cl = castlibList[r]
    --  repeat with cl in castlibList
    ---------------------------------------
    
    mDisplayWaitStatusText me, "SVN update of:" && castlibList[r][3]
    
    theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "update" && QUOTE & castlibList[r][3] & QUOTE, RETURN, 0, 1)
    
    if count(theresult) < 1 then
      theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "update" && QUOTE & castlibList[r][3] & QUOTE && "2>&1", RETURN, 0, 1)
      if count(theresult) < 1 then
        theError = theresult[1]
      else
        theError = "svn did not respond at all."
      end if
      alert "Can't proceed svn error while trying to update:" && theError
      if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
      return 0  
    end if
    ---------------------------------------
    
    
    
    theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "status" && QUOTE & castlibList[r][3] & QUOTE, RETURN, 0, 1)
    
    cnt = count(theresult)
    if cnt > 0 then
      isChanged = 1
      --      alert "Apparently there are no changes to the most recent version in the svn repository: svn status is empty"
      --      return 0
    end if
    
  end repeat
  
  if isChanged = 0 then
    alert "Apparently there are no changes to the most recent version in the svn repository: svn status is empty"
    if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
    return 0 
  end if
  
  
  pathStr = ""
  castlibListCnt = count(castlibList)
  repeat with r = 1 to castlibListCnt
    put " " & QUOTE & castlibList[r][3] & QUOTE after pathStr
  end repeat
  
  
  mDisplayWaitStatusText me, "SVN commit for:" && pathStr
  
  theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "ci --message" && QUOTE & msg & QUOTE & pathStr, RETURN, 0, 1)
  
  
  ---------------------------------------
  if count(theresult) > 0 then
    theResult = theresult[1]
    
    put theResult
    if dontShowAlert <> 1 then alert "Done, see the results of the svn operation in the message window."
    
  else
    if dontShowAlert <> 1 then alert "svn application did not respond, something seems to have failed. :-("
  end if
  ---------------------------------------
  
  setPref("svn_wCopies_paths_Binary.txt", string(workingCopies))
  
  if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
  
  return 1
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Commit_CurrentMovie_Binaries me, chooseNewPath, commitMsg, dontSave, dontShowAlert
  
  workingCopies = value(getPref("svn_wCopies_paths_Binary.txt"))
  if ilk(workingCopies) <> #proplist then workingCopies = [:]
  
  svnBinary = mGetSVNBinaryPath(me, workingCopies)
  if length(svnBinary) < 1 then return 0
  
  workingFolder = mGetWorkingCopyPath(me, the moviepath & the moviename, chooseNewPath, workingCopies, "No working copy is specified for this movies binaries (director files). Please select a working copy of the director files.")
  if length(workingFolder) < 1 then return 0
  
  
  ---------------------------------------
  msg = string(commitMsg)
  if length(msg) < 1 then
    msg = baPrompt("Commit message", "Enter commit message", "Commitmessage", 0, -2, -2)
    
    if length(msg) < 1 then
      alert "You must enter a message for the commit action."
      return 0
    end if
  end if
  ---------------------------------------
  
  
  ---------------------------------------
  if dontSave <> 1 then
    -- this flag is only for the case, we come here from the combined commit call "mSVN_Commit_CurrentMovie_Binaries_AND_Scripttext"
    -- as the movie was already saved in "mSVN_Commit_CurrentScripts_ScriptText"
    
    if workingCopies.getaprop(#dontWarnWhenSaving) <> 1 then
      answer = baMsgBoxEx("The movie will be saved before committing.", "Save movie?", "Save", "Cancel", "Always save", "Question", 1, "left", "Arial", 14, 4, -2, -2)
      if answer = "Cancel" then return 0
      if answer = "Always save" then workingCopies.setaprop(#dontWarnWhenSaving, 1)
    end if
    
    statusSwitched = mSwitchToStatusMode(me, 1)
    
    mDisplayWaitStatusText me, "Saving movie..."
    
    
    -------------------- get version number:
    if the platform contains "mac" then
      svnBin = mConvertHFS2Unix(me, svnBinary)
      workFolder = mConvertHFS2Unix(me, workingFolder)
    else
      workFolder = workingFolder
      if the last char of workFolder = the last char of the applicationpath then delete the last char of workFolder
    end if
    versionNumber = mGetCurrentRevision(me, svnBin & "version", workFolder)
    
    if integerP(versionNumber) then
      
      versionNumber = versionNumber + 2 -- the version will raise
      
      VersionCheckUrl = string(mGetVersionCheckUrl(me, the moviepath & the moviename, workingCopies, "Select an URL to use for version checking."))
      if length(VersionCheckUrl) > 0 then
        mscript = member("aleXtrasMovieScript")
        ident = ""
        if mGetMemType(me, mscript) = #script then ident = string(xscr().mGetThisMovieName())
        if length(ident) < 1 then ident = the moviename
        
        thePassword = ""
        placeholder = "%%password:"
        offs = offset(placeholder, VersionCheckUrl)
        if offs > 0 then
          thePassword = VersionCheckUrl.char[offs+length(placeholder) .. length(VersionCheckUrl)]
          thePassword = word 1 to thePassword.word.count of thePassword
          delete char offs to length(VersionCheckUrl) of VersionCheckUrl
        end if
        
        VersionCheckUrl = word 1 to VersionCheckUrl.word.count of VersionCheckUrl & "?movieName=" & ident
      end if
      updateurl = 0
      repeat with cl = the number of castlibs down to 1
        versmem = member("subversion_version_field", cl)
        if [#field, #text].getPos(mGetMemType(me, versmem)) > 0 then
          if updateurl = 0 then updateurl = 1
          curr = versmem.text
          if char 1 of curr = "r" then delete char 1 of curr
          curr = integer(curr)
          if not(integerP(curr)) then curr = 0
          if curr < versionNumber then
            versmem.text = "r" & versionNumber
            if length(VersionCheckUrl) > 0 then
              versmem.comments = VersionCheckUrl
            end if
          else
            updateurl = 2
          end if
        end if
      end repeat
      
      if length(VersionCheckUrl) > 0 then
        if updateurl = 1 then
          if length(thePassword) then
            id = getnettext(VersionCheckUrl & "&version=" & thePassword & versionNumber)
          end if
        end if
      end if
      
    end if
    --------------------- // version number
    
    
    saveMovie()
    
  else
    
    statusSwitched = mSwitchToStatusMode(me, 1)
    
  end if
  ----------------------------------------
  
  
  
  ---------------------------------------
  if workingFolder <> the moviepath then
    erg = baCopyFileProgress(the moviepath & the moviename, workingFolder & the moviename, "Always", "Copying movie to working folder", "", 33)
    if erg <> 0 then
      alert "An error occurred during copying the movie:" && erg
      if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
      return 0
    end if
    clnum = the number of castlibs
    mp = the moviepath
    mplen = length(mp)
    olddelim = the itemdelimiter
    the itemdelimiter = mp
    repeat with n = 2 to clnum
      fname = castlib(n).filename
      if length(fname) > 0 then
        
        if the moviepath & the moviename <> fname then
          offs = offset(mp, fname)
          if offs <> 1 then
            alert "Castlib" && castlib(n) && "is not within your current moviepath. This is not supported."
            if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
            return 0
          end if
          relpath = fname
          delete char 1 to mplen of relpath
          
          newpath = workingFolder & relpath
          newpathDir = newpath
          delete the last item of newpathDir
          if baFolderExists(newpathDir) <> 1 then baCreateFolder(newpathDir)
          baCopyFileProgress(fname, newpath, "Always", "Copying castlib " & n & " to working folder", "", 33)
          
        end if
        
      end if
    end repeat
    the itemdelimiter = olddelim
  end if
  ---------------------------------------
  
  ---------------------------------------
  if the platform contains "mac" then
    svnBinary = mConvertHFS2Unix(me, svnBinary)
    workingFolder = mConvertHFS2Unix(me, workingFolder)
  else
    if the last char of workingFolder = the last char of the applicationpath then delete the last char of workingFolder
  end if
  ---------------------------------------
  
  
  ---------------------------------------
  mDisplayWaitStatusText me, "SVN update for:" && workingFolder
  
  theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "update" && QUOTE & workingFolder & QUOTE, RETURN, 0, 1)
  
  if count(theresult) < 1 then
    theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "update" && QUOTE & workingFolder & QUOTE && "2>&1", RETURN, 0, 1)
    if count(theresult) < 1 then
      theError = theresult[1]
    else
      theError = "svn did not respond at all."
    end if
    alert "Can't proceed svn error while trying to update:" && theError
    if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
    return 0  
  end if
  ---------------------------------------
  
  
  ------- NOW, basically there are two options, one is only commiting the files, which we have copied (director movie and castlibs)
  ------- OR commit the whole folder (-> everything, which is changed or added to <workingFolder>)
  ------- The problem regarding option #1, is that it would be a new revision for each file -> ugly
  ------- #2 however "messes" with files, which the user may not expect...
  ------- I for one prefer #2 anyway, therefore I didn't even implement #1
  ------- in order to implement #1 you can use a repeat loop just like the above repeat, which goes thorugh each castlib
  
  theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "status" && QUOTE & workingFolder & QUOTE, RETURN, 0, 1)
  
  cnt = count(theresult)
  if cnt < 1 then
    alert "Apparently there are no changes to the most recent version in the svn repository: svn status is empty"
    if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
    return 0
  end if
  
  repeat with n = 1 to cnt
    answ = theresult[n]
    meth = word 1 of answ
    delete word 1 of answ
    
    case meth of
      "!": -- deleted file (missing here, but in the repository present)
        -- I don't think, it is a good idea to delete the file in the repository in this case
        -- as it may also be the case, that we only have a subset of the castlibs/scripts
        -- therefore I just skip these files and leave it to the user what to do with them in his svn client application
        -- we only put a message, so the user is made aware of this fact
        put "Missing file!" && answ && "is missing here, but present in the repository"
        mDisplayWaitStatusText me, "Missing file!" && answ && "is missing here, but present in the repository"
        
      "?": -- newly added file
        mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "add" && QUOTE & answ & QUOTE, RETURN, 0, 1)
        
        put "Added file" && answ && "to the repository"
        mDisplayWaitStatusText me, "Added file" && answ && "to the repository"
        
      "m":
        -- just log the changes:
        put "Update file:" && answ
        mDisplayWaitStatusText me, "Update file:" && answ
        
    end case
    
  end repeat
  
  mDisplayWaitStatusText me, "SVN commit of:" && workingFolder
  
  theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "ci --message" && QUOTE & msg & QUOTE && QUOTE & workingFolder & QUOTE, RETURN, 0, 1)
  
  
  ---------------------------------------
  if count(theresult) > 0 then
    theResult = theresult[1]
    
    put theResult
    if dontShowAlert <> 1 then alert "Done, see the results of the svn operation in the message window."
    
  else
    if dontShowAlert <> 1 then alert "svn application did not respond, something seems to have failed. :-("
  end if
  ---------------------------------------
  
  setPref("svn_wCopies_paths_Binary.txt", string(workingCopies))
  
  if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
  
  return 1
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Update_AllCastlibs_and_source me, chooseNewPath, onlyUpdate_SVN_WorkingFolder, selectionList, revisionNumber, dontShowAlert
  mSVN_Update_Working_Copy_ScriptText me, revisionNumber, void, #all, 1
  mSVN_Update_SelectedCastlib me, chooseNewPath, onlyUpdate_SVN_WorkingFolder, #all, revisionNumber, dontShowAlert
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Update_SelectedCastlib_and_source me, chooseNewPath, onlyUpdate_SVN_WorkingFolder, selectionList, revisionNumber, dontShowAlert
  mSVN_Update_Working_Copy_ScriptText me, revisionNumber, void, selectionList, 1
  mSVN_Update_SelectedCastlib me, chooseNewPath, onlyUpdate_SVN_WorkingFolder, selectionList, revisionNumber, dontShowAlert
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Update_SelectedCastlib me, chooseNewPath, onlyUpdate_SVN_WorkingFolder, selectionList, revisionNumber, dontShowAlert
  
  castlibList = getCastlibList(me, selectionList, the activecastlib)
  
  if count(castlibList) < 1 then
    alert "No castlib selected. The parameter didn't yield a valid list. Make sure, that you provide a linear list with integers, specifying a number of a castlib each."
    return 0
  end if
  put "Updating castlibs:" && castlibList
  
  
  workingCopies = value(getPref("svn_wCopies_paths_Binary.txt"))
  if ilk(workingCopies) <> #proplist then workingCopies = [:]
  
  svnBinary = mGetSVNBinaryPath(me, workingCopies)
  if length(svnBinary) < 1 then exit
  
  workingFolder = mGetWorkingCopyPath(me, the moviepath & the moviename, chooseNewPath, workingCopies, "No working copy is specified for this movies binaries (director files). Please select a working copy of the director files.")
  if length(workingFolder) < 1 then exit
  
  
  
  oldmoviepath = the moviepath
  oldmovie = oldmoviepath & the moviename
  
  
  isMac = (the platform contains "mac")
  castlibPathList = []
  
  ---------------------------------------
  if workingFolder <> oldmoviepath then
    
    mp = oldmoviepath
    mplen = length(mp)
    olddelim = the itemdelimiter
    the itemdelimiter = mp
    
    
    repeat with actCL in castlibList
      fname = castlib(actCL).filename
      if length(fname) > 0 then
        
        offs = offset(mp, fname)
        if offs <> 1 then
          alert "Castlib" && castlib(actCL) && "is not within your current moviepath. This is not supported."
          return 0
        end if
        
        relpath = fname
        delete char 1 to mplen of relpath
        
        newpath = workingFolder & relpath
        --        newpathDir = newpath
        --        delete the last item of newpathDir
        
        -- if baFolderExists(newpathDir) <> 1 then baCreateFolder(newpathDir)
        -- baCopyFileProgress(fname, newpath, "Always", "Copying castlib " & actCL & " to working folder", "", 33)
        
        if isMac then
          castlibPathList.add([#newpath:newpath, #newpathUnix: mConvertHFS2Unix(me, newpath), #fname:fname])
        else
          castlibPathList.add([#newpath:newpath, #newpathUnix: newpath, #fname:fname])
        end if
        
      else
        
        -- musty something REALLY wrong, as the filename should ALWAYS be of any length (internal castlibs )
        return 0
        
      end if
      
    end repeat
    
    the itemdelimiter = olddelim
    
  else
    
    repeat with actCL in castlibList
      fname = castlib(actCL).filename
      newpath = fname
      if isMac then
        castlibPathList.add([#newpath:newpath, #newpathUnix: mConvertHFS2Unix(me, newpath), #fname:fname])
      else
        castlibPathList.add([#newpath:newpath, #newpathUnix: newpath, #fname:fname])
      end if
    end repeat
    
  end if
  ---------------------------------------
  
  
  statusSwitched = mSwitchToStatusMode(me, 1)
  
  
  doReturnToOldMovie = 0
  
  if (onlyUpdate_SVN_WorkingFolder <> 1) or (oldmoviepath contains workingFolder) then
    
    mDisplayWaitStatusText me, "Temporary switch to new movie in order to replace files"
    
    ------------------------------
    -- close this movie by issuing a "new movie..." command
    -- in order to replace the current movie, with the updated movie
    dispatchcommand(4097)
    ------------------------------
    
    doReturnToOldMovie = 1
    
  end if
  
  
  
  if the platform contains "mac" then
    svnBinary = mConvertHFS2Unix(me, svnBinary)
    workingFolderUnix = mConvertHFS2Unix(me, workingFolder)
    -- newpathUnix = baUnixName(newpath)
  else
    -- newpathUnix = newpath
    workingFolderUnix = workingFolder
    if the last char of workingFolderUnix = the last char of the applicationpath then delete the last char of workingFolderUnix
  end if
  
  
  mDisplayWaitStatusText me, "SVN update..." -- && newpathUnix
  
  
  revisionNumber = getValidRevNumber(me, revisionNumber)
  
  repeat with castlibPaths in castlibPathList
    
    theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "update" && "-r" && revisionNumber && QUOTE & castlibPaths[#newpathUnix] & QUOTE, RETURN, 0, 1)
    
    if count(theresult) < 1 then
      if doReturnToOldMovie = 1 then go to movie oldmovie
      if dontShowAlert <> 1 then alert "svn application did not respond, something seems to have failed. :-("
      setPref("svn_wCopies_paths_Binary.txt", string(workingCopies))
      if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
      exit
    else
      put theresult[1]
    end if
  end repeat
  
  
  
  -------- reload the refreshed movie:
  if oldmoviepath contains workingFolder then
    
    -- go to movie the moviepath & the moviename
    
  else
    
    if onlyUpdate_SVN_WorkingFolder <> 1 then
      
      -------------------------- now get the updated movies over here:
      if the last char of workingFolder <> the last char of the applicationpath then put the last char of the applicationpath after workingFolder
      
      put "Copy file from " & workingFolder & " to " & oldmoviepath
      
      
      repeat with castlibPaths in castlibPathList
        
        erg = baCopyFileProgress(castlibPaths[#newpath], castlibPaths[#fname], "Always", "Replacing castlib with the version of the working folder", "", 33)
        if erg <> 0 then
          alert "An error occurred during copying the file (" & castlibPaths[#newpath] & "). Error:" && erg
        else
          put "Replaced file:" && castlibPaths[#newpath]
        end if
        
      end repeat
      ----------------------------------
      
    end if
    
  end if
  
  
  
  if doReturnToOldMovie = 1 then go to movie oldmovie
  
  if dontShowAlert <> 1 then alert "Done, see the results of the svn operation in the message window."
  
  
  
  setPref("svn_wCopies_paths_Binary.txt", string(workingCopies))
  
  if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Update_CurrentMovie_Binaries me, chooseNewPath, onlyUpdate_SVN_WorkingFolder, revisionNumber, dontShowAlert
  
  workingCopies = value(getPref("svn_wCopies_paths_Binary.txt"))
  if ilk(workingCopies) <> #proplist then workingCopies = [:]
  
  svnBinary = mGetSVNBinaryPath(me, workingCopies)
  if length(svnBinary) < 1 then exit
  
  workingFolder = mGetWorkingCopyPath(me, the moviepath & the moviename, chooseNewPath, workingCopies, "No working copy is specified for this movies binaries (director files). Please select a working copy of the director files.")
  if length(workingFolder) < 1 then exit
  
  
  statusSwitched = mSwitchToStatusMode(me, 1)
  
  
  oldmoviepath = the moviepath
  oldmovie = oldmoviepath & the moviename
  
  
  if the platform contains "mac" then
    
    if the platform contains "mac" then
      if not(interface(xtra "budapi") contains "baUnixName") then
        alert "Please update your copy of the buddyApi xtra to the latest version, baUnixName() is missing."
        exit
      end if
    end if
    
    
    svnBinary = mConvertHFS2Unix(me, svnBinary)
    workingFolderUnix = mConvertHFS2Unix(me, workingFolder)
    targetPath = mConvertHFS2Unix(me, oldmoviepath)
    
    theDelim = "/"
    if the last char of targetPath = theDelim then delete the last char of targetPath
    if the last char of workingFolderUnix = theDelim then delete the last char of workingFolderUnix
    
  else
    workingFolderUnix = workingFolder
    targetPath = oldmoviepath
    theDelim = the last char of the applicationpath
    if the last char of workingFolderUnix = theDelim then delete the last char of workingFolderUnix
    if the last char of targetPath = theDelim then delete the last char of targetPath
  end if
  
  
  
  doReturnToOldMovie = 0
  
  if (onlyUpdate_SVN_WorkingFolder <> 1) or (oldmoviepath contains workingFolder) then
    
    
    mDisplayWaitStatusText me, "Temporary switch to new movie in order to replace files"
    
    ------------------------------
    -- close this movie by issuing a "new movie..." command
    -- in order to replace the current movie, with the updated movie
    dispatchcommand(4097)
    ------------------------------
    
    doReturnToOldMovie = 1
    
  end if
  
  mDisplayWaitStatusText me, "SVN update:" && workingFolderUnix
  
  revisionNumber = getValidRevNumber(me, revisionNumber)
  
  theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "update" && "-r" && revisionNumber && QUOTE & workingFolderUnix & QUOTE, RETURN, 0, 1)
  
  if count(theresult) > 0 then
    
    -------- reload the refreshed movie:
    if oldmoviepath contains workingFolder then
      
      -- go to movie the moviepath & the moviename
      
    else
      
      if onlyUpdate_SVN_WorkingFolder <> 1 then
        
        -------------------------- now get the updated movies over here:
        if the last char of workingFolder <> the last char of the applicationpath then put the last char of the applicationpath after workingFolder
        
        put "Copy files from " & workingFolder & " to " & oldmoviepath
        
        --        mCopyAllFiles(me, workingFolder, oldmoviepath, theresult)
        
        mCopyAllFiles(me, workingFolder, oldmoviepath, theresult, workingFolderUnix, targetPath)
        
        ----------------------------------
        
      end if
      
    end if
    
    put theresult[1]
    
    if doReturnToOldMovie = 1 then
      --  put RETURN & "go to movie" && oldmovie & RETURN
      go to movie oldmovie
    end if
    
    if dontShowAlert <> 1 then alert "Done, see the results of the svn operation in the message window."
    
  else
    
    if doReturnToOldMovie = 1 then go to movie oldmovie
    
    if dontShowAlert <> 1 then alert "svn application did not respond, something seems to have failed. :-("
  end if
  
  
  setPref("svn_wCopies_paths_Binary.txt", string(workingCopies))
  
  
  if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mCopyAllFiles me, fromFolder, toFolder, listOfChangedFiles, fromFolderUnix, toFolderUnix
  
  if the platform contains "mac" then
    if not(interface(xtra "budapi") contains "baReturnUnixNames") then
      alert "Please update your copy of the buddyApi xtra to the latest version, baReturnUnixNames() is missing."
      listOfChangedFiles = 0
    end if
  end if
  
  if listP(listOfChangedFiles) then -- we have a list with status messages from svn for each file
    
    resetUNames = 0
    if the platform contains "mac" then
      if not(baSysFolder("prefs") starts "/") then
        baReturnUnixNames(1)
        resetUNames = 1
      end if
    end if
    
    repeat with thisFile in listOfChangedFiles
      if char 2 of thisFile = " " then
        action = char 1 of thisFile
        delete char 1 of thisFile
        repeat while char 1 of thisFile = " "
          delete char 1 of thisFile
          if length(thisFile) < 1 then exit repeat
        end repeat
        if length(thisFile) > 0 then
          offs = offset(fromFolderUnix, thisFile)
          if offs = 1 then
            case action of
              "D": -- deleted
                erg = baDeleteFile(toFolderUnix & thisFile)
                
                if erg <> 1 then
                  alert "An error occurred during deleting the file (" & toFolderUnix & thisFile & "). Error:" && erg
                else
                  put "Delted file:" && toFolderUnix & thisFile
                end if
                
              "U", "A": -- updated, added
                delete char 1 to length(fromFolderUnix) of thisFile
                erg = baCopyFileProgress(fromFolderUnix & thisFile, toFolderUnix & thisFile, "Always", "Replacing file " & thisFile & " with the version of the working folder", "", 33)
                
                if erg <> 0 then
                  alert "An error occurred during copying the file (" & thisFile & "). Error:" && erg
                else
                  put "Replaced file:" && thisFile
                end if
                
            end case
          else
            put "Couldn't copy file" && thisFile && "as it doesn't seem to be in the current path:" && fromFolderUnix
          end if
          
        end if
      end if
    end repeat
    
    if resetUNames = 1 then baReturnUnixNames(0)
    
  else -- no list? we just copy over all .dir and .cst and .ls files
    
    flist = baFolderList(fromFolder)
    flistCnt = count(flist)
    repeat with n = flistCnt down to 1
      thisFolder = flist[n]
      if thisFolder starts ".svn" then next repeat
      if not(baFolderExists(toFolder & thisFolder)) then
        put "Attempting to create folder:" && toFolder & thisFolder
        erg = baCreateFolder(toFolder & thisFolder)
        
        if erg = 0 then put "successfully created folder"
        else put "Unable to create folder."
        
      else
        erg = 1
      end if
      
      if erg = 1 then
        mCopyAllFiles me, fromFolder & thisFolder & the last char of toFolder, toFolder & thisFolder & the last char of toFolder
      else
        alert "Unable to create folder (" & thisFolder & "). Error:" && erg
      end if
      
    end repeat
    
    flist = baFileList(fromFolder, "*.*")
    flistCnt = count(flist)
    repeat with n = 1 to flistCnt
      thisFile = flist[n]
      if thisFile starts ".svn" then next repeat
      -- we get ourselves only dir and cast files:
      len = length(thisFile)
      ext = thisFile.char[len-3 .. len]
      if ext = ".dir" or ext = ".cst" or ext = ".ls" then
        erg = baCopyFileProgress(fromFolder & thisFile, toFolder & thisFile, "Always", "Replacing movie with the version of the working folder", "", 33)
        if erg <> 0 then
          alert "An error occurred during copying the file (" & thisFile & "). Error:" && erg
        else
          put "Replaced file:" && thisFile
        end if
      end if
    end repeat
    
  end if
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mSVN_Commit_CurrentScripts_ScriptText me, selected_Members_Only, chooseNewPath, commitMsg, dontShowAlert, selectionList, versionIncrease
  
  workingCopies = value(getPref("svn_workingCopies_paths.txt"))
  if ilk(workingCopies) <> #proplist then workingCopies = [:]
  
  svnBinary = mGetSVNBinaryPath(me, workingCopies)
  if length(svnBinary) < 1 then return 0
  
  workingFolder = mGetWorkingCopyPath(me, the moviepath & the moviename, chooseNewPath, workingCopies, "No working copy is specified for this movies scripttext. Please select a working copy for the scripttext now.")
  if length(workingFolder) < 1 then return 0
  
  msg = string(commitMsg)
  
  
  if length(msg) < 1 then
    
    msg = baPrompt("Commit message", "Enter commit message", "Commitmessage", 0, -2, -2)
    
    if length(msg) < 1 then
      alert "You must enter a message for the commit action."
      return 0
    end if
    
  end if
  
  
  if workingCopies.getaprop(#dontWarnWhenSaving) <> 1 then
    answer = baMsgBoxEx("The movie will be saved before committing.", "Save movie?", "Save", "Cancel", "Always save", "Question", 1, "left", "Arial", 14, 4, -2, -2)
    if answer = "Cancel" then return 0
    if answer = "Always save" then workingCopies.setaprop(#dontWarnWhenSaving, 1)
  end if
  
  
  
  statusSwitched = mSwitchToStatusMode(me, 1)
  
  clibfolder = ""
  
  castlibList = 0
  
  
  
  
  -------------------- get version number:
  VersionCheckUrl = ""
  
  if the platform contains "mac" then
    svnBin = mConvertHFS2Unix(me, svnBinary)
    workFolder = mConvertHFS2Unix(me, workingFolder)
  else
    workFolder = workingFolder
    if the last char of workFolder = the last char of the applicationpath then delete the last char of workFolder
  end if
  versionNumber = mGetCurrentRevision(me, svnBin & "version", workFolder)
  
  if integerP(versionNumber) then
    
    if voidP(versionIncrease) then versionIncrease = 1
    versionNumber = versionNumber + versionIncrease + 1 -- the version will raise
    
    VersionCheckUrl = string(mGetVersionCheckUrl(me, the moviepath & the moviename, workingCopies, "Select an URL to use for version checking."))
    if length(VersionCheckUrl) > 0 then
      mscript = member("aleXtrasMovieScript")
      ident = ""
      if mGetMemType(me, mscript) = #script then ident = string(xscr().mGetThisMovieName())
      if length(ident) < 1 then ident = the moviename
      
      thePassword = ""
      placeholder = "%%password:"
      offs = offset(placeholder, VersionCheckUrl)
      if offs > 0 then
        thePassword = VersionCheckUrl.char[offs+length(placeholder) .. length(VersionCheckUrl)]
        thePassword = word 1 to thePassword.word.count of thePassword
        delete char offs to length(VersionCheckUrl) of VersionCheckUrl
      end if
      
      VersionCheckUrl = word 1 to VersionCheckUrl.word.count of VersionCheckUrl & "?movieName=" & ident
      
    end if
    
  end if
  --------------------- // version number
  
  
  
  
  -------- if we work on only one castlib:
  
  if ilk(selected_Members_Only) = #castLib then
    
    -----------------------------------------------------
    processIOnlySelectedMembers = 1
    clsellist = string(selectionList)
    if length(clsellist) > 0 then
      if listP(value(clsellist)) then processIOnlySelectedMembers = 0
    end if
    castlibList = getCastlibList(me, selectionList, selected_Members_Only.number)
    
    
    mpname = the moviepath & the moviename
    isInternal = 0
    repeat with cl in castlibList
      if castlib(cl).filename = mpname then
        isInternal = 1
        exit repeat
      end if
    end repeat
    
    
    if count(castlibList) < 1 then
      alert "No castlib selected. The parameter didn't yield a valid list. Make sure, that you provide a linear list with integers, specifying a number of a castlib each."
      if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
      return 0
    end if
    
    -----------------------------------------------------
    
    delim = the last char of the applicationpath
    
    olddelim = the itemdelimiter
    the itemdelimiter = delim
    
    updateurl = 0
    
    repeat with n = count(castlibList) down to 1
      
      selected_Members_Only = castlib(castlibList[n])
      
      -------------------- get version number:
      
      
      versmem = member("subversion_version_field", selected_Members_Only.number)
      if [#field, #text].getPos(mGetMemType(me, versmem)) > 0 then
        if updateurl = 0 then updateurl = 1
        curr = versmem.text
        if char 1 of curr = "r" then delete char 1 of curr
        curr = integer(curr)
        if not(integerP(curr)) then curr = 0
        if curr < versionNumber then
          versmem.text = "r" & versionNumber
          if length(VersionCheckUrl) > 0 then
            versmem.comments = VersionCheckUrl
          end if
        else
          updateurl = 2
        end if
      end if
      
      --------------------- // version number
      
      
      if the last item of workingFolder = "castlib" & selected_Members_Only.number then
        clibfolder = workingFolder
      else
        if the last char of workingFolder <> delim then put delim after workingFolder
        clibfolder = workingFolder & "castlib" & selected_Members_Only.number
      end if
      
      castlibList[n] = [castlibList[n], clibfolder, workingFolder]
      --------
      
      
    end repeat
    
    -------------------- get version number:
    if length(VersionCheckUrl) > 0 then
      if updateurl = 1 then
        if length(thePassword) then
          id = getnettext(VersionCheckUrl & "&version=" & thePassword & versionNumber)
        end if
      end if
    end if
    --------------------- // version number
    
    the itemdelimiter = olddelim
    
    
    
  else -- treat all:
    
    processIOnlySelectedMembers = 0
    
    -------------------- get version number:
    updateurl = 0
    
    repeat with cl = the number of castlibs down to 1
      versmem = member("subversion_version_field", cl)
      if [#field, #text].getPos(mGetMemType(me, versmem)) > 0 then
        if updateurl = 0 then updateurl = 1
        curr = versmem.text
        if char 1 of curr = "r" then delete char 1 of curr
        curr = integer(curr)
        if not(integerP(curr)) then curr = 0
        if curr < versionNumber then
          versmem.text = "r" & versionNumber
          if length(VersionCheckUrl) > 0 then
            versmem.comments = VersionCheckUrl
          end if
        else
          updateurl = 2
        end if
      end if
    end repeat
    
    if length(VersionCheckUrl) > 0 then
      if updateurl = 1 then
        if length(thePassword) then
          id = getnettext(VersionCheckUrl & "&version=" & thePassword & versionNumber)
        end if
      end if
    end if
    --------------------- // version number
    
    
    castlibList = 0
    
  end if
  
  
  workingFolderOriginal = workingFolder
  
  if castlibList = 0 then
    
    -------- if we work on only one castlib:
    if length(clibfolder) > 0 then workingFolder = clibfolder
    --------
    
    if the platform contains "mac" then
      svnBinary = mConvertHFS2Unix(me, svnBinary)
      workingFolder = mConvertHFS2Unix(me, workingFolder)
    else
      if the last char of workingFolder = the last char of the applicationpath then delete the last char of workingFolder
    end if
    
    
    workingFolder = " " & QUOTE & workingFolder & QUOTE
    
  else
    
    if the platform contains "mac" then svnBinary = mConvertHFS2Unix(me, svnBinary)
    cnt = count(castlibList)
    
    workingFolder = ""
    
    repeat with n = 1 to cnt
      
      if the platform contains "mac" then
        castlibList[n][3] = mConvertHFS2Unix(me, castlibList[n][3])
      else
        str = castlibList[n][3]
        if the last char of str = the last char of the applicationpath then
          delete the last char of str
          castlibList[n][3] = str
        end if
      end if
      
      put " " & QUOTE & castlibList[n][3] & QUOTE after workingFolder
      
    end repeat
    
  end if
  
  
  mDisplayWaitStatusText me, "SVN update of:" && workingFolder
  
  
  theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "update" & workingFolder, RETURN, 0, 1)
  
  
  if count(theresult) < 1 then
    theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "update" & workingFolder && "2>&1", RETURN, 0, 1)
    if count(theresult) < 1 then
      theError = theresult[1]
    else
      theError = "svn did not respond at all."
    end if
    alert "Can't proceed svn error while trying to update:" && theError
    if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
    return 0  
  end if
  
  
  
  -------- if we work on only one castlib:
  
  if ilk(selected_Members_Only) = #castLib then
    
    mDisplayWaitStatusText me, "Commiting scripttext for castlibs:" && castlibList
    put "Commiting scripttext for castlibs:" && castlibList
    repeat with n = count(castlibList) down to 1
      
      selected_Members_Only = castlib(castlibList[n][1])
      if processIOnlySelectedMembers = 0 then processEntireCastlib = selected_Members_Only
      else processEntireCastlib = 1
      mDisplayWaitStatusText me, "Saving castlib:" && selected_Members_Only
      selected_Members_Only.save()
      mExportAllScriptsToDiskWithFolder me, 1, processEntireCastlib, 1, mConvertUnix2Hfs(me, castlibList[n][3])
      
    end repeat
    
  else -- treat all:
    
    mDisplayWaitStatusText me, "Saving movie..."
    saveMovie()
    mExportAllScriptsToDiskWithFolder me, 1, processIOnlySelectedMembers, 1, workingFolderOriginal
    
  end if
  
  
  theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "status" & workingFolder, RETURN, 0, 1)
  
  cnt = count(theresult)
  if cnt < 1 then
    alert "Apparently there are no changes to the scripts of the most recent version in the svn repository: svn status is empty"
    if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
    return 1
  end if
  
  repeat with n = 1 to cnt
    answ = theresult[n]
    meth = word 1 of answ
    delete word 1 of answ
    
    case meth of
      "!": -- deleted file (missing here, but in the repository present)
        
        if processIOnlySelectedMembers = 1 then
          -- I don't think, it is a good idea to delete the file in the repository in this case
          -- as it may also be the case, that we only have a subset of the castlibs/scripts
          -- therefore I just skip these files and leave it to the user what to do with them in his svn client application
          -- we only put a message, so the user is made aware of this fact
          put "Missing file!" && answ && "is missing here, but present in the repository"
          
          mDisplayWaitStatusText me, "Missing file!" && answ && "is missing here, but present in the repository"
          
        else
          
          mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "rm" && QUOTE & answ & QUOTE, RETURN, 0, 1)
          
          put "Deleted file" && answ && "in the repository"
          
          mDisplayWaitStatusText me, "Deleted file" && answ && "in the repository"
          
        end if
        
        
      "?": -- newly added file
        mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "add" && QUOTE & answ & QUOTE, RETURN, 0, 1)
        
        put "Added file" && answ && "to the repository"
        
        mDisplayWaitStatusText me, "Added file" && answ && "to the repository"
        
      "m":
        -- just log the changes:
        put "Update file:" && answ
        
        mDisplayWaitStatusText me, "Update file:" && answ
        
    end case
    
  end repeat
  
  mDisplayWaitStatusText me, "SVN commit:" && workingFolder
  
  theresult = mDoShellCmd(me, QUOTE & svnBinary & QUOTE && "ci --message" && QUOTE & msg & QUOTE & workingFolder, RETURN, 0, 1)
  
  
  if count(theresult) > 0 then
    theResult = theresult[1]
    
    put theResult
    
    
    if dontShowAlert <> 1 then
      alert "Done, see the results of the svn operation in the message window."
    end if
    
  else
    alert "svn application did not respond, something seems to have failed, while commiting the scripttext. :-("
  end if
  
  
  setPref("svn_workingCopies_paths.txt", string(workingCopies))
  
  
  if statusSwitched = 1 then statusSwitched = mSwitchToStatusMode(me, 0)
  
  return 1
  
end



-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
---------- svn helper handlers

on mGetSVNBinaryPath me, workingCopies
  
  if (mCheckForXtra(me, "budapi") + (mCheckForXtra(me, "Shell") + mCheckForXtra(me, "ff_shell"))) < 2 then
    alert "You need the shell xtra and the buddyApi xtra in order to proceed"
    return ""
  end if
  
  savePrefs = 0
  if ilk(workingCopies) <> #proplist then
    workingCopies = value(getPref("svn_workingCopies_paths.txt"))
    if ilk(workingCopies) <> #proplist then workingCopies = [:]
    savePrefs = 1
  end if
  
  
  if the platform contains "mac" then
    svnBinary = baHFSName(string(workingCopies.getaprop(#svnBinary)))
  else
    svnBinary = string(workingCopies.getaprop(#svnBinary))
  end if
  
  if the platform contains "mac" then -- dunno how to check on windows, if svn is installed, anyone ?
    
    if baFileExists(svnBinary) <> 1 then svnBinary = ""
    
    
    if length(svnBinary) < 1 then
      
      svnBinary = []
      
      svnBinary = mDoShellCmd(me, "for thisBinPath in /usr/bin /bin /usr/sbin /sbin /usr/local/sbin /usr/local/bin /opt/sbin /opt/bin /opt/local/sbin /opt/local/bin /sw/bin /sw/sbin ; do find $thisBinPath -name svn ; done", RETURN, 0, 1)
      -- end if
      
      if count(svnBinary) < 1 then
        alert "svn binary not found in your binaries folder. Where is the svn binary?"
        svnBinary = mGetFilePathFromUser(me)
        if length(svnBinary) < 1 then return ""
        
      else
        svnBinary = baHFSName(svnBinary[1])
        
      end if
      
      
    end if
    
    if baFileExists(svnBinary) <> 1 then
      alert "The path to the svn binary seems to have changed. Please try again in order to search the path."
      svnBinary = ""
    end if
    
    
  else
    -- dunno how to check on windows, if svn is installed, anyone ?
    svnBinary = "svn"
  end if
  
  
  workingCopies.setaprop(#svnBinary, svnBinary)
  
  if savePrefs = 1 then setPref("svn_workingCopies_paths.txt", string(workingCopies))
  
  return svnBinary
  
end


-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mGetVersionCheckUrl me, movieIdentifier, workingCopies, selectMsg
  
  movieIdentifier = string(movieIdentifier)
  if length(movieIdentifier) < 1 then
    movieIdentifier = the moviepath & the moviename
  end if
  
  savePrefs = 0
  if ilk(workingCopies) <> #proplist then
    workingCopies = value(getPref("svn_workingCopies_paths.txt"))
    if ilk(workingCopies) <> #proplist then workingCopies = [:]
    savePrefs = 1
  end if
  
  versionCheckUrlList = workingCopies.getaprop(#versionCheckUrls)
  if not(objectP(versionCheckUrlList)) then
    versionCheckUrlList = [:] 
    workingCopies.setaprop(#versionCheckUrls, versionCheckUrlList)
  end if
  
  versionCheckUrl = string(versionCheckUrlList.getaprop(movieIdentifier))
  
  if length(versionCheckUrl) > 0 then return versionCheckUrl
  
  
  repeat with n = the number of castlibs down to 1
    versmem = member("subversion_version_field", n)
    if [#field, #text].getPos(mGetMemType(me, versmem)) > 0 then
      versionCheckUrl = string(versmem.comments)
      if length(versionCheckUrl) then exit repeat
    end if
  end repeat
  
  if length(versionCheckUrl) > 0 then
    
    selectMsg = string(selectMsg)
    if length(selectMsg) < 1 then selectMsg = "No update check URL is specified for this movie. Please enter an URL."
    
    --    if mCheckForXtra(me, "BudApi") = 1 then
    --      versionCheckUrl = baPrompt("Select URL", selectMsg, "http://www.farbflash.de/cgi-bin/versionCheck.pl %%password: secretpassword", 0, -2, -2)
    --    else
    muiobj = new(xtra "Mui")
    --     versionCheckUrl = muiobj.GetUrl("http://www.farbflash.de/cgi-bin/versionChecker.pl %%password: secretpassword", 1)
    versionCheckUrl = muiobj.GetUrl(versionCheckUrl && "%%password: secretpassword", 1)
    muiobj = 0
    --    end if
    
    if length(versionCheckUrl) < 1 then return ""
    
    versionCheckUrlList.setaprop(movieIdentifier, versionCheckUrl)
    
    if savePrefs = 1 then setPref("svn_workingCopies_paths.txt", string(workingCopies))
    
  end if
  
  return versionCheckUrl
  
end

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mGetWorkingCopyPath me, movieIdentifier, chooseNewPath, workingCopies, selectMsg
  
  movieIdentifier = string(movieIdentifier)
  if length(movieIdentifier) < 1 then
    movieIdentifier = the moviepath & the moviename
  end if
  
  savePrefs = 0
  if ilk(workingCopies) <> #proplist then
    workingCopies = value(getPref("svn_workingCopies_paths.txt"))
    if ilk(workingCopies) <> #proplist then workingCopies = [:]
    savePrefs = 1
  end if
  
  workingFolder = string(workingCopies.getaprop(movieIdentifier))
  
  if length(workingFolder) > 0 then
    if baFolderExists(workingFolder) <> 1 then
      workingFolder = ""
    end if
  end if
  
  
  if chooseNewPath = 1 then
    workingFolder = mGetFolderPathFromUser(me, the moviepath, "Please select a working copy for SVN update", 3, "Select working copy", -2, -2)
  end if
  
  if workingFolder = "" then
    
    selectMsg = string(selectMsg)
    if length(selectMsg) < 1 then selectMsg = "No working copy is specified for this movie. Please select a working copy now."
    
    alert selectMsg
    
    workingFolder = mGetFolderPathFromUser(me, the moviepath, "Please select a working copy for SVN update", 3, "Select working copy", -2, -2)
  end if
  
  if length(workingFolder) < 1 then return ""
  
  if checkIfPathIsSVNWorkingCopy(me, workingFolder) <> 1 then
    alert workingFolder && "does not appear to be a svn working copy."
    workingCopies.deleteprop(movieIdentifier)
    return ""
  end if
  
  workingCopies.setaprop(movieIdentifier, workingFolder)
  
  if savePrefs = 1 then setPref("svn_workingCopies_paths.txt", string(workingCopies))
  
  return workingFolder
  
end

-- starting with version 1.7 now subversion keeps its metadata in only the root of a working copy
-- instead like how it was before in every folder, like it has been in git from the beginning
-- good move! But we now must check all the path upwoards to find the .svn folder in order to check
-- whether we have a working directory

on checkIfPathIsSVNWorkingCopy me, aPath
    if length(aPath) < 1 then return 0
  
  delim = the last char of the applicationpath
  if the last char of aPath <> delim then put delim after aPath
  
  if baFolderExists(aPath & ".svn") <> 1 then
    olddelim = the itemdelimiter
    the itemdelimiter = delim
    delete the last item of aPath
    delete the last item of aPath
    the itemdelimiter = olddelim
    return checkIfPathIsSVNWorkingCopy(me, aPath)
  end if
  
  return 1
end

