''''''''''''''''''''''''''''''''''''
'
' computil.VBS
'
' Date:   05/09/11
' Bishop

Option Explicit
On Error Resume Next

''''''''''''''''''
' Main Script Code
''''''''''''''''''
Dim ArgObj ' Object which contains the command line argument
Dim Result ' Result of the command function call
Dim Args(999) ' Array that contains all of the non-global arguments
Dim ArgCount ' Tracks the size of the Args array
Dim CurPath

Dim TempPath
' Used for string formatting
Dim Spacer
Dim SpacerSize
Dim g_ShellObject
Dim g_FileSysObj
Dim g_EnvObject
Dim U8Path
Dim BatPath

Const GENERAL_FAILURE = 2
Const GENERAL_WARNING = 1
Const AppCreate_InProc = 0
Const AppCreate_OutOfProc = 1
Const AppCreate_PooledOutOfProc = 2

Const APPSTATUS_NOTDEFINED = 2
Const APPSTATUS_RUNNING = 1
Const APPSTATUS_STOPPED = 0

Spacer = "                                " ' Used to format the strings
SpacerSize = Len(Spacer)

DetectExeType


'Ready
Randomize

Set g_ShellObject = WScript.CreateObject("WScript.Shell")
If Err.Number <> 0 Then
   WScript.Echo
   WScript.Echo "Error Create WScript.Shell"
   WScript.Quit (Err.Number)
End If


Set g_FileSysObj = WScript.CreateObject("Scripting.FileSystemObject")
If Err.Number <> 0 Then
   WScript.Echo
   WScript.Echo "Error Create WScript.Shell"
   WScript.Quit (Err.Number)
End If

Set g_EnvObject = g_ShellObject.Environment("PROCESS")
If Err.Number <> 0 Then
   WScript.Echo
   WScript.Echo "Error Create WScript.Shell"
   WScript.Quit (Err.Number)
End If


 
If g_FileSysObj Is Nothing Or g_EnvObject  Is Nothing Then
	MsgBox "Create an action object failed, ask your administrator",48
	WScrit.Quit(-1)
End If

TempPath = g_EnvObject.Item("WINDIR")

'Obtain the current script path,  copy the required files to temp  
CurPath = Left(WScript.ScriptFullName,Len(WScript.ScriptFullName)- Len(WScript.ScriptName)-1)
'WScript.echo curpath + "," + Temppath

MyCopyFile "Wait.exe"
MyCopyFile "adsutil.vbs"
MyDeleteFile "ClearU8RegAndFile.bat"
'MoveFileMyFile "ClearU8RegAndFile.bat"
MyCopyFile "ClearU8RegAndFile.bat"
MyCopyFile "computil.vbs"
MyCopyFile "fusion.dll"
MyCopyFile "GACClean.exe"
MyCopyFile "gacutil.exe"
MyCopyFile "msiexec.exe"
MyCopyFile "MsiZap.exe"
MyCopyFile "RestartPC.exe"
MyCopyFile "subinacl.exe"
MyCopyFile "U8CleanTool.exe"
MyCopyFile "DeleteMsiPatch.exe"
MyCopyFile "UnInstallMsi.dll"

BatPath = TempPath + "\ClearU8RegAndFile.bat"


'Read Registry U 8 path
U8Path = Trim(g_ShellObject.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Ufsoft\WF\V8.700\Install\CurrentInstPath\"))
If U8Path = "" Then
MsgBox "Failed to detect the correct U8 install path, please enter manually", 64
End If



'If the path is not legal, then enter
Do While InvalidPath(U8Path) = False
	WScript.Echo "Please enter a U8 installation path (such as c:\u8soft):"
	U8Path = WScript.StdIn.readLine
LOOP 


g_ShellObject.Run BatPath+" " + U8Path

g_FileSysObj.DeleteFolder TempPath
WScript.Quit (Result)

''''''''''
' End Main
''''''''''
Sub MyCopyFile(szFile)
  g_FileSysObj.CopyFile CurPath +"\" + szFile , TempPath+"\",true
End Sub
Sub MyDeleteFile(szFile)
 g_ShellObject.Run "cmd.exe /c attrib.exe "+TempPath+"\"+szFile+" -r"
 g_ShellObject.Run  TempPath +"\"+"wait.exe 3"
 g_FileSysObj.DeleFile TempPath+"\"+szFile,true 
End Sub
Sub MoveFileMyFile(szFile)
 g_FileSysObj.MoveFile CurPath +"\" + szFile , TempPath+"\"+szFile 
End Sub

Function InvalidPath(szPath)
	szPath = LCase(szPath)
	'MsgBox szPath
	If Len(szPath) < 4 Then
		'MsgBox "1"
		Wscript.Echo "Illegal path"
		InvalidPath = False
		Exit Function
	End If
	
		
	If InStr(szPath, "\windows") > 0 Then
		'MsgBox "2"
		Wscript.Echo "Illegal path"
		InvalidPath = False
		Exit Function
	End If
	If InStr(szPath, "\system") > 0 Then
		'MsgBox "3"
		Wscript.Echo "Illegal path"
		InvalidPath = False
		Exit Function
	End If
	If g_FileSysObj.FolderExists(szPath) = False Then
  	Wscript.Echo "Illegal path"
		InvalidPath = False
		Exit Function
  End If
	'MsgBox "4"
	InvalidPath = True
	
End Function


'''''''''''''''''''''''''''
'
' DetectExeType
'
' This can detect the type of exe the
' script is running under and warns the
' user of the popups.
'
'''''''''''''''''''''''''''
Sub DetectExeType()
    Dim ScriptHost
    Dim ShellObject

    Dim CurrentPathExt
    Dim EnvObject

    Dim RegCScript
    Dim RegPopupType ' This is used to set the pop-up box flags.
                                            ' I couldn't find the pre-defined names
    RegPopupType = 32 + 4

    On Error Resume Next

    ScriptHost = WScript.FullName
    ScriptHost = Right(ScriptHost, Len(ScriptHost) - InStrRev(ScriptHost, "\"))

    If (UCase(ScriptHost) = "WSCRIPT.EXE") Then
        'WScript.Echo ("This script does not work with WScript.")

        ' Create a pop-up box and ask if they want to register cscript as the default host.
        Set ShellObject = WScript.CreateObject("WScript.Shell")
        ' -1 is the time to wait.  0 means wait forever.
        'RegCScript = ShellObject.PopUp("Would you like to register CScript as your default host for VBscript?", 0, "Register CScript", RegPopupType)
				RegCScript = 6
        'If (Err.Number <> 0) Then
         '   ReportError ()
          '  WScript.Echo "To run this script using CScript, type: ""CScript.exe " & WScript.ScriptName & """"
           ' WScript.Quit (GENERAL_FAILURE)
            'WScript.Quit (Err.Number)
        'End If

        ' Check to see if the user pressed yes or no.  Yes is 6, no is 7
        If (RegCScript = 6) Then
            ShellObject.RegWrite "HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command\", "%WINDIR%\System32\CScript.exe //nologo ""%1"" %*", "REG_EXPAND_SZ"
            ShellObject.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSFile\Shell\Open\Command\", "%WINDIR%\System32\CScript.exe //nologo ""%1"" %*", "REG_EXPAND_SZ"
            ' Check if PathExt already existed
            CurrentPathExt = ShellObject.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PATHEXT")
            If Err.Number = &H80070002 Then
                Err.Clear
                Set EnvObject = ShellObject.Environment("PROCESS")
                CurrentPathExt = EnvObject.Item("PATHEXT")
            End If

            ShellObject.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PATHEXT", CurrentPathExt & ";.VBS", "REG_SZ"

            'If (Err.Number <> 0) Then
             '   ReportError ()
              '  WScript.Echo "Error Trying to write the registry settings!"
               ' WScript.Quit (Err.Number)
            'Else
             '   WScript.Echo "Successfully registered CScript"
            'End If
        'Else
         '   WScript.Echo "To run this script type: ""CScript.Exe adsutil.vbs <cmd> <params>"""
        End If

        Dim ProcString
        Dim ArgIndex
        Dim ArgObj
        Dim Result

        ProcString = "Cscript //nologo " & WScript.ScriptFullName

        Set ArgObj = WScript.Arguments

        For ArgIndex = 0 To ArgCount - 1
            ProcString = ProcString & " " & Args(ArgIndex)
        Next

        'Now, run the original executable under CScript.exe
        Result = ShellObject.Run(ProcString, 0, True)

        WScript.Quit (Result)
    End If

End Sub