
Dim WshShell
Dim wmi,os,value,sha
Dim execPath 
Dim lngPos

lngPos = InStr(WScript.ScriptFullName, "install.vbs")
execPath = Left(WScript.ScriptFullName, lngPos-1)  

do while WScript.Arguments.Count=0 and WScript.Version>=5.7
  'Check if Vista
  Set wmi = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
  Set os = wmi.ExecQuery ("SELECT * FROM Win32_OperatingSystem")
  For Each value in os
    if left(value.Version,3)<6.0 then exit do 'Exit if not Vista
  Next

  'Run this script as admin.
  Set sha=CreateObject("Shell.Application")
  sha.ShellExecute "wscript.exe",""""+WScript.ScriptFullName+""" vista","","runas"

  WScript.Quit
loop

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.CurrentDirectory = execPath 

Dim objFS
Set objFS = CreateObject("Scripting.FileSystemObject")

'---- Check & Uninstall Old Version Software -----------------------------

strHost = "."
Const HKLM = &H80000002
Set objReg = GetObject("winmgmts://" & strHost & _
    "/root/default:StdRegProv")

If UCase(WshShell.Environment("Process").Item("PROCESSOR_ARCHITECTURE")) = "X86" Then
   strBaseKey = _
       "Software\Microsoft\Windows\CurrentVersion\Uninstall\"
Else
   strBaseKey = _
       "Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
End If

objReg.EnumKey HKLM, strBaseKey, arrSubKeys

numOfSoftware = 2
strSoftwareArr = Array( "FACT Helper", _
			"Was Helper")
						
intVersionArr = Array( &H01000001, _
			&H01000004)

intAppNum = 0

For Each strSubKey In arrSubKeys

    intRet = objReg.GetStringValue(HKLM, strBaseKey & strSubKey, _
        "DisplayName", strName)
        
    If intRet <> 0 Then
        intRet = objReg.GetStringValue(HKLM, strBaseKey & strSubKey, _
        "QuietDisplayName", strName)
    End If

    intValue = 123
    If (strName <> "") and (intRet = 0) Then
	For intAppNum =0 To numOfSoftware-1
    		lngPos = InStr(1, strName, strSoftwareArr(intAppNum), 0)
		IF( lngPos <> 0 ) Then
            intRet = objReg.GetDwordValue(HKLM, strBaseKey & strSubKey, _
        			"Version",  intValue)

			If intValue < intVersionArr(intAppNum) Then
				intRet = objReg.GetStringValue(HKLM, strBaseKey & strSubKey, _
        			"UninstallString", strValue)
        		WScript.Echo "Uninstall " & strName 
				ResultVal = WshShell.Run( "MsiExec.exe /passive /x " & strSubKey, 1,1 )
        	End If
			'WScript.Echo strValue
			intAppNum =  intAppNum +1       		        	
        	End If
	Next
    End If

Next

'---- Install Helper ---------------------------------------------

If ( objFS.FileExists( "C:\Program Files\TOYOTA Factory Management System\FactHelper.exe" ) Or objFS.FileExists( "C:\Program Files (x86)\TOYOTA Factory Management System\FactHelper.exe" ) ) Then

  Wscript.Echo "FactHelper is already installed."

Else

  ResultVal = WshShell.Run( "msiexec /i ""FactHelper-1.01.msi""", 1,1 )

  If ResultVal <> 0 Then
    Wscript.Quit
  End If

End If

If ( objFS.FileExists( "C:\Program Files\TOYOTA Weave Assist System\WasHelper.exe" ) Or objFS.FileExists( "C:\Program Files (x86)\TOYOTA Weave Assist System\WasHelper.exe" ) ) Then

  Wscript.Echo "WasHelper is already installed."

Else

  ResultVal = WshShell.Run( "msiexec /i ""WasHelper-v1.04.msi""", 1,1 )

  If ResultVal <> 0 Then
    Wscript.Quit
  End If

  ResultVal = WshShell.Run( "RegChk_washlp.vbs", 1,1 )

  If ResultVal <> 0 Then
    Wscript.Quit
  End If
End If

'---- Install T810 viewer -------------------------------------------
TargetDir = "C:\JAT810"
If objFS.FolderExists( TargetDir ) Then
  objFS.DeleteFolder TargetDir, True
End If
TargetDir = "C:\T810"
If objFS.FolderExists( TargetDir ) Then
  objFS.DeleteFolder TargetDir, True
End If

File = WshShell.CurrentDirectory & "\" & "T810.zip"
ExtractFolder = "C:\"
With CreateObject("Shell.Application")
	.NameSpace(ExtractFolder).CopyHere .NameSpace(File).Items
End With

ResultVal = WshShell.Run( "C:\T810\jat810.reg", 1,1 )
ResultVal = WshShell.Run( "C:\T810\lwt810.reg", 1,1 )

If ResultVal <> 0 Then
  Wscript.Quit
End If


Wscript.Echo "The installation of all programs ended."
