CurrentScriptFolder = Left(Wscript.ScriptFullName, Len(Wscript.ScriptFullName) - Len(Wscript.ScriptName))
if instr(CurrentScriptFolder," ") then
   wscript.echo CurrentScriptFolder & vbcrlf & " The MybookRescue folder Full Path must not contain any Spaces ' ' ." & vbcrlf & " Please, copy the MybookRescue Folder in a correct location, for example : C:\MybookRescue" & vbcrlf & vbcrlf & " Then try again"
   wscript.quit
end if


Set oShell = CreateObject("WScript.Shell")

OS_VERSION = oShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\CurrentVersion")

if OS_VERSION <> "5.1" then
  if IsElevated=false then

    Set objShell = CreateObject("Shell.Application")
    objShell.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """" & " uac", "", "runas", 1
    wscript.quit
  end if
  

end if



'Ensure we are in a Administrator Mode , for Windows Vista /  Windows Seven
'If WScript.Arguments.length =0 Then
'End If



set WshShell = WScript.CreateObject("WScript.Shell")
Set objetFSO = CreateObject("Scripting.FileSystemObject")

test= DisplayGUI("1_Welcome.htm")
if test ="CANCEL" then wscript.quit

CheckDataFiles "FilesDescr"

test=DisplayGUI("2_Select.htm")
if test <> "NEXT" then wscript.quit

GUI_Mode=WshShell.Regread ("HKCU\SOFTWARE\HTA-UI\MODE")

if GUI_Mode = "RETRY" and test="NEXT" then
	Msgbox " No MybookWorld Bricked disk  Found . ==> " & vbcrlf & "Please insert a bricked Mybook world Hardrive, then try again."
        wscript.quit
end if

' A disk has been selected : ensure that it is a correct on that looks to be a MybookWorld Hard Drive
GUI_DiskIndex=WshShell.Regread ("HKCU\SOFTWARE\HTA-UI\HDD")
if IsAMybookHardDrive(GUI_DiskIndex) = true then

        WshShell.RegWrite "HKCU\SOFTWARE\HTA-UI\Selected_HDD",GUI_DiskIndex
	test=DisplayGUI("3_Confirm.htm")
	if test <> "NEXT" then wscript.quit
		RestoreDisk(GUI_DiskIndex)
		
else
	Msgbox "Unfortunately, this Disk does not seems to be a Mybook Hardrive. ==> " & vbcrlf & "Please insert a bricked Mybook world Hardrive, then try again."
	wscript.quit
end  if


sub HTAEXEC(CMDLINE)
  WshShell.run "mshta.exe  """ & CurrentScriptFolder & "ask-UI.hta"" """ & CMDLINE & "",0,True
end Sub



Function DisplayGUI(GUIFile)

'***********************  User interface   **************************
 WshShell.RegWrite "HKCU\SOFTWARE\HTA-UI\ASK-UI_BTN1","   CANCEL   "
 WshShell.RegWrite "HKCU\SOFTWARE\HTA-UI\ASK-UI_BTN2","    NEXT    "
 WshShell.RegWrite "HKCU\SOFTWARE\HTA-UI\AutoRetry","false"
 WshShell.RegWrite "HKCU\SOFTWARE\HTA-UI\ASK-UI_DisplayMode","NORMAL"
 WshShell.RegWrite "HKCU\SOFTWARE\HTA-UI\ASK-UI_HEIGHT","400"
 WshShell.RegWrite "HKCU\SOFTWARE\HTA-UI\ASK-UI_WIDTH","600"
 WshShell.RegWrite "HKCU\SOFTWARE\HTA-UI\UI_BTN",""

 'Calling HTA user interface
 HTAEXEC CurrentScriptFolder & GUIFile

 'Retrieving results
 on error resume next
 Button=WshShell.Regread ("HKCU\SOFTWARE\HTA-UI\UI_BTN")	
 on error goto 0

 if (Button = "NEXT" ) then
  DisplayGUI="NEXT"  
 else
  DisplayGUI="CANCEL"  
 end if

end function


function IsAMybookHardDrive(HDDindex)
   
  IsAMybookHardDrive=true

end function

sub CheckDataFiles(FilesDescr)
	WshShell.RegWrite "HKEY_CURRENT_USER\SOFTWARE\SplashMsg\CurrentDisplayText", "Checking Files ..."
	WshShell.Run  "mshta.exe " & CurrentScriptFolder & "ProgressBar\ProgressBar.hta",0,False

'	File_List_url="http://192.168.0.5:8082/Mybook_HDD/FileList.txt"
'	wget_str=CurrentScriptFolder & "\wget\wget " & File_List_url & " -O " & CurrentScriptFolder & "\Data\FileList.descr"
'	WshShell.Run wget_str,0,False

	sFile=ReadAllFile( CurrentScriptFolder & "\Data\FileList.descr" )


	arrFiles = Split(sFile, vbcrlf)
	TotalNbFile=4
	CurNbFile=1
	Err_Message=""
	
	For each FileLine in arrFiles
		arrFileInfo=Split(FileLine, ",")
		FileName=arrFileInfo(0)
		FileSize=arrFileInfo(1)

		SplashMsg "checking Files ..."
		SplashSetPercentage CurNbFile,TotalNbFile
		CurNbFile=CurNbFile+1
		err.clear
		on error resume next
		Set Myfile = objetFSO.GetFile(CurrentScriptFolder & "\Data\" & FileName )
		if err then
			Found_Error=TRUE
			Err_Message=Err_Message & vbcrlf & "ERROR : Data\" & FileName & " file is not present."
		else
			if ucase(Myfile.size) <> ucase(FileSize) then
				'msgbox Myfile.size & "="  & FileSize
				Found_Error=TRUE
				Err_Message=Err_Message & vbcrlf & "ERROR : Data\" & FileName & " file has a wrong Size. (" & Myfile.size & ")"	
			end if
			
		end if
		on error goto 0


		wscript.sleep 1000



	Next

	SplashMsg("EXIT")

	if Found_Error=TRUE then
		msgbox "Errors have ben encountered during File Checking : " & Err_Message & vbcrlf & " Operation Cancelled " 
		wscript.quit
	end if

end sub

sub RestoreDisk(GUI_DiskIndex)

	run_str=CurrentScriptFolder & "dd\dd.exe bs=1 skip=24064 count=416 if=\\?\Device\Harddisk" & GUI_DiskIndex & "\Partition0 of=" & CurrentScriptFolder & "data\MacAddress.dd --progress"
   	WshShell.Run run_str,1,true

	wget_str=CurrentScriptFolder & "dd\dd.exe of=\\?\Device\Harddisk" & GUI_DiskIndex & "\Partition2 if=" & CurrentScriptFolder & "data\Part2a.dd --progress"
	WshShell.Run wget_str,1,true

	wget_str=CurrentScriptFolder & "dd\dd.exe of=\\?\Device\Harddisk" & GUI_DiskIndex & "\Partition1 if=" & CurrentScriptFolder & "data\Part1a.dd --progress"
	WshShell.Run wget_str,1,true

	wget_str=CurrentScriptFolder & "dd\dd.exe of=\\?\Device\Harddisk" & GUI_DiskIndex & "\Partition3 if=" & CurrentScriptFolder & "data\Part3a.dd --progress"
	WshShell.Run wget_str,1,true

	run_str=CurrentScriptFolder & "dd\dd.exe bs=1 skip=24064 count=416 of=\\?\Device\Harddisk" & GUI_DiskIndex & "\Partition0 if=" & CurrentScriptFolder & "data\MacAddress.dd --progress"
   	WshShell.Run run_str,1,true

	Msgbox "Partitions images have been applied"
end sub

'------------------------------------------------------------------------------
' Function SplashMsg
'------------------------------------------------------------------------------
Sub SplashMsg(sDisplay)
	WshShell.RegWrite "HKEY_CURRENT_USER\SOFTWARE\SplashMsg\CurrentDisplayInfo" ,sDisplay
End Sub

Sub SplashMsg2(sDisplay)
	WshShell.RegWrite "HKEY_CURRENT_USER\SOFTWARE\SplashMsg\CurrentDisplayInfo2" ,sDisplay
End Sub

Sub SplashSetPercentage(CurValue,MaxValue)
	WshShell.RegWrite "HKCU\SOFTWARE\SplashMsg\PercentMaxValue" ,MaxValue
	WshShell.RegWrite "HKCU\SOFTWARE\SplashMsg\PercentCurrentValue" ,CurValue
End Sub

function ReadAllFile(FILE_PATH)


'FILE_IO Modes Const
  ForReading=1
  ForWriting=2
  ForAppending=8

'  on error resume next
  Set objFile=objetFSO.OpenTextFile(FILE_PATH,ForReading)
  ReadAllFile=objfile.ReadAll
  objFile.Close
  on error goto 0
  set  objFile=nothing
End function




function IsElevated()
 Set oExec = oShell.Exec("whoami /groups") 
 	      
 Do While (oExec.Status = cnWshRunning)    
 WScript.Sleep 100   
 if not oExec.StdOut.AtEndOfStream then       
 szStdOut = szStdOut & oExec.StdOut.ReadAll   
 end if
 Loop 

select case oExec.ExitCode
   case 0
       if not oExec.StdOut.AtEndOfStream then
           szStdOut = szStdOut & oExec.StdOut.ReadAll
       end if
       if instr(szStdOut,"S-1-16-12288") Then
		IsElevated=true
       else
           if instr(szStdOut,"S-1-16-8192")  Then
		IsElevated=false
           else
                'wscript.echo "Unknown!"
		IsElevated=false

           end if
       end if
   case else
       if not oExec.StdErr.AtEndOfStream then
'            wscript.echo oExec.StdErr.ReadAll
       end if

end select

end function


