#Define UNICODE
#Define _WIN32_WINNT &h0602
#Include Once "windows.bi"
#Include Once "Afx\CWindow.inc"
#define CODEGEN_FORM
#define CODEGEN_LISTBOX
#define CODEGEN_PROGRESSBAR
#define CODEGEN_LABEL
#define CODEGEN_MAINMENU
#Include once "WinFormsX\WinFormsX.bi"
Using Afx

' WINFBE_CODEGEN_START

Declare Function Form1_MainMenu_Click( ByRef sender As wfxMenuItem, ByRef e As EventArgs ) As LRESULT
Declare Function Form1_MainMenu_Popup( ByRef sender As wfxMenuItem, ByRef e As EventArgs ) As LRESULT

type Form1Type extends wfxForm
  private:
    temp as byte
  public:
    declare static function FormInitializeComponent( byval pForm as Form1Type ptr ) as LRESULT
    declare constructor
    ' Controls
    List1 As wfxListBox
    Progress1 As wfxProgressBar
    Label1 As wfxLabel
end type


function Form1Type.FormInitializeComponent( byval pForm as Form1Type ptr ) as LRESULT
  dim as long nClientOffset

  dim ncm As NONCLIENTMETRICS
  ncm.cbSize = SizeOf(ncm)
  SystemParametersInfo(SPI_GETNONCLIENTMETRICS, SizeOf(ncm), @ncm, 0)
  nClientOffset = AfxUnScaleY(ncm.iMenuHeight)  ' holds the height of the mainmenu

  pForm->MainMenu.MenuItems.Clear
  pForm->MainMenu.Parent = pForm
  dim mnuStart as wfxMenuItem = wfxMenuItem("Start...", "mnuStart", "", 0, 0)
  pForm->MainMenu.MenuItems.Add(mnuStart)
  pForm->MainMenu.OnPopup = @Form1_MainMenu_Popup
  pForm->MainMenu.OnClick = @Form1_MainMenu_Click
  pForm->Controls.Add(ControlType.MainMenu, @(pForm->MainMenu))

  pForm->Name = "Form1"
  pForm->Text = "Mirror backup for Windows"
  pForm->StartPosition = FormStartPosition.CenterScreen
  pForm->BorderStyle = FormBorderStyle.FixedSingle
  pForm->MaximizeBox = False
  pForm->SetBounds(10,10,500,333)
  pForm->List1.Parent = pForm
  pForm->List1.Name = "List1"
  pForm->List1.ItemHeight = 16
  pForm->List1.SetBounds(26,32-nClientOffset,370,183)
  pForm->Progress1.Parent = pForm
  pForm->Progress1.Name = "Progress1"
  pForm->Progress1.SetBounds(26,233-nClientOffset,370,23)
  pForm->Label1.Parent = pForm
  pForm->Label1.Name = "Label1"
  pForm->Label1.Text = "Label1"
  pForm->Label1.SetBounds(26,268-nClientOffset,366,27)
  pForm->Controls.Add(ControlType.ListBox, @(pForm->List1))
  pForm->Controls.Add(ControlType.ProgressBar, @(pForm->Progress1))
  pForm->Controls.Add(ControlType.Label, @(pForm->Label1))
  Application.Forms.Add(ControlType.Form, pForm)
  function = 0
end function

constructor Form1Type
  InitializeComponent = cast( any ptr, @FormInitializeComponent )
  this.FormInitializeComponent( @this )
end constructor

dim shared Form1 as Form1Type

' WINFBE_CODEGEN_END

' You should always include a resource file that references a valid manifest.xml
' file otherwise your application will not properly display Windows themed controls.
' Sample resource.rc and manifest.xml files can be found in the WinFBE \Settings folder.
' The following WinFBE directive includes the resource in your application. Simply
' uncomment the line.
' If you are using WinFBE's project management features then delete the following line
' because a resource file will be generated automatically.
'     '#RESOURCE "resource.rc"


''
''  Remove the following Application.Run code if it used elsewhere in your application.
Application.Run(Form1)

#include once "Afx/CFindFile.inc"

declare sub Quetzal

Wren:
Data "C:\Decoy","E:\Decoy"

dim shared y as boolean
dim shared g as Integer
dim shared as CWSTR c,d,rm(),m

''
''
Function Form1_MainMenu_Click( ByRef sender As wfxMenuItem, ByRef e As EventArgs ) As LRESULT
  Select Case UCase(sender.Name)
    Case "MNUSTART"
      Quetzal
      'Form1.List1.Items.Add("Abcdefghijklmnopqrstuvwxyz")
      'Form1.List1.Items.Add("Abcdefghijklnmopqrstuvwxyz")
      Form1.Progress1.Value = 90
    Case Else
  End Select
  Function = 0
End Function

''
''
Function Form1_MainMenu_Popup( ByRef sender As wfxMenuItem, ByRef e As EventArgs ) As LRESULT
  Select Case UCase(sender.Name)
    Case Else
  End Select
  Function = 0
End Function

sub StatusUpdate (byref target as CWSTR)
  m = space(15 - len(m)) & m & "  " & target
  Form1.Label1.Text = ltrim(m)
  select case g
    case 1 : y = AfxCreateDirectory (d)
    case 2 : y = AfxCopyFile (c,d)
    case 3 : y = AfxMoveFile (c,d)
    case 4 : y = AfxDeleteFile (c)
    case 5 : y = AfxRemoveDirectory (c)
  end select
  Form1.List1.Items.Add(ltrim(m))
  print #3, m : m = ""  
  if g > 0 and y = false then print #3, spc(17);"Error ^"
  'print #3,spc(17);c
  'print #3,spc(17);d
end sub

sub Quetzal ()
  If Dir("E:\*") = "" Then form1.label1.Text = " Drive E was not found." : exit sub
  Using Afx
  Chdir "\"
  open "backup2.log" For Output Encoding "utf-8" As #3
  print #3,";"
  print #3, "  Mirror backup  ";date;spc(2);time
  print #3, spc(2); string(50,45)

  dim z as Boolean
  dim as Integer w,e
  Dim As Long n,k,f,fc,t(7)
  Dim As String a,b,sf(),ef()
  Dim As CFindFile p,p2
  Redim sf(100)
  redim ef(10)
  redim rm(10,3)

  'for w = 1 to 1
    read a,b
    print #3, spc(11);"From  ";a;" > ";b
    Form1.List1.Items.Add(a & " > " & B)
  'next w

  'find folders and files

  Wren2:
  restore Wren
  for w = 1 to 1
    read a,b
    if z = true then swap a,b
    sf(0) = a & "\*"
    n = -1 : k = 0
    Do Until n = k
      n = n + 1
      If p.FindFile(sf(n)) = S_OK Then
        Do
          If p.IsDots = FALSE Then
            c = p.filepath
            d = AfxStrReplace(c,a,b)
            If p.isfolder = true Then
              k = k + 1
              sf(k) = c & "\*"
              If k = Ubound(sf) Then Redim Preserve sf(k + 100)
              if AfxFolderExists(d) = false then
                if z = false then
                  m = "Create folder" : g = 1
                  t(7) = t(7) + 1
                else
                  e = e + 1 : ef(e) = c
                  if e = ubound(ef) then redim preserve ef(e + 10)
                  m = "Empty folder" : g = 0
                  d = c
                end if
                StatusUpdate d
              end if
            Else
              fc = fc + 1
              If p2.FindFile(d) = S_OK Then 'matching file found
                if p.filesize <> p2.filesize then
                  if z = false then m = "Update"
                elseIf p.lastwritetime.dwHighDateTime <> p2.lastwritetime.dwHighDateTime then
                  if z = false then m = "Up(date)"
                end if
                if m <> "" then
                  g = 2
                  StatusUpdate d
                  t(3) = t(3) + 1
                end if
              else 'no match found
                f = f + 1
                rm(f,0) = p.filesize : rm(f,1) = p.lastwritetime.dwhighdatetime
                rm(f,2) = c : rm(f,3) = d : if f = ubound(rm) then redim preserve rm(f + 100, 3)
              end if
              p2.Close
            End If
          End If
          If p.FindNext = 0 Then Exit Do
        Loop
      End If
      p.Close
    Loop
    p.Close
    t(0) = t(0) + k 'folder count
  next w
  z = not z
  if z = true then t(1) = t(0) : t(2) = fc : goto Wren2

  'for n = 1 to f
    'write #3, rm(n,0), rm(n,1), rm(n,2), rm(n,3)
  'next n

  'unmatched files
  for n = 1 to f
    for k = n + 1 to f
      if rm(n,0) = rm(k,0) then 'same size
        if rm(n,1) = rm(k,1) and left(rm(n,2),1) <> left(rm(k,2),1) then 'same date, different drive letter
          c = rm(k,2) 'source path & filename
          d = rm(n,3) 'destination path & filename
          if left(d, InStrRev(d, "\")) = left(c, InStrRev(c, "\")) then m = "renamed as" else m = "moved to"
          m = "File "& m : g = 3
          StatusUpdate d
          t(4) = t(4) + 1
          rm(k,0)= "" : rm(k,1) = "" : rm(k,2) = ""
          exit for
        end if
      end if
    next k
    if k > f then
      c = rm(n,2)
      if c <> "" then
        if left(c,1) = "C" then
          m = "Copy" : g = 2
          d = rm(n,3)
          StatusUpdate c
          t(5) = t(5) + 1
        else
          m = "Delete" : g = 4
          StatusUpdate c
          t(6) = t(6) + 1
        end if
      end if
    end if
  next n

  'folders to be removed
  g = 5
  for n = e to 1 step -1
    m = "Erase folder"
    c = ef(n)
    StatusUpdate c
  next n

  'housekeeping
  print #3, spc(2); string(50,45)
  print #3, t(1);" source folders and ";t(2);" source files found."
  print #3, t(0) - t(1);" destination folders and ";fc - t(2);" destination files found."
  print #3, t(0);" folders and ";fc;" files processed."
  print #3, t(3);" files updated"
  print #3, t(4);" files renamed or moved"
  print #3, t(5);" files copied"
  print #3, t(6);" files deleted"
  print #3, t(7);" folders created"
  print #3, e;" folders erased"
  print #3, " Net folder change: ";t(7) - e
  print #3, " Net file change: ";t(5) - t(6)
  'shell "backup2.log"
  Print #3, "Â¯\(Âº_Âº)/Â¯"
  close
end sub


