@echo off

SETLOCAL

:: this version
set VERSION=DIGSI 4.95 Hotfix 1

:: the DIGSI4 version to be updated
set PREVIOUS_VERSION=4.95

:: ReadMe file name, see below

title Installing %VERSION% ...

:: cmd.exe does not support UNC paths, see Microsoft knowledge base Q156276.
:: If the batch file is started from a network path
:: the current directory defaults to the Windows directory. 
:: If this is the case, we abort this batch file here.
:: %cd% returns the current directory
if #"%cd%"#==#"%windir%"# goto ERROR_UNC

:: When a batch file is started via "Run as administrator" under Windows Vista
:: the current directory is C:\Windows\system32 
:: (not the directory where the batch file is located as it is the case under Win XP)
:: to change directory: %0 is the path of this batch as given
:: %~f0 is always the full path and filename of this batch
:: %~d0 is the drive (d), e.g. d:, %~dp0 is the drive (d) and path (p) part 

:: change drive
%~d0
:: change directory
CD %~dp0

:: under Win 7 SP1, Win 10 and higher server OS: check necessary user rights to write into "Common Files" and into registry
:: When a batch file is started via "Run as administrator" under Windows Vista

VER | find /i "6.0." > nul
IF %ERRORLEVEL% EQU 0 GOTO CHECKRIGHTS

:: WinServer 2008 R2: VER = 6.1.7601
:: Win7 SP1 (32Bit): VER = 6.1.7601
VER | find /i "6.1." > nul
IF %ERRORLEVEL% EQU 0 GOTO CHECKRIGHTS

GOTO CONTINUE1

:CHECKRIGHTS

:: if running on Win 7 SP1, Win 10 and higher server OS:
:: a normal user, even when he is an administrator, runs this batch with 
:: security level Medium (Mittel), SID=S-1-16-8192
:: to write into "Common Files" and into the registry we need at least
:: security level High (Hoch), SID=S-1-16-12288
whoami /user /groups | find "S-1-16-12288" > nul
if errorlevel 1 GOTO ERROR_INSUFFICIENT_USER_RIGHTS

:CONTINUE1

echo Installing %VERSION% ...
echo.
echo Please read the ReadMe file carefully that came with %VERSION%.
echo ------
pause

:: SCN debugging
::@echo on


:: check if DIGSI4 is installed (if Siemens\DIGSI4\USERINFO exists in the registry) 

:: the registry key for 32 bit Windows 
set REG_SIEMENS=HKLM\SOFTWARE\WOW6432Node\Siemens

REG QUERY "%REG_SIEMENS%\DIGSI4\USERINFO" >nul 2>nul
if not errorlevel 1 goto READ_DIGSIVERSION


:: the registry key for 64 bit Windows 
set REG_SIEMENS=HKLM\SOFTWARE\Siemens

REG QUERY "%REG_SIEMENS%\DIGSI4\USERINFO" >nul 2>nul
if not errorlevel 1 goto READ_DIGSIVERSION


:: Siemens\DIGSI4\USERINFO not found
echo ------
echo HKLM\SOFTWARE\Siemens\DIGSI4\USERINFO
echo or 
echo HKLM\SOFTWARE\WOW6432Node\Siemens\DIGSI4\USERINFO
echo cannot be read from the registry.
echo Please check your DIGSI 4 installation.
::pause
::goto ERROR_REGISTRY_ERROR
goto END1

:READ_DIGSIVERSION

:: detect DIGSI 4 version
:: delims is a TAB followed by a space
::FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "HKLM\SOFTWARE\Siemens\DIGSI4\USERINFO" /v VERSION') DO SET DIGVER=%%B
FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "%REG_SIEMENS%\DIGSI4\USERINFO" /v VERSION') DO SET DIGVER=%%B
SET SHORTVER=%DIGVER:~0,4%

:: SCN debugging
::@echo DIGVER='%DIGVER%'
::pause
::goto END1

if %SHORTVER% == %PREVIOUS_VERSION% (
	goto CONTINUE2
) else (
	echo %VERSION% can only be used to update DIGSI V%PREVIOUS_VERSION%. 
	echo It shall not be applied to the installed DIGSI V%DIGVER:~0,4%.
	pause
	goto END1
)

:: :CONTINUE2

:::: check if DotNetFramework is installed
::REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework" >nul 2>nul
::if errorlevel 1 (
::	echo Error reading HKLM\SOFTWARE\Microsoft\.NETFramework
::	goto ERROR_DOTNETFRAMEWORK_ERROR
::)

:::: detect path of DotNetFramework installation
:::: delims is a TAB followed by a space
::FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework" /v InstallRoot') DO SET FRAMEWORKPATH=%%B

:::: we need e.g. C:\WINNT\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe 
:::: to register PrintManager2
::for /D %%i in (%FRAMEWORKPATH%v2"."*) do (
::	set REGASM=%%i\RegAsm.exe 
::	goto CONTINUE2
::)
::echo.
::echo %FRAMEWORKPATH%v2.* not found
::goto ERROR_DOTNETFRAMEWORK_ERROR

:CONTINUE2

::echo ------
::echo Installing DIGSI 4.83 Patch 2
::echo ------
::pause
::call BATCH\PatchDrivers.bat

:: reset title (has been changed by PatchDrivers.bat)
title Installing %VERSION% ...

:: detect Step7 Manager installation directory (the path of s7bin\S7tgtopx.exe)
:: default is
:: C:\Siemens\Step7\s7bin if Step7 full version is used
:: C:\Siemens\Digsi4\Manager\s7bin if DIGSI 4 OEM version of Step7 is used
:: delims is a TAB followed by a space
FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "%REG_SIEMENS%\AUTSW\STEP7" /v Path') DO SET ManagerPath=%%B\
if #"%ManagerPath%"#==#""# goto ERROR_NO_MANAGERPATH
if #"%ManagerPath%"#==#"\"# goto ERROR_NO_MANAGERPATH

:: detect DIGSI4 installation directory (the path of Digsi4.exe)
:: default is C:\Siemens\Digsi4\
FOR /F "tokens=2* delims=	 " %%A IN ('REG QUERY "%REG_SIEMENS%\DIGSI4\PROGRAMME" /v Digsi') DO SET DigsiPath=%%B
if #"%DigsiPath%"#==#""# goto ERROR_NO_DIGSIPATH

:: SCN debugging
::@echo ManagerPath='%ManagerPath%'
::@echo DigsiPath='%DigsiPath%'
::pause


:: add Reydisp Manager menu entries
::REG IMPORT REG\p7diiedx.reg
:: delete DIGSI Manager cache
::del "%ManagerPath%\S7MET\*.dat"

:: copy all supplied files
::xcopy /I /E /Y /F /R DIGSI4\*.*	"%DigsiPath%"
xcopy /I /E /Y /F /R MANAGER\*.*	"%ManagerPath%"
:: xcopy /I /E /Y /F /R COMMONFILES\*.*	"%CommonProgramFiles%"

:: change directory
::CD %~dp0\BATCH

:: register PrintManager2
::call RegisterPrintManager.bat

:: change directory
::CD %~dp0

:: write date, time and what is installed into a textfile
echo ---	>>"%DigsiPath%"\Updates.txt
date /t	>>"%DigsiPath%"\Updates.txt
time /t	>>"%DigsiPath%"\Updates.txt
echo %VERSION%	>>"%DigsiPath%"\Updates.txt

:: display the product information file 
:: (using the default PDF reader application)
echo ------
set /P choice=Display Product Info with the default PDF reader program? (y/n)
if /i "%choice%"=="y" goto:SHOW_PRODUCTINFO
goto END1

:SHOW_PRODUCTINFO

DIGSI_4-V4.95_HF1_Readme.pdf

goto END1


:ERROR_UNC
echo ------
echo Solution: copy %VERSION% into a directory on your local PC or
echo assign a drive letter to the network drive via "net use" and start from there
goto END1

:ERROR_NO_MANAGERPATH
echo ------
echo Could not read path of Step 7 Manager from the registry
echo Please check your installation of Step 7 and/or DIGSI 4
goto END1

:ERROR_NO_DIGSIPATH
echo ------
echo Could not read DIGSI 4 path from the registry
echo Please check your DIGSI 4 installation
goto END1

:ERROR_REGISTRY_ERROR
echo Registry access failed.
goto END1

:ERROR_DOTNETFRAMEWORK_ERROR
echo %VERSION% needs at least Microsoft .Net Framework 2.0
echo Please check your DotNetFramework installation. 
goto END1

:ERROR_INSUFFICIENT_USER_RIGHTS
echo Insufficient user rights for the current Windows version.
echo Please run this batch file via 
echo right click - "Run as administrator"
echo even if you already are an administrator.
goto END1

:END1

echo ------
echo End of %VERSION% installation
echo ------
pause

