@echo off
setlocal EnableDelayedExpansion
set LOG_FILE=%TEMP%\uninstall_pacs.log

echo %~dp0 " " %TEMP%   >> "%LOG_FILE%"
if not "%~dp0" =="%TEMP%\" (
    echo Start copy to tmp... >> "%LOG_FILE%"
    copy "%~f0" "%TEMP%\uninstall_server.bat" || exit 1
    echo Start uninstall... >> "%LOG_FILE%"
    cmd /c "%TEMP%\uninstall_server.bat" %1

    exit /b 0
)

echo Running uninstall script from temp location. >> "%LOG_FILE%"

echo [%date% %time%] Uninstall script started with args: %* >> "%LOG_FILE%"

if not "%1"=="/uninstall" (
    echo ERROR: This script must be called with /uninstall argument. Manual execution is not allowed. >> "%LOG_FILE%"
    exit /b 1
)

echo Checking privileges... >> "%LOG_FILE%"
openfiles >nul 2>&1
if %errorlevel% neq 0 (
    echo [%date% %time%] ERROR: No admin privileges. >> "%LOG_FILE%"
    exit /b 1
)

set "ProgName=Inobitec DICOM Server (PACS)"
set "RegPath=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%ProgName%"

for /f "tokens=2*" %%a in ('reg query "%RegPath%" /v InstallLocation 2^>nul ^| find "InstallLocation"') do set DEST_DIR=%%b
for /f "tokens=2*" %%a in ('reg query "%RegPath%" /v ServiceName 2^>nul ^| find "ServiceName"') do set SERVICE_NAME=%%b

echo Registry values: >> "%LOG_FILE%"
if defined DEST_DIR (echo - InstallLocation = "!DEST_DIR!" >> "%LOG_FILE%") else (echo - InstallLocation not found >> "%LOG_FILE%")
if defined SERVICE_NAME (echo - ServiceName = "!SERVICE_NAME!" >> "%LOG_FILE%") else (echo - ServiceName not found >> "%LOG_FILE%")

rem Удаляем кавычки из прочитанных значений (reg query иногда возвращает строку в "...")
if defined DEST_DIR set "DEST_DIR=!DEST_DIR:"=!"
if defined SERVICE_NAME set "SERVICE_NAME=!SERVICE_NAME:"=!"

if not defined DEST_DIR (
    echo InstallLocation not found in registry. Skipping directory removal. >> "%LOG_FILE%"
    set SKIP_DIR=1
) else if "!DEST_DIR!"=="" (
    echo InstallLocation empty in registry. Skipping directory removal. >> "%LOG_FILE%"
    set SKIP_DIR=1
) else (
    set SKIP_DIR=0
)

if not defined SERVICE_NAME (
    echo ServiceName not found in registry. Skipping service operations. >> "%LOG_FILE%"
    set SKIP_SERVICE=1
) else if "!SERVICE_NAME!"=="" (
    echo ServiceName empty in registry. Skipping service operations. >> "%LOG_FILE%"
    set SKIP_SERVICE=1
) else (
    set SKIP_SERVICE=0
)

if !SKIP_SERVICE! equ 0 (
    echo Stopping service "!SERVICE_NAME!"... >> "%LOG_FILE%"
    sc stop "!SERVICE_NAME!" >nul 2>&1
    if !errorlevel! equ 0 (
        echo Service stopped successfully. >> "%LOG_FILE%"
    ) else (
        echo Service not running or failed to stop. >> "%LOG_FILE%"
    )

    echo Deleting service "!SERVICE_NAME!"... >> "%LOG_FILE%"
    sc delete "!SERVICE_NAME!" >nul 2>&1
    if !errorlevel! equ 0 (
        echo Service deleted successfully. >> "%LOG_FILE%"
    ) else (
        echo Failed to delete service. >> "%LOG_FILE%"
    )
)

if !SKIP_DIR! equ 0 (
    echo. >> "%LOG_FILE%"
    echo Removing installation directory "!DEST_DIR!"... >> "%LOG_FILE%"
    if exist "!DEST_DIR!" (
        echo Directory exists. Starting removal... >> "%LOG_FILE%"
        del /f /s /q "!DEST_DIR!" >nul 2>&1
        rmdir /s /q "!DEST_DIR!" >nul 2>&1
        if !errorlevel! equ 0 (
            echo Installation directory removed successfully. >> "%LOG_FILE%"
        ) else (
            echo Failed to remove installation directory. >> "%LOG_FILE%"
        )
    ) else (
        echo Installation directory does not exist. >> "%LOG_FILE%"
    )
)

echo. >> "%LOG_FILE%"
echo Removing registry entry... >> "%LOG_FILE%"
reg delete "!RegPath!" /f >> "%LOG_FILE%" 2>&1
if !errorlevel! equ 0 (
    echo Registry entry removed successfully. >> "%LOG_FILE%"
) else (
    echo Failed to remove registry entry. >> "%LOG_FILE%"
)

echo. >> "%LOG_FILE%"
echo [%date% %time%] Uninstallation completed successfully. >> "%LOG_FILE%"

exit /b 0