@echo off

set currentPath=%~dp0
set servicePath="C:\Program Files\MtnOS\terminalService"
: set javaPath="C:\Program Files (x86)\Java\jre1.8.0_271\bin\java.exe"
set restTerminal=restTerminal-3.6.6.jar

echo Looking for java installation

set javaPathRoot="C:\Program Files (x86)\Java"
if not exist %javaPathRoot% (
    set javaPathRoot="C:\Program Files\Java"
)
if not exist %javaPathRoot% (
    echo No java path found
    echo Please install java first
    pause
    exit 
)

cd %javaPathRoot%
for /F "tokens=* USEBACKQ" %%F IN (`dir /s /b /a-d "java.exe"`) DO (
set javaPath="%%F"
)

if not exist %javaPath% (
    echo No Java executable found
    echo Please install java first
    pause
    exit 
)


echo Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
    echo Success: Administrative permissions confirmed.
) else (
    echo Failure: Current permissions inadequate.
    pause
    exit 
)


echo Installing terminal service
echo Current Path is %currentPath%
echo service Path is set to %servicePath%
echo java Path is set to %javaPath%
echo restTerminal version is %restTerminal%

mkdir %servicePath%

Copy %currentPath%nssm.exe %servicePath%
Copy %currentPath%log.txt %servicePath%
Copy %currentPath%application.properties %servicePath%
Copy %currentPath%%restTerminal% %servicePath%

cd %servicePath%

nssm.exe install terminalservice AppDirectory %servicePath%
nssm.exe set terminalservice Application %javaPath%
nssm.exe set terminalservice  AppDirectory %servicePath%
nssm.exe set terminalservice AppParameters -jar %restTerminal%
: nssm.exe set terminalservice AppStdout %servicePath%/log.txt
: nssm.exe set terminalservice AppStderr %servicePath%/log.txt
nssm.exe start terminalservice

pause
exit