CurrentScriptFolder = Left(Wscript.ScriptFullName, Len(Wscript.ScriptFullName) - Len(Wscript.ScriptName))

if instr(CurrentScriptFolder," ") then
   wscript.echo CurrentScriptFolder & vbcrlf & " The MybookRecscue 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



'Ensure we are in a Administrator Mode , for Windows Vista /  Windows Seven
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


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

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

GUI_Mode="RETRY"
HDD=""

do while ( HDD = "" and test="NEXT" )
  WshShell.RegWrite "HKCU\SOFTWARE\HTA-UI\HDD",""
  test=DisplayGUI("MybookFormat_2_Select.htm")
  HDD=WshShell.Regread ("HKCU\SOFTWARE\HTA-UI\HDD")
loop

if test <> "NEXT" then wscript.quit


' 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")

WshShell.RegWrite "HKCU\SOFTWARE\HTA-UI\Selected_HDD",GUI_DiskIndex
test=DisplayGUI("MybookFormat_3_Confirm.htm")
if test <> "NEXT" then wscript.quit


HDD_STRING=WshShell.Regread ("HKCU\SOFTWARE\HTA-UI\HDD_STRING")
if  HDD_STRING = "\\?\Device\Harddisk" & GUI_DiskIndex  & "\Partition0" then
   FormatDisk(GUI_DiskIndex)
else
   msgbox " unexpected error occured : HDD_STRING is not consistent. operation is cancelled."
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","500"
 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


sub FormatDisk(HDD_STRING)
	exec_str = CurrentScriptFolder & "dd\dd.exe of=\\?\Device\Harddisk" & GUI_DiskIndex & "\Partition0 if=" & CurrentScriptFolder & "data\fullbootsect.sda bs=512 count=48194 --progress"
        msgbox exec_str

'	exec_str = CurrentScriptFolder & "dd\dd.exe of=" & HDD_STRING & " if=" & CurrentScriptFolder & "data\fullbootsect.sda bs=512 count=48194 --progress"
'        msgbox exec_str

	WshShell.Run exec_str,1,true
	Msgbox "Mybook Partitions  have been created" & vbcrlf  & " Please, unplug the hard Drive, then plug it back , or reboot the computer if you use a sata cable " & vbcrlf & " then apply the Mybook Rescue Procedure"
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


