@echo off
setlocal
title RootPlugins Installer

rem The one file a player downloads. Fetches the installer and runs it with the Java that RuneLite
rem already ships, so nobody has to install Java. Re-launches itself as administrator once, because
rem the hosts file needs it.

set "ENDPOINT=https://rootsoft.org/rootplugins"
set "RL=%LOCALAPPDATA%\RuneLite"
set "JAVA=%RL%\jre\bin\java.exe"
set "WORK=%TEMP%\rootplugins-installer"
set "MODE=%~1"

net session >nul 2>&1
if errorlevel 1 (
	echo Asking for administrator rights, needed to add the server to your hosts file...
	powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -ArgumentList '%MODE% elevated' -Verb RunAs"
	exit /b
)

echo.
echo   RootPlugins for RuneLite
echo.

if not exist "%JAVA%" (
	echo   [!!] RuneLite isn't installed yet, or has never been run.
	echo        Install it from https://runelite.net, start it once, then run this again.
	echo.
	pause
	exit /b 1
)

if not exist "%WORK%" mkdir "%WORK%"
echo   Downloading the installer...
curl -fsS -o "%WORK%\rootplugins-installer.jar" "%ENDPOINT%/rootplugins-installer.jar"
if errorlevel 1 (
	echo   [!!] Couldn't reach %ENDPOINT%.
	echo        Check your internet connection and try again.
	echo.
	pause
	exit /b 1
)

if /i "%MODE%"=="uninstall" (
	"%JAVA%" -jar "%WORK%\rootplugins-installer.jar" uninstall
) else (
	"%JAVA%" -jar "%WORK%\rootplugins-installer.jar"
)

echo.
pause
