;
;************************************************************************
;Title      : update-pbp.pb (For all my projects)
;Auteur     : (C) 2017 CAGE
;Date       : 2017/09/21
;Version PB : PureBasic 5.61 (Windows - x86)
;Version PB : PureBasic 5.61 (Windows - x64)
;Compiler Options
;Compiler Options : 
;Compile/Run      : Create temporary executable in the source directory
;
;Libairies:
;
;************************************************************************
;

EnableExplicit ; all the variables must be declared

OnErrorGoto(?ErrorHandler)

Global G$ = Chr(34)

Global CREATOR32$ = "PureBasic 5.61 (Windows - x86)"
Global CREATOR64$ = "PureBasic 5.61 (Windows - x64)"

Global COMPILER32$ = "      <compiler version="+G$+CREATOR32$+G$+"/>"
Global COMPILER64$ = "      <compiler version="+G$+CREATOR64$+G$+"/>"

Global VER1$ = FormatDate("%yy,%mm,0,%dd", Date()) ; value="15,11,0,16"

Global VER2$ = FormatDate("%yy.%mm.%dd"  , Date()) ; value="15.11.16"

Global foldr$, old$

Global project$ , pbpfile$, bakfile$, tmpfile$

Procedure fileExist(f$)
  If FileSize(f$) = -1
    ProcedureReturn #False
  Else
    ProcedureReturn #True
  EndIf
EndProcedure

Procedure folderExist(f$)
  If FileSize(f$) = -2
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

Procedure updateVersion(f$)
  Protected readline$
  Protected text$
  Protected in.a  = 0
  Protected out.a = 1
  Protected pbfile$  = f$
  Protected bakfile$ = pbfile$ + ".bak"
  Protected tmpfile$ = pbfile$ + ".tmp"
  If Not fileExist(bakfile$)
    CopyFile(pbfile$, bakfile$)
  Else
    ;DeleteFile(bakfile$, #PB_FileSystem_Force)
  EndIf
  DeleteFile(tmpfile$, #PB_FileSystem_Force)
  CopyFile(pbfile$, tmpfile$)
  If ReadFile(in, tmpfile$)
    If CreateFile(out, pbfile$,#PB_UTF8)
      While Eof(in) = 0 ; loop as long the 'end of file' isn't reached
        readline$ = RTrim(ReadString(in))
        If FindString(readline$,"#AppVer$ = ",1,#PB_String_NoCase)
          text$ = "#AppVer$ = "+G$+VER2$+G$
          Debug text$
          WriteStringN(out, text$)
        Else
          Debug readline$
          WriteStringN(out, readline$)
        EndIf
      Wend
      CloseFile(out)
    Else
      ; ecriture impossible
    EndIf
    CloseFile(in)
  Else
    ; leture impossible
  EndIf
  DeleteFile(tmpfile$, #PB_FileSystem_Force)
EndProcedure

Procedure updateProject(pbp$)
  Protected readline$
  Protected text$
  Protected in.a  = 0
  Protected out.a = 1
  If ReadFile(in,pbp$)
    If CreateFile(out, pbpfile$,#PB_UTF8)
      While Eof(in) = 0 ; loop as long the 'end of file' isn't reached
        readline$ = RTrim(ReadString(in))
        ;<project xmlns="http://www.purebasic.com/namespace" version="1.0" creator="PureBasic 5.61 (Windows - x64)">
        If FindString(readline$,"creator=",1,#PB_String_NoCase)
          text$ = "<project xmlns="+G$+"http://www.purebasic.com/namespace"+G$+" version="+G$+"1.0"+G$+" creator="+G$+CREATOR64$+G$+">"
          Debug text$
          WriteStringN(out, text$)
        ;<compiler version="PureBasic 5.61 (Windows - x86)"/>
        ;<compiler version="PureBasic 5.61 (Windows - x64)"/>
        ElseIf FindString(readline$,"compiler version=",1,#PB_String_NoCase)
          If FindString(readline$,"x86",1,#PB_String_NoCase)
            Debug COMPILER32$
            WriteStringN(out, COMPILER32$)
          Else
            Debug COMPILER64$
            WriteStringN(out, COMPILER64$)
          EndIf
        ;<field0 value="15,11,0,15"/>
        ;<field1 value="15,11,0,15"/>
        ;<field4 value="15.11.15"/>
        ;<field5 value="15.11.15"/>
        ElseIf FindString(readline$,"field0 value=",1,#PB_String_NoCase)
          text$ = "        <field0 value="+G$+VER1$+G$+"/>"
          Debug text$
          WriteStringN(out, text$)
          Continue
        ElseIf FindString(readline$,"field1 value=",1,#PB_String_NoCase)
          text$ = "        <field1 value="+G$+VER1$+G$+"/>"
          Debug text$
          WriteStringN(out, text$)
          Continue
        ElseIf FindString(readline$,"field2 value=",1,#PB_String_NoCase)
          text$ = "        <field2 value="+G$+"CAGE"+G$+"/>"
          Debug text$
          WriteStringN(out, text$)
          Continue
        ElseIf FindString(readline$,"field4 value=",1,#PB_String_NoCase)
          text$ = "        <field4 value="+G$+VER2$+G$+"/>"
          Debug text$
          WriteStringN(out, text$)
          Continue
        ElseIf FindString(readline$,"field5 value=",1,#PB_String_NoCase)
          text$ = "        <field5 value="+G$+VER2$+G$+"/>"
          Debug text$
          WriteStringN(out, text$)
          Continue
        ElseIf FindString(readline$,"field9 value=",1,#PB_String_NoCase)
          text$ = "        <field9 value="+G$+"© CAGE"+G$+"/>"
          Debug text$
          WriteStringN(out, text$)
          Continue
        ElseIf FindString(readline$,"field10 value=",1,#PB_String_NoCase)
          text$ = "        <field10 value="+G$+"This software is the property of the author : CAGE"+G$+"/>"
          Debug text$
          WriteStringN(out, text$)
          Continue
        Else
          Debug readline$
          WriteStringN(out, readline$)
        EndIf
      Wend
      CloseFile(out)
    Else
      ; ecriture impossible
    EndIf
    CloseFile(in)
  Else
    ; leture impossible
  EndIf
EndProcedure

NewList folders.s()

If ExamineDirectory(0, ".", "*.*")
  While NextDirectoryEntry(0)
    If DirectoryEntryType(0) = #PB_DirectoryEntry_Directory
      foldr$ = DirectoryEntryName(0)
      If foldr$ <> old$
        Select Left(LCase(foldr$), 12)
          Case ".", "..", "icones"
          Default
            AddElement(folders())
            folders() = foldr$
        EndSelect
      EndIf
      old$ = foldr$
    EndIf
  Wend
  FinishDirectory(0)
EndIf

SortList(folders(), #PB_Sort_Ascending | #PB_Sort_NoCase)

updateVersion("pb-version.pbi")

updateVersion("pb-console-version.pbi")

ForEach folders()
  foldr$ = folders()
  If ExamineDirectory(0, ".\" + foldr$, "*.pbp")
    While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
        project$ = DirectoryEntryName(0)
        pbpfile$ = ".\" + foldr$ + "\" + project$
        bakfile$ = pbpfile$ + ".bak"
        tmpfile$ = pbpfile$ + ".tmp"
        Debug pbpfile$
        If Not fileExist(bakfile$)
          ;Debug bakfile$
          CopyFile(pbpfile$, bakfile$)
        Else
          ;DeleteFile(bakfile$, #PB_FileSystem_Force)
        EndIf
        DeleteFile(tmpfile$, #PB_FileSystem_Force)
        CopyFile(pbpfile$, tmpfile$)
        updateProject(tmpfile$)
        DeleteFile(tmpfile$, #PB_FileSystem_Force)
      EndIf
    Wend
  EndIf
Next

ErrorHandler:

End

; IDE Options = PureBasic 5.61 (Windows - x64)
; CursorPosition = 15
; Folding = w
; EnableXP
; CompileSourceDirectory
; EnablePurifier
; EnableUnicode