@set __COMPAT_LAYER=RunAsInvoker
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights
:::::::::::::::::::::::::::::::::::::::::
@echo off
CLS 
ECHO.
ECHO =============================
ECHO Running Admin shell
ECHO =============================

:checkPrivileges 
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges ) 

:getPrivileges 
if '%1'=='ELEV' (shift & goto gotPrivileges)  
ECHO. 
ECHO **************************************
ECHO Invoking UAC for Privilege Escalation 
ECHO **************************************

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" 
ECHO UAC.ShellExecute "!batchPath!", "ELEV", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs" 
"%temp%\OEgetPrivileges.vbs" 
exit /B 

:gotPrivileges 
::::::::::::::::::::::::::::
:START
::::::::::::::::::::::::::::
setlocal & pushd .

:: ---------------------------------------------------------------------------
:: Settings

set MuiFileName=winhlp32.exe.mui
set ExeFileName=winhlp32.exe
goto :StartMod

:: ---------------------------------------------------------------------------
:: Displaying installer information

echo --------------------------------------------------------------------
echo   Windows Help program (WinHlp32) installer for Windows 10 / 8.1 
echo   May 10, 2016
echo   Command-line scripting for Windows 10 / 8.1
echo --------------------------------------------------------------------
echo.

:StartMod

:: Preserve current directory for further referrings.
set InitialDirectory=%cd%
:: Change drive and path to where the batch file was started from. This is
:: required when your batch file is run-as-administrator, because it sets
:: System32 the working (current) directory.
cd /d "%0\.."

:Install
echo.
echo Terminating all possible running instances of WinHlp32...
taskkill /f /im %ExeFileName% /t
:: No error checking is required, because if it's not opened so far, an error will occur.
echo.

set lang=de-DE
if not exist "%SystemRoot%\%lang%\*.*" goto :en-US
echo Taking the ownership of system's existing %MuiFileName%...
takeown /f "%SystemRoot%\%lang%\%MuiFileName%"
echo.
echo Granting %UserName% full-access permission to system's existing %MuiFileName%...
icacls "%SystemRoot%\%lang%\%MuiFileName%" /grant "%UserName%":F
echo.
echo Copying %lang%\%MuiFileName% to system...
xcopy /r /y /h /q .\%lang%\%MuiFileName% "%SystemRoot%\%lang%"
echo.

:en-US
set lang=en-US
if not exist "%SystemRoot%\%lang%\*.*" goto :fr-FR
echo Taking the ownership of system's existing %MuiFileName%...
takeown /f "%SystemRoot%\%lang%\%MuiFileName%"
echo.
echo Granting %UserName% full-access permission to system's existing %MuiFileName%...
icacls "%SystemRoot%\%lang%\%MuiFileName%" /grant "%UserName%":F
echo.
echo Copying %lang%\%MuiFileName% to system...
xcopy /r /y /h /q .\%lang%\%MuiFileName% "%SystemRoot%\%lang%"
echo.

:fr-FR
set lang=fr-FR
if not exist "%SystemRoot%\%lang%\*.*" goto :it-IT
echo Taking the ownership of system's existing %MuiFileName%...
takeown /f "%SystemRoot%\%lang%\%MuiFileName%"
echo.
echo Granting %UserName% full-access permission to system's existing %MuiFileName%...
icacls "%SystemRoot%\%lang%\%MuiFileName%" /grant "%UserName%":F
echo.
echo Copying %lang%\%MuiFileName% to system...
xcopy /r /y /h /q .\%lang%\%MuiFileName% "%SystemRoot%\%lang%"
echo.

:it-IT
set lang=it-IT
if not exist "%SystemRoot%\%lang%\*.*" goto :es-ES
echo Taking the ownership of system's existing %MuiFileName%...
takeown /f "%SystemRoot%\%lang%\%MuiFileName%"
echo.
echo Granting %UserName% full-access permission to system's existing %MuiFileName%...
icacls "%SystemRoot%\%lang%\%MuiFileName%" /grant "%UserName%":F
echo.
echo Copying %lang%\%MuiFileName% to system...
xcopy /r /y /h /q .\%lang%\%MuiFileName% "%SystemRoot%\%lang%"
echo.

:es-ES
set lang=es-ES
if not exist "%SystemRoot%\%lang%\*.*" goto :cs-CZ
echo Taking the ownership of system's existing %MuiFileName%...
takeown /f "%SystemRoot%\%lang%\%MuiFileName%"
echo.
echo Granting %UserName% full-access permission to system's existing %MuiFileName%...
icacls "%SystemRoot%\%lang%\%MuiFileName%" /grant "%UserName%":F
echo.
echo Copying %lang%\%MuiFileName% to system...
xcopy /r /y /h /q .\%lang%\%MuiFileName% "%SystemRoot%\%lang%"
echo.

:cs-CZ
set lang=cs-CZ
if not exist "%SystemRoot%\%lang%\*.*" goto :xx-XX
echo Taking the ownership of system's existing %MuiFileName%...
takeown /f "%SystemRoot%\%lang%\%MuiFileName%"
echo.
echo Granting %UserName% full-access permission to system's existing %MuiFileName%...
icacls "%SystemRoot%\%lang%\%MuiFileName%" /grant "%UserName%":F
echo.
echo Copying %lang%\%MuiFileName% to system...
xcopy /r /y /h /q .\%lang%\%MuiFileName% "%SystemRoot%\%lang%"
echo.

:xx-XX

if errorlevel 1 goto :Error

echo Taking the ownership of system's existing %ExeFileName%...
takeown /f "%SystemRoot%\%ExeFileName%"
echo.
echo Granting %UserName% full-access permission to system's existing %ExeFileName%...
icacls "%SystemRoot%\%ExeFileName%" /grant "%UserName%":F
echo.
echo Copying %ExeFileName% to system...
xcopy /r /y /h /q %ExeFileName% "%SystemRoot%"
echo.
if errorlevel 1 goto :Error

echo Updating Registry entries...
reg add "HKLM\SOFTWARE\Microsoft\WinHelp" /v "AllowProgrammaticMacros" /t REG_DWORD /d "0x00000001" /f
reg add "HKLM\SOFTWARE\Microsoft\WinHelp" /v "AllowIntranetAccess" /t REG_DWORD /d "0x00000001" /f
if %PROCESSOR_ARCHITECTURE%==AMD64 (
  reg add "HKLM\SOFTWARE\Wow6432Node\Microsoft\WinHelp" /v "AllowProgrammaticMacros" /t REG_DWORD /d "0x00000001" /f
  reg add "HKLM\SOFTWARE\Wow6432Node\Microsoft\WinHelp" /v "AllowIntranetAccess" /t REG_DWORD /d "0x00000001" /f
)
echo.
if errorlevel 1 goto :Error

pause
goto :eof

:: ---------------------------------------------------------------------------
:: Error management

:Error
echo Error installing WinHlp32
echo.
echo Setup will now exit.
echo 
pause
goto :eof
