@echo off
REM Safe reinstall script for FidelioHotelConnector service
REM Run as Administrator in the folder that contains fideliohotelconnector.exe

setlocal

set SERVICE_NAME=FidelioHotelConnector
set DISPLAY_NAME=Fidelio Hotel Connector
set EXE_NAME=fideliohotelconnector.exe
set EXE_PATH=%~dp0%EXE_NAME%

echo.
echo === Verify EXE exists ===
if not exist "%EXE_PATH%" (
  echo ERROR: "%EXE_PATH%" not found. Copy %EXE_NAME% into this folder and re-run.
  pause
  exit /b 1
)

echo.
echo === Current service config (if present) ===
sc qc %SERVICE_NAME% 2>nul || echo "Service not present or cannot query."
echo.

echo === Attempt to stop service (if running) ===
sc stop %SERVICE_NAME% 2>nul
timeout /t 2 /nobreak >nul

echo.
echo === Attempt to delete existing service (if present) ===
sc delete %SERVICE_NAME% 2>nul
timeout /t 2 /nobreak >nul

echo.
echo If the service was marked for deletion, Windows may hold the name until a reboot.
echo If you see "service is marked for deletion", please reboot the server and re-run this script.
echo.

echo === Create service ===
REM Important: spacing after '=' is required by sc; binPath value must include quoted exe and any args
sc create "%SERVICE_NAME%" binPath= "\"%EXE_PATH%\" --service" start= auto DisplayName= "%DISPLAY_NAME%" obj= LocalSystem
if ERRORLEVEL 1 (
  echo.
  echo ERROR: sc create failed. Exit code %ERRORLEVEL%.
  echo Show current config / reasons:
  sc qc %SERVICE_NAME% 2>nul
  echo.
  echo If the service is not present or sc create failed with "Invalid field", try rebooting to clear pending deletions.
  pause
  exit /b 1
)

echo.
echo === Set service description ===
sc description "%SERVICE_NAME%" "Local Hotel Connector for Fidelio Suite 8 - WebSocket client and SQL executor" 2>nul

echo.
echo === Set failure actions (restart on failure) ===
REM Note: 'sc failure' parameters must be provided as separate tokens; the exact syntax may vary by Windows version
sc failure "%SERVICE_NAME%" reset= 86400 actions= restart/60000/restart/60000/restart/60000 2>nul

echo.
echo === Ensure start type is auto ===
sc config "%SERVICE_NAME%" start= auto 2>nul

echo.
echo === Show created service config ===
sc qc "%SERVICE_NAME%"

echo.
echo === Try to start service ===
net start "%SERVICE_NAME%"
if ERRORLEVEL 1 (
  echo.
  echo Service did NOT start. Common causes:
  echo " - Service marked for deletion (requires reboot to fully remove)"
  echo " - The EXE crashes immediately (missing DLLs, wrong bitness, runtime error)."
  echo " - Permission issues for the account (we used LocalSystem)."
  echo.
  echo Next steps:
  echo " 1) If service is marked for deletion -> reboot the server, then re-run this script."
  echo " 2) Otherwise run the EXE manually from command line to see errors:"
  echo "      \"%EXE_PATH%\""
  echo "    Then check logs: logs\connector.log and Event Viewer (Application/System)."
  echo " 3) If EXE prints missing DLL / ImportError, rebuild including that binary or install the dependency."
  pause
  exit /b 1
)

echo.
echo Service started successfully.
pause