@echo off
setlocal EnableDelayedExpansion

echo.
echo ========================================
echo Inobitec DICOM Server (PACS) Installation
echo ========================================
echo.

openfiles >nul 2>&1
if %errorlevel% neq 0 (
    echo This script requires administrator privileges.
    echo Please run Command Prompt as administrator.
    pause
    exit /b 1
)

set CONFIG_DIR=C:\ProgramData\Inobitec
set CONFIG_FILE=%CONFIG_DIR%\Inobitec DICOM Server (PACS).ini

:enter_db_type
echo.
echo Select database type:
echo 1 - MySQL
echo 2 - PostgreSQL  
echo.
set DB_TYPE=
set /p DB_TYPE="Enter database type [1-2]: "

if "!DB_TYPE!"=="1" (
  set DB_DRIVER=QMYSQL
  set DEPLOY_UTIL=inobitec-dicom-server-mysql-util.exe
) else if "!DB_TYPE!"=="2" (
  set DB_DRIVER=QPSQL
  set DEPLOY_UTIL=inobitec-dicom-server-postgres-util.exe
) else (
  echo Invalid database type. Please specify "1" for MySQL, "2" for PostgreSQL.
  goto enter_db_type
)

set DB_HOST=
set /p DB_HOST="Enter database host ip [127.0.0.1]: "
if "!DB_HOST!"=="" set DB_HOST=127.0.0.1

rem Default port depends on database type
if "!DB_TYPE!"=="1" (
  set "DEFAULT_DB_PORT=3306"
) else (
  set "DEFAULT_DB_PORT=5432"
)

set DB_PORT=
set /p DB_PORT="Enter database port [!DEFAULT_DB_PORT!]: "
if "!DB_PORT!"=="" set DB_PORT=!DEFAULT_DB_PORT!

set DB_NAME=
set /p DB_NAME="Enter database name [pacs_db]: "
if "!DB_NAME!"=="" set DB_NAME=pacs_db

set DB_USER=
set /p DB_USER="Enter database username [pacs_user]: "
if "!DB_USER!"=="" set DB_USER=pacs_user

:enter_db_pass
set DB_PASS=
set /p "DB_PASS=Enter database user password: "
if "!DB_PASS!"=="" (
  echo The password can not be empty
  goto enter_db_pass
)

if not exist "%CONFIG_DIR%" (
  mkdir "%CONFIG_DIR%"
)

echo [General] > "%CONFIG_FILE%"
echo DB_ADDRESS=!DB_HOST! >> "%CONFIG_FILE%"
echo DB_PORT=!DB_PORT! >> "%CONFIG_FILE%"
echo DB_NAME=!DB_NAME! >> "%CONFIG_FILE%"
echo DB_USER=!DB_USER! >> "%CONFIG_FILE%"
echo DB_PASSWORD=!DB_PASS! >> "%CONFIG_FILE%"
echo DB_DRIVER_NAME=!DB_DRIVER! >> "%CONFIG_FILE%"
echo DB_MAX_CONNECTION=100 >> "%CONFIG_FILE%"
 
echo Environment file for connection to database created at "%CONFIG_FILE%"

set DEST_DIR=
set /p DEST_DIR="Enter the path to the folder to install the Inobitec DICOM Server (PACS) [C:\InobitecDicomServer]: "
if "!DEST_DIR!"=="" set "DEST_DIR=C:\InobitecDicomServer"
if exist "!DEST_DIR!" (
  echo The directory !DEST_DIR! is already exists.
  echo Please uninstall the current version of 'Inobitec DICOM Server' as per the instructions prior to installing the new version.
  goto failed
)

if exist "%~dp0\..\bin\vc_redist.x64.exe" (
  echo Installing Microsoft Visual C++ Redistributable...
  start /wait "" "%~dp0\..\bin\vc_redist.x64.exe" /install /passive /norestart /log "%TEMP%\vc_redist_install.log"
  set "VC_REDIST_EXIT_CODE=!ERRORLEVEL!"
  if "!VC_REDIST_EXIT_CODE!"=="0" (
    echo Microsoft Visual C++ Redistributable installed successfully.
  ) else if "!VC_REDIST_EXIT_CODE!"=="3010" (
    echo Microsoft Visual C++ Redistributable installed successfully. Reboot is required.
  ) else if "!VC_REDIST_EXIT_CODE!"=="1602" (
    echo Microsoft Visual C++ Redistributable installation was canceled by the user. Continuing deployment.
  ) else if "!VC_REDIST_EXIT_CODE!"=="1638" (
    echo Microsoft Visual C++ Redistributable is already installed or a newer version is present.
  ) else (
    echo [ERROR] Microsoft Visual C++ Redistributable installation failed with exit code !VC_REDIST_EXIT_CODE!.
    goto failed
  )
) else (
  echo [ERROR] "%~dp0\..\bin\vc_redist.x64.exe" not found.
  goto failed
)

echo Copying files...

if exist "%~dp0\..\bin" (
  if not exist "!DEST_DIR!\bin" mkdir "!DEST_DIR!\bin"
  xcopy /E /Y /I /H /R "%~dp0\..\bin\*" "!DEST_DIR!\bin\"
  if ERRORLEVEL 1 goto copy_files_failed
) else (
  echo Error: "%~dp0\..\bin" folder not found. 
  cd
  goto failed
)

if exist "%~dp0\..\certs" (
  if not exist "!DEST_DIR!\certs" mkdir "!DEST_DIR!\certs"
  xcopy /E /Y /I /H /R "%~dp0\..\certs\*" "!DEST_DIR!\certs\"
  if ERRORLEVEL 1 goto copy_files_failed
) else (
  echo Error: "%~dp0\..\certs" folder not found, creating empty certs folder
  goto failed
)

if exist "%~dp0\..\docs" (
  if not exist "!DEST_DIR!\docs" mkdir "!DEST_DIR!\docs"
  xcopy /E /Y /I /H /R "%~dp0\..\docs\*" "!DEST_DIR!\docs\"
  if ERRORLEVEL 1 goto copy_files_failed
) else (
  echo Error: "%~dp0\..\docs" folder not found
  goto failed
)

if exist "%~dp0\..\admin" (
  if not exist "!DEST_DIR!\admin" mkdir "!DEST_DIR!\admin"
  xcopy /E /Y /I /H /R "%~dp0\..\admin\*" "!DEST_DIR!\admin\"
  if ERRORLEVEL 1 goto copy_files_failed
) else (
  echo Error: "%~dp0\..\admin" folder not found
  goto failed
)

if exist "%~dp0\..\scripts" (
  if not exist "!DEST_DIR!\scripts" mkdir "!DEST_DIR!\scripts"
  xcopy /E /Y /I /H /R "%~dp0\..\scripts\*" "!DEST_DIR!\scripts\"
  if ERRORLEVEL 1 goto copy_files_failed
) else (
  echo Error: "%~dp0\..\scripts" folder not found
  goto failed
)

if not exist "!DEST_DIR!\bin\inobitec-dicom-server.exe" (
  echo Error: inobitec-dicom-server.exe not found in !DEST_DIR!\bin\
  echo Available files:
  dir "!DEST_DIR!\bin\" /b
  goto failed
)

set SERVICE_NAME=
set /p SERVICE_NAME="Enter the Inobitec DICOM Server (PACS) service name [InobitecDicomServer]: "
if "!SERVICE_NAME!"=="" set "SERVICE_NAME=InobitecDicomServer"

echo Installing service with name: !SERVICE_NAME!

set "ProgName=Inobitec DICOM Server (PACS)"
set "DisplayName=Inobitec DICOM Server (PACS)"
set "UninstallStr=\"%DEST_DIR%\scripts\uninstall_server.bat\" /uninstall"
set "DisplayIcon=\"%DEST_DIR%\bin\inobitec-dicom-server.exe\",0"
set "DisplayVersion=2.11.0"
set "Publisher=Inobitec"

set "RegPath=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%ProgName%"
reg add "%RegPath%" /v "DisplayName" /t REG_SZ /d "%DisplayName%" /f
reg add "%RegPath%" /v "UninstallString" /t REG_SZ /d "%UninstallStr%" /f
reg add "%RegPath%" /v "DisplayIcon" /t REG_SZ /d "%DisplayIcon%" /f
reg add "%RegPath%" /v "DisplayVersion" /t REG_SZ /d "%DisplayVersion%" /f
reg add "%RegPath%" /v "Publisher" /t REG_SZ /d "%Publisher%" /f
reg add "%RegPath%" /v "NoModify" /t REG_DWORD /d 1 /f
reg add "%RegPath%" /v "NoRepair" /t REG_DWORD /d 1 /f
reg add "%RegPath%" /v "InstallLocation" /t REG_SZ /d "!DEST_DIR!" /f
reg add "%RegPath%" /v "ServiceName" /t REG_SZ /d "!SERVICE_NAME!" /f


echo Adding service...
sc query !SERVICE_NAME! >nul 2>&1
if !errorlevel! equ 0 (
    echo Service !SERVICE_NAME! already exists.
    choice /m "Stop and delete existing service"
    if "!ERRORLEVEL!"=="1" (
        sc stop !SERVICE_NAME! >nul 2>&1
        sc delete !SERVICE_NAME! >nul 2>&1
        timeout /t 2 /nobreak >nul
    ) else (
        echo Please choose another service name.
        goto failed
    )
)
sc create "!SERVICE_NAME!" binPath= "\"!DEST_DIR!\bin\inobitec-dicom-server.exe\"" start= auto DisplayName= "Inobitec DICOM Server (PACS)"
if ERRORLEVEL 1 goto add_service_failed
rem Устанавливаем описание службы, чтобы было видно в msconfig/services.msc
sc description "!SERVICE_NAME!" "Inobitec DICOM Server (PACS) - DICOM server service"
choice /m "Add dependency from DBMS service?"
if not "!ERRORLEVEL!"=="1" goto after_adding_dependency

:enter_dbms_service_name
set "DBMS_SERVICE_EXAMPLE=MSSQLSERVER"
if "!DB_TYPE!"=="1" (
  set "DBMS_SERVICE_EXAMPLE=MySQL"
) else if "!DB_TYPE!"=="2" (
  set "DBMS_SERVICE_EXAMPLE=postgresql-x64-15"
)

set DBMS_SERVICE_NAME=
set /p DBMS_SERVICE_NAME="Enter DBMS service name (e.g., !DBMS_SERVICE_EXAMPLE!): "
if "!DBMS_SERVICE_NAME!"=="" (
  echo Skip adding DBMS service dependency.
  goto after_adding_dependency
)

sc query "!DBMS_SERVICE_NAME!" >nul 2>&1
if !errorlevel! neq 0 (
  echo Service "!DBMS_SERVICE_NAME!" does not exist.
  goto enter_dbms_service_name
)

sc config "!SERVICE_NAME!" depend= "!DBMS_SERVICE_NAME!"
if ERRORLEVEL 1 (
  echo Adding dependency failed.
  goto enter_dbms_service_name
) else (
  echo Dependency added successfully.
)

:after_adding_dependency
echo Installing Inobitec DICOM Server ^(PACS^) completed.
exit /b 0

:copy_files_failed
echo Copy the files failed.
goto failed

:add_service_failed
echo Add service failed.
goto failed

:failed
echo Installing Inobitec DICOM Server ^(PACS^) failed.
exit /b 1

