@echo off
ECHO ----
ECHO -----------
ECHO -------------------------------------------------------------
ECHO Instalacja aktualizacji systemu Sage Symfonia ERP (modul RDF)
ECHO -------------------------------------------------------------
ECHO -----------
ECHO ----
rem Skrypt instaluje dll z aktualnego katalogu do katalogu Sage\Symfonia ERP\25.20
rem Katalog w którym jest skrypt
set SCRIPTDIR=%~dp0
rem Katalog w jakim jestesmy w konsoli w trakcie uruchamiania skryptu
set CURRENTDIR=%CD%

rem Opis poprawki
rem TYPE %SCRIPTDIR%ReadMe.txt

rem Wykrywanie wersji Windows
for /f "tokens=4-7 delims=[.] " %%i in ('ver') do (if %%i==Version (set WIN_VER=%%j.%%k) else (set WIN_VER=%%i.%%j))
echo Wersja systemu Windows: %WIN_VER%

IF NOT EXIST "%SCRIPTDIR%Assemblies2GAC\*" GOTO InstallDirectory

ECHO -
ECHO Instalowanie assembly do GAC:
ECHO -

::list of Microsoft Windows versions.
::6.3  Windows 8.1 and Windows Server 2012 
::6.2  Windows 8 
::6.1  Windows 7 
::6.0  Windows Vista 
::5.2  Windows XP Professional x64 
::5.1  Windows XP 
::5.0  Windows 2000 
::4.10 Windows 98 

rem Wybór odpowiedniego gacutil'a
IF "%WIN_VER%"=="5.1" (
SET GACUTIL=Tools\gacutil\WinXP\gacutil.exe
) ELSE IF "%WIN_VER%"=="5.2" (
SET GACUTIL=Tools\gacutil\WinXP\gacutil.exe
) ELSE SET GACUTIL=Tools\gacutil\Win7\gacutil.exe

SET GACUTIL=%SCRIPTDIR%%GACUTIL%
rem ECHO %GACUTIL%

cd /d "%SCRIPTDIR%Assemblies2GAC\"
dir /b *.*.dll > assemblyList
rem instalowanie dll do GAC'a
"%GACUTIL%" /f /il assemblyList
IF errorlevel 1 GOTO error
del assemblyList

:InstallDirectory

REM Wykrywanie 64b vs 32b
reg Query "HKLM\Hardware\Description\System\CentralProcessor\0" | find /i "x86" > NUL && set OS=32BIT || set OS=64BIT

echo Wersja systemu Windows: %OS%

IF "%OS%"=="64BIT" (
ECHO -------------------------------------------------------------
echo RDF 2025.2
ECHO -------------------------------------------------------------
call :RegImpl "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Sage\Symfonia ERP\25.20\RDF" InstallDir ) 

IF "%OS%"=="32BIT" (
ECHO -------------------------------------------------------------
echo RDF 2025.2
ECHO -------------------------------------------------------------
call :RegImpl "HKEY_LOCAL_MACHINE\SOFTWARE\Sage\Symfonia ERP\25.20\RDF" InstallDir )

goto end


:RegImpl
REM @echo OFF

setlocal ENABLEEXTENSIONS

set KEY_NAME="%~1"
set VALUE_NAME=%~2
set VERSION="%~3"
set FOUND=0

FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
    if %%A==%VALUE_NAME% (
       Rem tutaj wiemy że zainstalowano w tej wesji
       Rem VERSION zawiera wersję (katalog) do podmiany, 
       Rem miejsce instalacji jest w %%C
       
        rem @echo Name = %%A
        rem @echo Type = %%B
        rem @echo InstallFolder = %%C 
	set FOUND=1
ECHO -------------------------------------------------------------
echo Znaleziono RDF w katalogu 
echo "%%C"	
ECHO -------------------------------------------------------------
	set MyPath=%%C
	   call :Install Install %VERSION% InstallFolder %VERSION%
    )
)
if %FOUND%==0 (
   echo Nie znaleziono RDF w katalogu "%%C"
)
echo.   
echo Nacisnij dowolny klawisz . . .
pause
exit /b
echo ##########################################################
REM -----------------------------------------------------------


:Install
echo off
REM folder z którego instalujemy
set "SourceFolder=%~1"
set "DestinationFolder=%~2"
set "ExeVersion=%~3"

REM kopiowanie dll'ek pod exe'ka aplikacji
ECHO ----
ECHO -----------
ECHO -------------------------------------------------------------
ECHO Kopiowanie plikow do katalogu aplikacji:
ECHO -------------------------------------------------------------

IF NOT EXIST "%SCRIPTDIR%\CommonFiles(x86)\*" GOTO Copy
IF "%CommonProgramFiles(x86)%"=="" SET CommonProgramFiles(x86)=%CommonProgramFiles%
xcopy "%SCRIPTDIR%\CommonFiles(x86)\*" "%CommonProgramFiles(x86)%\" /E /F /Y /R /S

:Copy
IF NOT EXIST "%SCRIPTDIR%\Dodatki\Copy\*" GOTO Resources
xcopy "%SCRIPTDIR%\Dodatki\Copy" "%MyPath%" /F /Y /R /S

:Resources
IF NOT EXIST "%SCRIPTDIR%\Dodatki\Resources\*" GOTO Reports
xcopy "%SCRIPTDIR%\Dodatki\Resources" "%MyPath%\Resources"  /F /Y /R /S

:Reports
IF NOT EXIST "%SCRIPTDIR%\Dodatki\Reports\*" GOTO ok
xcopy "%SCRIPTDIR%\Dodatki\Reports" "%MyPath%\Raporty"  /F /Y /R /S

IF errorlevel 1 GOTO error

goto ok


:error
ECHO ----
ECHO -----------
ECHO --------------------------------------------------------
ECHO EEEEEE  RRRRRR   RRRRRR    OOOOO   RRRRRR 
ECHO EE      RR   RR  RR   RR  OO   OO  RR   RR
ECHO EEEEE   RRRRRR   RRRRRR   OO   OO  RRRRRR 
ECHO EE      RR  RR   RR  RR   OO   OO  RR  RR 
ECHO EEEEEE  RR   RR  RR   RR   OOOOO   RR   RR
ECHO --------------------------------------------------------
ECHO UWAGA!
ECHO Aktualizacja wersji RDF 2025.2 NIE zostala zainstalowana.
ECHO Prosze uruchomic skrypt z prawami administratora.
ECHO --------------------------------------------------------
ECHO -----------
ECHO ----
PAUSE
EXIT /b

:ok
ECHO ----
ECHO -----------
ECHO --------------------------------------------------------
ECHO  OOOOO   KK   KK 
ECHO OO   OO  KK KK
ECHO OO   OO  KKK 
ECHO OO   OO  KK KK 
ECHO  OOOOO   KK   KK
ECHO --------------------------------------------------------
ECHO Aktualizacja dla wersji RDF 2025.2 zostala zainstalowana poprawnie
ECHO --------------------------------------------------------
ECHO -----------
ECHO ----
PAUSE
EXIT /b

:end
