Option Explicit
Dim args, msi, fso, shell, product, match, backup, cache, log, db, result, chosen, fixed, table, rows
Set args = WScript.Arguments
If args.Count < 1 Then WScript.Quit 64
Set msi = CreateObject("WindowsInstaller.Installer")
Set fso = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")
match = ""
For Each product In msi.Products
    If msi.ProductInfo(product, "ProductName") = "VMware Tools" Then
        If match <> "" Then
            WScript.Echo "ERROR multiple VMware Tools product codes"
            WScript.Quit 2
        End If
        match = product
    End If
Next
If match = "" Then
    WScript.Echo "VMWARE TOOLS ABSENT"
    WScript.Quit 0
End If
cache = msi.ProductInfo(match, "LocalPackage")
WScript.Echo "VMWARE TOOLS " & msi.ProductInfo(match, "VersionString") & " " & match
WScript.Echo "MSI CACHE " & cache
If Not fso.FileExists(cache) Or InStr(1,cache,shell.ExpandEnvironmentStrings("%WINDIR%") & "\Installer\",vbTextCompare) <> 1 Then
    WScript.Echo "ERROR cached package unavailable or unexpected path"
    WScript.Quit 2
End If
If args(0) = "audit" Then
    Set db = msi.OpenDatabase(cache, 0)
    For Each table In Array("InstallExecuteSequence", "InstallUISequence")
        Audit db, table
    Next
    WScript.Echo "MSI AUDIT DONE"
    WScript.Quit 0
End If
If args(0) <> "remove" Or args.Count <> 2 Then WScript.Quit 64
chosen = args(1)
If chosen <> "VM_CheckRequirements" And chosen <> "none" Then WScript.Quit 64
fixed = False
If chosen <> "none" Then
    backup = shell.ExpandEnvironmentStrings("%WINDIR%") & "\Temp\UTM-VMwareTools-" & fso.GetFileName(cache)
    If fso.FileExists(backup) Then
        WScript.Echo "ERROR backup already exists; inspect before retry"
        WScript.Quit 2
    End If
    Set db = msi.OpenDatabase(cache, 0)
    rows = CountAction(db, "InstallExecuteSequence", chosen)
    Set db = Nothing
    If rows <> 1 Then
        WScript.Echo "ERROR expected one exact execute action, found " & rows
        WScript.Quit 2
    End If
    fso.CopyFile cache, backup, False
    Set db = msi.OpenDatabase(cache, 1)
    Patch db, chosen
    db.Commit
    Set db = Nothing
    fixed = True
    WScript.Echo "PLATFORM CHECK DISABLED FOR UNINSTALL " & chosen
    WScript.Echo "ORIGINAL CACHE " & backup
End If
log = shell.ExpandEnvironmentStrings("%WINDIR%") & "\Temp\UTM-VMwareTools-Uninstall.log"
result = shell.Run("msiexec.exe /x " & match & " /qb! REBOOT=ReallySuppress /L*V " & Chr(34) & log & Chr(34), 1, True)
WScript.Echo "VMWARE UNINSTALL EXIT " & result
If result <> 0 And result <> 3010 And fixed Then
    If fso.FileExists(cache) Then
        fso.CopyFile backup, cache, True
        WScript.Echo "ORIGINAL MSI RESTORED"
    End If
End If
WScript.Quit result

Sub Audit(database, name)
    Dim cv, rec, key
    Set cv = database.OpenView("SELECT `Action`, `Condition`, `Sequence` FROM `" & name & "`")
    cv.Execute
    Set rec = cv.Fetch
    Do Until rec Is Nothing
        key = rec.StringData(1)
        If InStr(1,key,"Check",vbTextCompare) > 0 Or InStr(1,key,"Requirement",vbTextCompare) > 0 Or key = "VM_LogStart" Or key = "LaunchConditions" Then
            WScript.Echo Left(name,11) & " " & rec.IntegerData(3) & " " & key & " | " & rec.StringData(2)
        End If
        Set rec = cv.Fetch
    Loop
    cv.Close
End Sub

Function CountAction(database, name, key)
    Dim cv, rec, count
    Set cv = database.OpenView("SELECT `Action` FROM `" & name & "` WHERE `Action`='" & key & "'")
    cv.Execute
    count = 0
    Set rec = cv.Fetch
    Do Until rec Is Nothing
        count = count + 1
        Set rec = cv.Fetch
    Loop
    cv.Close
    CountAction = count
End Function

Sub Patch(database, key)
    Dim cv
    Set cv = database.OpenView("UPDATE `InstallExecuteSequence` SET `Condition`='0' WHERE `Action`='" & key & "'")
    cv.Execute
    cv.Close
End Sub
