; Copyright (c) uib gmbh (www.uib.de)
; This sourcecode is owned by uib
; and published under the Terms of the General Public License.
; credits: http://www.opsi.org/credits/

[Actions]
requiredWinstVersion >= "4.10.8.6"

DefVar $MsiId32$
DefVar $UninstallProgram32$
DefVar $MsiId64$
DefVar $UninstallProgram64$
DefVar $LogDir$
DefVar $ProductId$  
DefVar $MinimumSpace$
DefVar $InstallDir32$
DefVar $InstallDir64$
DefVar $ExitCode$
DefVar $LicenseRequired$
DefVar $LicenseKey$
DefVar $LicensePool$
DefVar $INST_SystemType$
DefVar $INST_architecture$

Set $INST_SystemType$ = GetSystemType
set $INST_architecture$ = GetProductProperty("install_architecture","system specific")

Set $LogDir$ = "%SystemDrive%\tmp"

DefVar	$MSVersion$
Set 	$MSVersion$ = GetMsVersionInfo
DefVar	$xp_wsus_bugfix$
Set 	$xp_wsus_bugfix$ = GetProductProperty("xp_wsus_bugfix", "1")
DefVar	$java_reinstall$
Set 	$java_reinstall$ = GetProductProperty("java_reinstall", "1")

DefVar	$extract_install$
Set 	$extract_install$ = GetProductProperty("extract_install", "0")
DefVar	$extract_use$
Set 	$extract_use$ = GetProductProperty("extract_use", "0")

DefVar	$connection_set_max_latency$
Set 	$connection_set_max_latency$ = GetProductProperty("connection_set_max_latency", "30")
DefVar	$connection_wan$
DefStringList $Output$

DefVar	$timer_value$
Set 	$timer_value$ = GetProductProperty("timer_value", "40")
DefVar	$timer_enabled$
Set		$timer_enabled$ = GetProductProperty("timer_enabled", "1")
DefVar	$inst_net1.1$
Set 	$inst_net1.1$ = GetProductProperty("inst_net1.1", "1")
DefVar 	$inst_net2.0$
Set 	$inst_net2.0$ = GetProductProperty("inst_net2.0", "1")
DefVar	$inst_net3.0$
Set 	$inst_net3.0$ = GetProductProperty("inst_net3.0", "1")
DefVar	$inst_net3.5$
Set 	$inst_net3.5$ = GetProductProperty("inst_net3.5", "1")
DefVar	$inst_net4.0$
Set 	$inst_net4.0$ = GetProductProperty("inst_net4.0", "0")
DefVar	$inst_net4.5$
Set 	$inst_net4.5$ = GetProductProperty("inst_net4.5", "1")
DefVar	$inst_msjsharp$
Set 	$inst_msjsharp$ = GetProductProperty("inst_msjsharp", "1")
DefVar	$inst_silver$
Set 	$inst_silver$ = GetProductProperty("inst_silver", "1")
DefVar	$inst_jre7$
Set 	$inst_jre7$ = GetProductProperty("inst_jre7", "1")
DefVar	$inst_AdobeShock$
Set 	$inst_AdobeShock$ = GetProductProperty("inst_AdobeShock", "1")
DefVar	$inst_basicruntimes$
Set 	$inst_basicruntimes$ = GetProductProperty("inst_basicruntimes", "1")
DefVar	$inst_msdx9$
Set 	$inst_msdx9$ = GetProductProperty("inst_msdx9", "1")
DefVar	$inst_msdxm$
Set 	$inst_msdxm$ = GetProductProperty("inst_msdxm", "1")
DefVar	$inst_msvc$
Set 	$inst_msvc$ = GetProductProperty("inst_msvc", "1")
DefVar	$inst_AdobeFlash$
Set 	$inst_AdobeFlash$ = GetProductProperty("inst_AdobeFlash", "1")

; The token BUILDER_VARIABLES will be replaced by opsi-builder.sh
; and adds the following variables:
;   from builder-product.cfg :  all variables definded by attribute WINST[index]
;   from builder-product.cfg :  VENDOR PN VERSION RELEASE PRIORITY ADVICE TYPE   
;   from opsi-builder.cfg    :  CREATOR_TAG CREATOR_NAME CREATOR_EMAIL           
;   auto generated winst-variables
;         $IconFile$: path to product picture
;
@@BUILDER_VARIABLES@@


; ----------------------------------------------------------------
; - Please edit the following values                             -
; ----------------------------------------------------------------
;$ProductId$ should be the name of the product in opsi
; therefore please: only lower letters, no umlauts, 
; no white space use '-' as a seperator
Set $ProductId$       = "sereby.aio"
Set $MinimumSpace$    = "400 MB"
; the path were we find the product after the installation
Set $InstallDir32$      = "%ProgramFiles32Dir%\aio"
Set $InstallDir64$      = "%ProgramFiles64Dir%\aio"
Set $LicenseRequired$ = "false"
Set $LicensePool$     = "p_" + $ProductId$
; ----------------------------------------------------------------

if not(HasMinimumSpace ("%SystemDrive%", $MinimumSpace$))
	LogError "Not enough space on %SystemDrive%, " + $MinimumSpace$ + " on drive %SystemDrive% needed for " + $ProductId$
	isFatalError
	; Stop process and set installation status to failed
else
	comment "Show product picture"
	ShowBitmap "%ScriptPath%\" + $ProductId$ + ".png" $ProductId$
	
;	if FileExists("%ScriptPath%\delsub.ins")
;		comment "Start uninstall sub section"
;		Sub "%ScriptPath%\delsub.ins"
;	endif
	
	if $LicenseRequired$ = "true"
		comment "Licensing required, reserve license and get license key"
		Sub_get_licensekey
	endif
	
	comment "installing"

	if (($INST_SystemType$ = "x86 System") and ($INST_architecture$ = "system specific")) or ($INST_architecture$ = "both") or ($INST_architecture$ = "32 only")
		Message "Installing " + $ProductId$ + " 32 Bit..."
		if (( $MSVersion$ = "5.1" ) and ($xp_wsus_bugfix$ = "1"))
			if (GetRegistryStringValue32("[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{C09FB3CD-3D0C-3F2D-899A-6A1D67F2073F}] DisplayName") = "")
				comment "DotNet 2.0 SP2 not found in registry, starting install dotNetFX3.5"
				Message "Installing " + "dotNetFX 2 - 3.5" + " 32 Bit..."
				Winbatch_install_dotNet_XP_2-3.5_32
				sub_check_exitcode
			endif
			if (GetRegistryStringValue32("[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{3C3901C5-3455-3E0A-A214-0B093A5070A6}] DisplayName") = "")
				comment "DotNet 4.0 not found in registry, starting install dotNetFX4 install"
				Message "Installing " + "dotNetFX 4" + " 32 Bit..."
				Winbatch_install_dotNet_XP_4_32
				sub_check_exitcode
			endif
		endif
		
		comment "Reinstall Java"
		if ($java_reinstall$ = "1")
			Message "Uninstalling " + "Java" + " 32 Bit..."
			Dosbatch_java_reinstall
		endif
		
		comment "Install the extracted version of the sereby packet on opsiserver"
		if (($extract_install$ = "1") and not (FileExists("%ScriptPath%\X86\sereby\extracted\WinPKG.exe")))
			Message "Installing " + $ProductId$ + " on depotserver..."
			Dosbatch_extract_install
		endif
		
		comment "Get connection latency between server and client"
		if ($extract_use$ = "auto")
			Message "Get connection latency..."
			Dosbatch_get_connection_latency
			Set $Output$ = getOutStreamFromSection('Dosbatch_get_connection_latency')
			Set $connection_wan$ = takeFirstStringContaining($Output$, "opsi wan mode : ")
			Set $connection_wan$ = TakeString(1, SplitString($connection_wan$, " : "))
		endif
		
		comment "Build sereby configuration"
		Message "Build sereby configuration"
		Dosbatch_build_config
		
		comment "Choose version to install"
		if (($extract_use$ = "1") or (($extract_use$ = "auto") and ($connection_wan$ = "1")))
			Message "Installing extracted " + $ProductId$ + " 32 Bit..."
			comment "Start extracted setup program"
			Dosbatch_install_extracted_32
			Sub_check_exitcode
		else
			Message "Installing " + $ProductId$ + " 32 Bit..."
			comment "Start setup program"
			Winbatch_install_32
			Sub_check_exitcode
		endif
		
		comment "Copy files"
		Files_install_32 /32Bit
		comment "Patch Registry"
		Registry_install /32Bit
		comment "Create shortcuts"
		LinkFolder_install
	endif

	if ($INST_SystemType$ = "64 Bit System") and (($INST_architecture$ = "system specific") or ($INST_architecture$ = "both") or ($INST_architecture$ = "64 only"))
		Message "Installing " + $ProductId$ + " 64 Bit..."
		if (( $MSVersion$ = "5.1" ) and ($xp_wsus_bugfix$ = "1"))
			if (GetRegistryStringValue64("[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{C09FB3CD-3D0C-3F2D-899A-6A1D67F2073F}] DisplayName") = "")
				comment "DotNet 2.0 SP2 not found in registry, starting install dotNetFX3.5"
				Message "Installing " + "dotNetFX 2 - 3.5" + " 32 Bit..."
				Winbatch_install_dotNet_XP_2-3.5_32
				sub_check_exitcode
			endif
			if (GetRegistryStringValue64("[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{3C3901C5-3455-3E0A-A214-0B093A5070A6}] DisplayName") = "")
				comment "DotNet 4.0 not found in registry, starting install dotNetFX4 install"
				Message "Installing " + "dotNetFX 4" + " 32 Bit..."
				Winbatch_install_dotNet_XP_4_32
				sub_check_exitcode
			endif
		endif
		
		comment "Reinstall Java"
		if ($java_reinstall$ = "1")
			Message "Uninstalling " + "Java" + " 32 + 64 Bit..."
			Dosbatch_java_reinstall
		endif
		
		comment "Install the extracted version of the sereby packet on opsiserver"
		if (($extract_install$ = "1") and not (FileExists("%ScriptPath%\X86\sereby\extracted\WinPKG.exe")))
			Message "Installing extracted " + $ProductId$ + " on depotserver..."
			Dosbatch_extract_install
		endif
		
		comment "Get connection latency between server and client"
		if ($extract_use$ = "auto")
			Message "Get connection latency..."
			Dosbatch_get_connection_latency
			Set $Output$ = getOutStreamFromSection('Dosbatch_get_connection_latency')
			Set $connection_wan$ = takeFirstStringContaining($Output$, "opsi wan mode : ")
			Set $connection_wan$ = TakeString(1, SplitString($connection_wan$, " : "))
		endif
		
		comment "Build sereby configuration"
		Message "Build sereby configuration"
		Dosbatch_build_config winst /64Bit
		
		comment "Choose version to install"
		if (($extract_use$ = "1") or (($extract_use$ = "auto") and ($connection_wan$ = "1")))
			Message "Installing extracted " + $ProductId$ + " 64 Bit..."
			comment "Start extracted setup program"
			Dosbatch_install_extracted_32
			Sub_check_exitcode
		else
			Message "Installing " + $ProductId$ + " 64 Bit..."
			comment "Start setup program"
			Winbatch_install_64
			Sub_check_exitcode
		endif
		
		comment "Copy files"
		Files_install_64 /64Bit
		comment "Patch Registry"
		Registry_install /64Bit
		comment "Create shortcuts"
		LinkFolder_install
	endif
	
	comment "Reboot"
	ExitWindows /Reboot

endif

[Winbatch_install_dotNet_XP_2-3.5_32]
"$Install32_XP1_Exe$"

[Winbatch_install_dotNet_XP_4_32]
"$Install32_XP2_Exe$" /ain

[Dosbatch_java_reinstall]
cscript /nologo "%ScriptPath%\X86\java\JavaUninstallScript.vbs" /keeponly

[Dosbatch_extract_install]
@echo off &setlocal
"$Install32Exe$" -gm0 -nr
echo Ident opsi Server
echo .
for /f "tokens=2 delims=\" %%a in ('net use p:') do set "opsiservername=%%a"
echo opsi Server: %opsiservername%
@echo off
net use x: \\%opsiservername%\opt_pcbin
net use /delete p:
net use p: \\%opsiservername%\opt_pcbin\install
xcopy "%SystemDrive%\AiO-Files\*.*" "%ScriptPath%\X86\sereby\extracted\" /S /E /D /Y
rem rd "%SystemDrive%\AiO-Files" /S /Q
net use /delete p:
net use p: \\%opsiservername%\opsi_depot
net use /delete x:

[Dosbatch_get_connection_latency]
@echo off 
setlocal enabledelayedexpansion 
echo Ident opsi Server
echo .
for /f "tokens=2 delims=\" %%a in ('net use p:') do set "opsiservername=%%a"
echo opsi Server   : %opsiservername%
for /f "tokens=4 delims='='" %%a in ('ping %opsiservername% -n 10') do set "opsiserverping=%%a"
set Latenzms=!opsiserverping:~1,10!
echo Latenz in ms  : %Latenzms%
for /f "tokens=1 delims=m" %%a in ('@echo %Latenzms%') do set "Latenz=%%a"
echo Latenz        : %Latenz%
if %Latenz% GTR $connection_set_max_latency$ (set wan=1) else (set wan=0)
@echo opsi wan mode : %wan%

[Dosbatch_build_config]
copy "%ScriptPath%\X86\winpkg\WinPKG.xml" "%System%"

copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/timer[@value=40]/@value" -v $timer_value$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/timer[@enabled=1]/@enabled" -v $timer_enabled$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='net1.1']/@checked" -v $inst_net1.1$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='net2.0']/@checked" -v $inst_net2.0$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='net3.0']/@checked" -v $inst_net3.0$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='net3.5']/@checked" -v $inst_net3.5$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='net4.0']/@checked" -v $inst_net4.0$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='net4.5']/@checked" -v $inst_net4.5$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='msjsharp']/@checked" -v $inst_msjsharp$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='silver']/@checked" -v $inst_silver$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='jre7']/@checked" -v $inst_jre7$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='AdobeShock']/@checked" -v $inst_AdobeShock$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='basicruntimes']/@checked" -v $inst_basicruntimes$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='msdx9']/@checked" -v $inst_msdx9$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='msdxm']/@checked" -v $inst_msdxm$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='msvc']/@checked" -v $inst_msvc$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"
copy "%System%\WinPKG.xml" "%System%\WinPKG.xml.old"
xml ed -O -u "/settings/set[@component='AdobeFlash']/@checked" -v $inst_AdobeFlash$ "%System%\WinPKG.xml.old" > "%System%\WinPKG.xml"

[Dosbatch_install_extracted_32]
@echo off
p:
cd "%ScriptPath%\X86\sereby\extracted"
"$Install_extracted_32Exe$"

[Winbatch_install_32]
; Choose one of the following examples as basis for your installation
; You can use $LicenseKey$ var to pass a license key to the installer
;
; === Nullsoft Scriptable Install System ================================================================
"$Install32Exe$"

[Files_install_32]
; Example of recursively copying some files into the installation directory:
;
; copy -s "%ScriptPath%\files\*.*" "$InstallDir32$"

[Dosbatch_install_extracted_64]
@echo off
p:
cd "%ScriptPath%\X86\sereby\extracted"
"$Install_extracted_64Exe$"

[Winbatch_install_64]
; Choose one of the following examples as basis for your installation
; You can use $LicenseKey$ var to pass a license key to the installer
;
; === MSI package =======================================================================================
"$Install64Exe$"

[Files_install_64]
; Example of recursively copying some files into the installation directory:
;
; copy -s "%ScriptPath%\files\*.*" "$InstallDir64$"

[Registry_install]
; Example of setting some values of an registry key:
;
; openkey [HKEY_LOCAL_MACHINE\Software\$ProductId$]
; set "name1" = "some string value"
; set "name2" = REG_DWORD:0001
; set "name3" = REG_BINARY:00 af 99 cd

[LinkFolder_install]
; Example of deleting a folder from AllUsers startmenu:
;
; set_basefolder common_programs
; delete_subfolder $ProductId$
;
; Example of creating an shortcut to the installed exe in AllUsers startmenu:
;
; set_basefolder common_programs
; set_subfolder $ProductId$
;
; set_link
; 	name: $ProductId$
; 	target: <path to the program>
; 	parameters:
; 	working_dir: $InstallDir$
; 	icon_file:
; 	icon_index:
; end_link
;
; Example of creating an shortcut to the installed exe on AllUsers desktop:
;
; set_basefolder common_desktopdirectory
; set_subfolder ""
;
; set_link
; 	name: $ProductId$
; 	target: <path to the program>
; 	parameters: <some_param>
; 	working_dir: $InstallDir$
; 	icon_file: <path to icon file>
; 	icon_index: 2
; end_link

[Sub_get_licensekey]
if opsiLicenseManagementEnabled
	comment "License management is enabled and will be used"

	comment "Trying to get a license key"
	Set $LicenseKey$ = demandLicenseKey ($LicensePool$)
	; If there is an assignment of exactly one licensepool to the product the following call is possible:
	; Set $LicenseKey$ = demandLicenseKey ("", $ProductId$)
	;
	; If there is an assignment of a license pool to a windows software id, it is possible to use:
	; DefVar $WindowsSoftwareId$
	; $WindowsSoftwareId$ = "..."
	; Set $LicenseKey$ = demandLicenseKey ("", "", $WindowsSoftwareId$)
	
	DefVar $ServiceErrorClass$
	set $ServiceErrorClass$ = getLastServiceErrorClass
	comment "Error class: " + $ServiceErrorClass$
	
	if $ServiceErrorClass$ = "None"
		comment "Everything fine, we got the license key '" + $LicenseKey$ + "'"
	else
		if $ServiceErrorClass$ = "LicenseConfigurationError"
			LogError "Fatal: license configuration must be corrected"
			LogError getLastServiceErrorMessage
			isFatalError
		else 
			if $ServiceErrorClass$ = "LicenseMissingError"
				LogError "Fatal: required license is not supplied"
				isFatalError
			endif
		endif
	endif
else
	LogError "Fatal: license required, but license management not enabled"
	isFatalError
endif


[Sub_check_exitcode]
comment "Test for installation success via exit code"
set $ExitCode$ = getLastExitCode
; informations to exit codes see
; http://msdn.microsoft.com/en-us/library/aa372835(VS.85).aspx
; http://msdn.microsoft.com/en-us/library/aa368542.aspx
if ($ExitCode$ = "0")
	comment "Looks good: setup program gives exitcode zero"
else
	comment "Setup program gives a exitcode unequal zero: " + $ExitCode$
	if ($ExitCode$ = "1605")
		comment "ERROR_UNKNOWN_PRODUCT	1605	This action is only valid for products that are currently installed."
		comment "Uninstall of a not installed product failed - no problem"
	else
		if ($ExitCode$ = "1641")
			comment "looks good: setup program gives exitcode 1641"
			comment "ERROR_SUCCESS_REBOOT_INITIATED	1641	The installer has initiated a restart. This message is indicative of a success."
		else
			if ($ExitCode$ = "3010")
				comment "looks good: setup program gives exitcode 3010"
				comment "ERROR_SUCCESS_REBOOT_REQUIRED	3010	A restart is required to complete the install. This message is indicative of a success."
			else
				logError "Fatal: Setup program gives an unknown exitcode unequal zero: " + $ExitCode$
				isFatalError
			endif
		endif
	endif
endif

