@echo off

rem Check if running as administrator
net session >nul 2>&1
if %errorlevel% NEQ 0 (
    echo Administrator rights required. Please run the script as an administrator.
    pause
    exit /b 1
)

echo Configuring Assignment for The mSupply Foundation... Please wait! 

rem Importing registry file...
regedit /s "TeamViewer_Settings.reg"

rem Install TeamViewer and configure UI
"%ProgramFiles%\TeamViewer\TeamViewer.exe" assignment --id 0001CoABChBOhG0g6vYR7qrqZnJ_ASPrEigIACAAAgAJABBlCawdNzcI8NlWi072Y5XDZ8_PCqWRs6FV7NAazwxUGkADBn9P4HcNaREs20CMHASg-_d4ktj_TAHNjr3f4glyOFYqjjKskIGA7_IezMkP1nTKmQ-TP2_Ph6BkBF5AMIy1IAEQkZKN5gU= --retries 5 --timeout=120

timeout /t 5 /nobreak > nul 2>&1

"%ProgramFiles%\TeamViewer\TeamViewer.exe" assign --api-token 24696914-2jggyl5FDlOO5hg240MA --alias %COMPUTERNAME% --grant-easy-access

echo Settings applied. Restarting TeamViewer service...

rem Stop the TeamViewer service
net stop "TeamViewer" >nul 2>&1

if %errorlevel% neq 0 (
    echo Failed to stop TeamViewer service. It may not be running or you need administrator rights.
    pause
    exit /b 1
)

rem Start the TeamViewer service
net start "TeamViewer" >nul 2>&1

if %errorlevel% neq 0 (
    echo Failed to start TeamViewer service. Please check the installation and permissions.
    pause
    exit /b 1
)

echo.
echo TeamViewer assignment complete. Please contact support@msupply.org.nz if you have any issues.