[Initial]
Message=Installiere Python...
DefVar $Version$
DefVar $ExitCode$
DefVar $AssociateFiles$
DefVar $AssociateFilesArguments$
DefVar $CreateShortcut$
DefVar $ShortcutArgument$
DefVar $PrependPath$
DefVar $PrependPathArgument$
DefVar $PackageList$

[Aktionen]
ShowBitmap "%ScriptPath%\logo.png" "MDG Python"
Sub_GetProperties
Sub_ConfigureProperties
WinBatch_Uninstall
Files_Cleanup
WinBatch_Setup
WinBatch_UpdatePip
Sub_CheckPackages
Sub_HandleExitCode

[Sub_GetProperties]
Set $Version$ = GetValue("productversion", GetProductMap)
Set $AssociateFiles$ = GetProductProperty("associatefiles", "true")
Set $CreateShortcut$ = GetProductProperty("shortcut", "true")
Set $PrependPath$ = GetProductProperty("prependpath", "true")
Set $PackageList$ = GetProductProperty("pippackages", "")

[Sub_ConfigureProperties]
if ($AssociateFiles$ = "true")
  Set $AssociateFilesArguments$ = "AssociateFiles=1"
else
  Set $AssociateFilesArguments$ = "AssociateFiles=0"
endif

if ($CreateShortcut$ = "true")
  Set $ShortcutArgument$ = "Shortcuts=1"
else
  Set $ShortcutArgument$ = "Shortcuts=0"
endif

if ($PrependPath$ = "true")
  Set $PrependPathArgument$ = "PrependPath=1"
else
  Set $PrependPathArgument$ = "PrependPath=0"
endif

[WinBatch_Uninstall]
; First uninstall Python due to an upstream issue with the installer on existing installations: https://bugs.python.org/issue41800
"%ScriptPath%\data\python-$Version$-amd64.exe" /quiet /uninstall

[Files_Cleanup]
; This ensures all data has been removed
delete -sf "%ProgramFiles64Dir%\python39\"

[WinBatch_Setup]
; https://docs.python.org/3/using/windows.html#installing-without-ui
"%ScriptPath%\data\python-$Version$-amd64.exe" /passive InstallAllUsers=1 InstallLauncherAllUsers=1 $AssociateFilesArguments$ $ShortcutArgument$ $PrependPathArgument$ /log "%opsiLogDir%\python-install.log"

[WinBatch_UpdatePip]
"%ProgramFiles64Dir%\python39\python.exe" -m pip install --upgrade pip

[Sub_CheckPackages]
if not($PackageList$ = "")
  WinBatch_Packages
endif

[WinBatch_Packages]
"%ProgramFiles64Dir%\python39\Scripts\pip.exe" install -U $PackageList$

[Sub_HandleExitCode]
; check return code
Set $ExitCode$ = GetLastExitCode
comment "GetLastExitCode: " + $ExitCode$
if not($ExitCode$ = "0" or $ExitCode$ = "3010" or $ExitCode$ = "2359302" or $ExitCode$ = "2359297")
  ; 0    ERROR_SUCCESS
  ; 3010 ERROR_SUCCESS_REBOOT_REQUIRED
  ; List of WUSA ExitCodes: https://support.microsoft.com/en-us/kb/938205
  ; 2359297 (WUSA ExitCode, Hex: 0x240001) WU_S_SERVICE_STOP
  ; 2359302 (WUSA ExitCode, Hex: 0x00240006) WU_S_ALREADY_INSTALLED
  LogError "Fatal: setup returned exit code " + $ExitCode$
  IsFatalError
endif
