Option Explicit
Const TeamViewerVersion = "6.0.9947"

Dim Shell, FSO, msiPath
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Shell = WScript.CreateObject("WScript.Shell")

''''
'''''''''''' DO NOT EDIT ABOVE THIS LINE ''''''''''''
''''
msiPath = FSO.GetParentFolderName (WScript.ScriptFullName) & "\"  'Pfad aus Script-Namen extrahieren - Get path from script name!
'msiPath = "\\MSIPATH\"  'Pfad zur Netzfreigabe mit MSI-Packet  -   Path to network share containing the MSI files

Const TvModuleName = "teamviewer.msi"
'Const TvModuleName = "teamviewer_host.msi"

Const Regfilename32  = "tv-regfile32.reg"  'Reg-Datei von TeamViewer für 32-bit Systeme  -  Reg file for TeamViewer on 32-bit systems
Const Regfilename64  = "tv-regfile64.reg"  'Reg-Datei von TeamViewer für 64-bit Systeme  -  Reg file for TeamViewer on 64-bit systems

Const FORCEINSTALLATION = false			   'TeamViewer immer Installieren/Aktuallisieren -  install or update TeamViewer allways  
''''
'''''''''''' DO NOT EDIT BELOW THIS LINE ''''''''''''
''''

Dim Install_regfilename32, Install_regfilename64, Install_regfilename, VersionCompare, UninstallRegDir

Install_regfilename32  = chr(34) & msipath & Regfilename32 & chr(34)  'Reg-Datei von TeamViewer für 32-bit Systeme  -  Reg file for TeamViewer on 32-bit systems
Install_regfilename64  = chr(34) & msipath & Regfilename64 & chr(34)  'Reg-Datei von TeamViewer für 64-bit Systeme  -  Reg file for TeamViewer on 64-bit systems

CheckPlatform										' first of all check Plattform!!!
VersionCompare = CheckInstalledTeamViewerVersion	' check current installed version

If ((VersionCompare = 1) Or FORCEINSTALLATION) Then
	WriteRegistry Install_regfilename	' import registry settings
End If

If (VersionCompare = 1) Then 
	InstallTeamViewer	' install newer or very first version
Else 
	If (VersionCompare = 0 And FORCEINSTALLATION) Then 
		RepareTeamViewer	' reinstall same version 
	End If
End If


' Return Values
' -1 eine ältere Version -  Older version
' 0 Gleiche Version      -  Equal version
' 1 neuere Version oder nichts installiert.  -  Newer Version or not yet installed
Function CheckInstalledTeamViewerVersion()
	Dim objReg, arrSubkeys, strValue, installedVersionNumber, strSubkey, intRet
	CheckInstalledTeamViewerVersion = 1		'Assume newer version
	Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
	
	Set objReg = GetObject("winmgmts://./root/default:StdRegProv")    
	objReg.EnumKey HKLM, UninstallRegDir, arrSubkeys


	For Each strSubkey In arrSubkeys
		intRet = objReg.GetStringValue(HKLM, UninstallRegDir & strSubkey, "DisplayName", strValue)        
		If (intRet = 0) Then
			If strValue = "TeamViewer 6 (MSI Wrapper)" Or strValue = "TeamViewer 6 Host (MSI Wrapper)" Then
				objReg.GetStringValue HKLM, UninstallRegDir & strSubkey, "DisplayVersion", InstalledVersionNumber
				'MsgBox(InstalledVersionNumber)
				If ( cint(Mid(TEAMVIEWERVERSION,5,Len(TEAMVIEWERVERSION)-4)) < cint(Mid(installedVersionNumber,5,Len(InstalledVersionNumber)-4)) ) Then
					CheckInstalledTeamViewerVersion = -1	'older version				
				End If
				If ( cint(Mid(TEAMVIEWERVERSION,5,Len(TEAMVIEWERVERSION)-4)) = cint(Mid(installedVersionNumber,5,Len(InstalledVersionNumber)-4)) ) Then
					CheckInstalledTeamViewerVersion = 0		'equal verion								
				End If
			End If
		End If    
	Next
End Function


'Überprüfung der aktuellen Plattform
Function CheckPlatform()
	Dim regCheck, platform
	
	RegCheck = shell.regread("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")

	'x86
	platform=InStr(RegCheck,"86")
	If platform >= 1 Then
		Install_regfilename = Install_regfilename32
		UninstallRegDir = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
	End If
		
	'x64
	platform=InStr(RegCheck,"64")
	If platform >= 1 Then
		Install_regfilename = Install_regfilename64
		UninstallRegDir = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
	End If
End Function

'Schreiben der TeamViewer Registry-Keys
Function WriteRegistry(regfilename)
	Shell.Run "regedit /s " & regfilename, 0,True
End Function

Function InstallTeamViewer()
	'MsgBox("Install")
	Shell.Run "msiexec.exe /i " & chr(34) & msiPath & TvModuleName & chr(34) & " /quiet", 0,True
End Function

Function RepareTeamViewer()
	'MsgBox("Repair")
	'Shell.Run "msiexec.exe /fa " & chr(34) & msiPath & TvModuleName & chr(34) & " /quiet", 0,True
	Shell.Run "msiexec.exe /i " & chr(34) & msiPath & TvModuleName & chr(34) & " /quiet REINSTALL=ALL REBOOT=ReallySuppress", 0,True
End Function

