@echo off
setlocal enabledelayedexpansion

:: ============================================================================
:: Tablet Manager Installer - T8655 Model
:: ============================================================================

:: Ensure the script runs from its current directory
cd /d "%~dp0"

:: Hardcode model - using codename for folder structure
set "MODEL=NB41"
set "PRODUCT_NAME=T8655"

goto PREPARE

:PREPARE
cls
echo ============================================================================
echo Installing Tablet Manager for Model: %PRODUCT_NAME%
echo ============================================================================
echo.

:: Define file paths
set "SOURCE_EX_=%~dp0Tablet manager_1.0.0.5.ex_"
set "TEMP_EXE=%~dp0Tablet manager_1.0.0.5.exe"
set "SYS_INI=%~dp0%MODEL%\SysInfo.ini"
set "SET_INI=%~dp0%MODEL%\Setting.ini"

:: Verify source .ex_ file exists
if not exist "%SOURCE_EX_%" (
    echo [ERROR] Source installer file not found:
    echo         "%SOURCE_EX_%"
    echo.
    pause
    exit /b 1
)

:: Verify SysInfo.ini exists
if not exist "%SYS_INI%" (
    echo [ERROR] SysInfo.ini not found at:
    echo         "%SYS_INI%"
    echo.
    pause
    exit /b 1
)

:: Verify Setting.ini exists
if not exist "%SET_INI%" (
    echo [ERROR] Setting.ini not found at:
    echo         "%SET_INI%"
    echo.
    pause
    exit /b 1
)

:: Create temporary copy of EXE file
echo Copying temporary executable file...
copy /y "%SOURCE_EX_%" "%TEMP_EXE%" >nul
if errorlevel 1 (
    echo [ERROR] Failed to create temporary EXE file.
    echo.
    pause
    exit /b 1
)

:INSTALL
echo.
echo Launching installer for %PRODUCT_NAME% with "%MODEL%" parameters...
echo.

:: Execute installation silent command and wait for completion
start "" /wait "%TEMP_EXE%" /verysilent /sysini="%SYS_INI%" /userini="%SET_INI%"

echo.
echo Installation completed successfully for %PRODUCT_NAME%!

:CLEANUP
:: Delete the temporary executable file
if exist "%TEMP_EXE%" (
    echo.
    echo Cleaning up temporary files...
    del /f /q "%TEMP_EXE%" >nul 2>&1
)

echo.
timeout /t 5
exit /b 0