#include <GUIConstants.au3>
#include <File.au3>
#Include <process.au3>
Global $button1
Global $button2
Global $handle1
Global $USBDRIVE
Global $NOSPACE
Global $CHKDSKLOG
Global $CHKDSKLOGtxt
Global $buttonOK
Global $button_not_ok
Global $USBOK

Assign_USB()

Func Create_Window()
	$handle1 = GUICreate("MY GUI",1024,768,0,0, $WS_POPUP)  ; will create a dialog box that when displayed is centered
	$button1 = GUICtrlCreateButton ("Check logs", 422, 200, 180, 100)
	$button2 = GUICtrlCreateButton ( "Save Data",  422, 350, 180, 100)
	GUISetState ()       ; will display a dialog box
EndFunc

Func Close_Window()
	GuiDelete($handle1)
	GUISetState () 
EndFunc

Func Assign_USB()
	For $iDrive=67 To 90
		$sDrive=Chr($iDrive) & ":"
		If(DriveGetFileSystem($sDrive)<>"") Then
			If(DriveGetType($sDrive)<>"" And DriveGetType($sDrive)=="Removable") Then
				$USBDRIVE=$sDrive
;				MsgBox(4096,"USB-Drive:", $USBDRIVE)
			EndIf
		EndIf
	Next
EndFunc

Func Check_USB_Space()
	Assign_USB()
	Check_USB()
	Local $USBFREESPACE = Round(DriveSpaceFree($USBDRIVE)) ; calculate free space on USB memory
;	determine the space of C:\Data
	Local $USERDATASPACE = Round(DirGetSize("C:\Data") / 1024 / 1024) ; calculate space of C:\Data
	If $USBFREESPACE < $USERDATASPACE Then	; Check whether USBmemory has enough space, if not
		$DIFF = ($USERDATASPACE - $USBFREESPACE) ; calculate needed space
		MsgBox(0, "WARNING", "USB memory does NOT have enough space for saving Userdata!" & @CR & "Please use a higher capacity USB memory or make additional " & $DIFF & " MB free space on it")
		MsgBox(0, "SPACE INFO", "The size of directory C:\Data is " & $USERDATASPACE & " MB. " & @CR & "Missing space on USB memory is: " & $DIFF & " MB.")
		$NOSPACE = 1 ; Not enough spae on USB memory
	Else
		$NOSPACE = 0 ; enough space on USB memory
	EndIf
EndFunc

Func Check_USB()
	If ($USBDRIVE == "") Then 
		MsgBox(4096,"WARNING", "NO USB MEMORY FOUND!")
	Else
		$USBOK = 1
	EndIf
EndFunc

Func Save_Data()
	Assign_USB()
	If FileExists($USBDRIVE & "\SYSBACKUP") = 0 then
		DirCreate($USBDRIVE & "\SYSBACKUP") 
	EndIf
	FileCopy("C:\Data" , $USBDRIVE & "\SYSBACKUP",1)
EndFunc

Func OK()
	$handle5 = GUICreate("TEST GUI",1024,768,0,0, $WS_POPUP)
	GUISetBkColor (0x00E0FFFF)
	GUISetFont(18, 400, -1)
	GUICtrlCreateLabel("Tests completed without any failure.", 210, 150, 600, 100, 0x01)
	GUICtrlCreateLabel("(Click the button below to continue)", 260, 185, 500, 100, 0x01)
	$buttonOK = GuiCtrlCreateButton("", 375, 250, 270, 270,$BS_BITMAP)
	GUICtrlSetImage (-1, "C:\Programs\OK.bmp")
	GUISetState ()       ; will display a dialog box
	While 1
		$msg = GUIGetMsg()
		If $msg = $buttonOK Then
			GUIDelete($handle5)
			Sleep(100)
		EndIf
	WEnd
EndFunc

Func not_ok()
	$handle6 = GUICreate("TEST GUI",1024,768,0,0, $WS_POPUP)
	GUISetBkColor (0x00E0FFFF)
	GUISetFont(18, 400, -1)
	GUICtrlCreateLabel("Tests completed with failure!", 210, 130, 600, 100, 0x01)
	GUICtrlCreateLabel("(Click the button below to continue.)", 260, 200, 500, 100, 0x01)
	$button_not_ok = GuiCtrlCreateButton("", 375, 250, 270, 270,$BS_BITMAP)
	GUICtrlSetImage (-1, "C:\Programs\not-ok.bmp")
	GUISetState ()       
	While 1
		$msg = GUIGetMsg()
		If $msg = $button_not_ok Then
			GuiDelete($handle6)
			Sleep(100)
		EndIf
	WEnd
EndFunc	

Func check_all()
	If $USBOK = 1 And $NOSPACE = 0 Then
		OK()
	Else
		not_ok()
	EndIf
EndFunc

; MAIN PROGRAM
Create_Window()
Assign_USB()
While 1
    $msg = GUIGetMsg()
	If $msg = $button1 then 
		Close_Window()
		Assign_USB()
		Check_USB()
		If ($USBDRIVE <> "") Then
			Check_USB()
			Sleep(100)
			Check_USB_Space()
			Sleep(100)
			check_all()
		EndIf
		Create_Window()	
	EndIf

;	DELETE SYSDATA
	If $msg = $button2 then 
		Close_Window()
		Assign_USB()
		Save_Data()
		MsgBox(0,"INFO", "Data saved")
		Create_Window()
	EndIf
Wend
