@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

:: ====================== Configuration ======================
set "ServiceName=iVentoy"
set "ExePath=%~dp0iVentoy_64.exe"
set "ServiceArgs=-Service -R"
set "BinPath=%ExePath% %ServiceArgs%"
:: ======================================================

:: ====================== Check System Language ======================
set "LANG=EN"
for /f "skip=1 tokens=3" %%a in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Nls\Language" /v "Default" 2^>nul') do (    
    if "%%a"=="0804" set "LANG=CN"
)

:: ====================== zh_cn Tip Msg ======================
if "%LANG%"=="CN" (
    set "MSG_ADMIN_CHECK=正在检查管理员权限..."
    set "MSG_NO_CFG=错误：请先按照如下步骤操作，之后再注册服务！"
    set "MSG_NO_CFG1=  1. 直接双击运行"
    set "MSG_NO_CFG2=  2. 选择网卡，设置参数，点击页面开始按钮启动"
    set "MSG_NO_CFG3=  3. 点击页面结束按钮停止"    
    set "MSG_NO_CFG4=说明：以上操作之后iVentoy会保存配置，注册为服务之后即按照本次参数启动！"
    set "MSG_NO_ADMIN=错误：请【右键以管理员身份】运行此脚本！"
    set "MSG_HAVE_ADMIN=已获取管理员权限"
    set "MSG_SERVICE_EXISTS=服务已存在，查询状态..."
    set "MSG_SERVICE_RUNNING=服务正在运行，正在停止..."
    set "MSG_SERVICE_STOPPED=服务已停止"
    set "MSG_SERVICE_ALREADY_STOPPED=服务已处于停止状态"
    set "MSG_SERVICE_NOT_EXIST=服务不存在，将创建新服务"
    set "MSG_UPDATING_BINPATH=更新服务路径..."
    set "MSG_CREATING_SERVICE=创建服务..."
    set "MSG_STARTING_SERVICE=启动服务..."
    set "MSG_PAUSE=按任意键退出..."
    set "MSG_DONE=操作成功完成！"
)

:: ====================== en_us Tip Msg ======================
if "%LANG%"=="EN" (
    set "MSG_ADMIN_CHECK=Checking administrator privileges..."
    set "MSG_NO_CFG=ERROR: Please run the following steps before register service!"
    set "MSG_NO_CFG1=  1. Double-click iVentoy exe file to run"
    set "MSG_NO_CFG2=  2. Select local IP, set IP pool and click the green start button in the web page."
    set "MSG_NO_CFG3=  3. Click the red stop button to stop"    
    set "MSG_NO_CFG4=The above steps make iVentoy save the configuration and it will auto run with this configuration after register service."
    set "MSG_NO_ADMIN=ERROR: Please run this script as Administrator!"
    set "MSG_HAVE_ADMIN=Administrator privileges confirmed."
    set "MSG_SERVICE_EXISTS=Service exists, checking status..."
    set "MSG_SERVICE_RUNNING=Service is running, stopping..."
    set "MSG_SERVICE_STOPPED=Service stopped successfully."
    set "MSG_SERVICE_ALREADY_STOPPED=Service is already stopped."
    set "MSG_SERVICE_NOT_EXIST=Service does not exist, will create new."
    set "MSG_UPDATING_BINPATH=Updating service binary path..."
    set "MSG_CREATING_SERVICE=Creating new service..."
    set "MSG_STARTING_SERVICE=Starting service..."
    set "MSG_PAUSE=Press any key to continue . . ."
    set "MSG_DONE=Operation successfull completed!"
)


:: ====================== Check config.dat ======================
if not exist "%~dp0data\config.dat" (
    echo %MSG_NO_CFG%
    echo %MSG_NO_CFG1%
    echo %MSG_NO_CFG2%
    echo %MSG_NO_CFG3%
    echo.
    echo %MSG_NO_CFG4%
    echo.
    echo %MSG_PAUSE%
    pause >nul
    exit /b 1
)

:: ====================== Check Admin ======================
echo %MSG_ADMIN_CHECK%
fltmc >nul 2>&1
if %errorlevel% neq 0 (
    echo %MSG_NO_ADMIN%
    echo %MSG_PAUSE%
    pause >nul
    exit /b 1
)
echo %MSG_HAVE_ADMIN%
echo.

:: ====================== Stop Service If Needed ======================
sc query "%ServiceName%" >nul 2>&1
if %errorlevel% equ 0 (
    echo %MSG_SERVICE_EXISTS%
    
    for /f "skip=3 tokens=2" %%a in ('sc query "%ServiceName%" ^| findstr "STATE"') do set "state=%%a"
    
    if /i "!state!"=="RUNNING" (
        echo %MSG_SERVICE_RUNNING%
        sc stop "%ServiceName%" >nul
        :wait_stop
        sc query "%ServiceName%" | findstr "STOPPED" >nul
        if %errorlevel% neq 0 (
            timeout /t 1 /nobreak >nul
            goto wait_stop
        )
        echo %MSG_SERVICE_STOPPED%
    ) else (
        echo %MSG_SERVICE_ALREADY_STOPPED%
    )
) else (
    echo %MSG_SERVICE_NOT_EXIST%
)
echo.

:: ======================  Ceate Srevice / Update Service BinPath ======================
sc query "%ServiceName%" >nul 2>&1
if %errorlevel% equ 0 (
    echo %MSG_UPDATING_BINPATH%
    sc config "%ServiceName%" binPath= "%BinPath%" start= auto >nul
) else (
    echo %MSG_CREATING_SERVICE%
    sc create "%ServiceName%" binPath= "%BinPath%" start= auto >nul
)
echo.

:: ====================== Start Service ======================
echo %MSG_STARTING_SERVICE%
sc start "%ServiceName%" >nul
echo.

:: ====================== Done ======================
echo ==============================================
sc qc "%ServiceName%"
sc query "%ServiceName%"
echo ==============================================
echo.

echo %MSG_DONE%
echo %MSG_PAUSE%
pause >nul

