@echo off
setlocal
title RootPlugins - Import Settings

rem Unpacks a zip made by export-settings.bat into this machine's RuneLite folder.
rem Drag the zip onto this file, or leave it on the Desktop and just double-click.
rem Close RuneLite first, or it will overwrite what you import when it exits.

set "RL=%USERPROFILE%\.runelite"
set "ZIP=%~1"
if "%ZIP%"=="" set "ZIP=%USERPROFILE%\Desktop\rootplugins-settings.zip"

echo.
echo   Importing RuneLite settings
echo.

if not exist "%ZIP%" (
	echo   [!!] No settings zip found at:
	echo        %ZIP%
	echo        Drag the zip onto this file, or put it on your Desktop first.
	echo.
	pause
	exit /b 1
)

rem Full paths: if something else on PATH shadows find.exe, this check would pass silently.
"%SystemRoot%\System32\tasklist.exe" /fi "imagename eq RuneLite.exe" /nh 2>nul | "%SystemRoot%\System32\findstr.exe" /i "RuneLite.exe" >nul
if not errorlevel 1 (
	echo   [!!] RuneLite is running. Close it first, or it will write its own
	echo        settings back over these when it exits.
	echo.
	pause
	exit /b 1
)

if not exist "%RL%" mkdir "%RL%"

rem Keep whatever is already here, so a mistake is undoable.
for /f %%T in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd-HHmmss"') do set "STAMP=%%T"
set "BACKUP=%RL%\settings-backup-%STAMP%"
for %%D in (profiles2 MacroCombat PacketUtils bank-templates notifications) do (
	if exist "%RL%\%%D" (
		if not exist "%BACKUP%" mkdir "%BACKUP%"
		xcopy "%RL%\%%D" "%BACKUP%\%%D\" /e /i /q /y >nul
	)
)
if exist "%BACKUP%" echo   [ok] Saved your current settings to %BACKUP%

powershell -NoProfile -Command "Expand-Archive -Path '%ZIP%' -DestinationPath '%RL%' -Force"
if errorlevel 1 (
	echo   [!!] Couldn't unpack %ZIP%.
	echo.
	pause
	exit /b 1
)

rem The zip carries the importer and its instructions; they aren't settings, so don't leave
rem them sitting in the RuneLite folder.
if exist "%RL%\import-settings.bat" del "%RL%\import-settings.bat"
if exist "%RL%\READ-ME-FIRST.txt" del "%RL%\READ-ME-FIRST.txt"

echo   [ok] Imported into %RL%
echo.
echo   Start RuneLite; your plugin settings, macros and bank templates
echo   will be the same as on the other PC.
echo.
pause
