' Gambas module file

Public NewFile As Boolean

Private $sPath As String
Private $bRename As Boolean
Private $sAuth As String

Public Sub Begin(sPath As String, Optional bKeep As Boolean)
  
  $bRename = False
  $sPath = sPath
  
  NewFile = Not Exist(sPath)
  Try $sAuth = Stat(sPath).Auth
  
  Try Kill sPath & "~"
  If Not NewFile Then
    If bKeep Then
      Copy sPath To sPath & "~"
    Else
      Move sPath To sPath & "~"
    Endif
  Endif
  
  $bRename = True

End

Public Sub End()
  
  Dim sForm As String

  If $sAuth Then 
    ' Chmod can fail on Samba shares. Do not store Gambas project on non POSIX filesystems!
    Try Chmod $sPath To $sAuth
  Endif
  
  VersionControl.UpdatePath($sPath)
  Try Project.Tree.RefreshKey($sPath)
  If File.Ext($sPath) = "class" Then
    sForm = Project.Tree.FindForm(File.BaseName($sPath))
    If sForm Then Try Project.Tree.RefreshKey(sForm)
  Endif
  Try Project.Tree.SelectKey($sPath)
  
  FSearch.RefreshSearch
  
  $sPath = ""
  $bRename = False
  
End


Public Function Error() As Boolean
  
  Dim sError As String
  
  sError = Error.Text & " (" & Error.Where & ")"
  Debug sError
  
  If $bRename Then 
    Try Kill $sPath
    Try Move $sPath & "~" To $sPath
  Endif
  
  FMain.ShowError("<b>" & ("Unable to save file.") & "</b>\n\n" & sError, $sPath)
  Try Save.End
  Return True
  
End

