@echo off
setlocal enabledelayedexpansion
title RootPlugins - Export Settings

rem Packs the settings that don't follow you between machines into one zip.
rem Close RuneLite first: it writes your settings out when it exits.
rem Optional argument: where to put the zip. Defaults to your Desktop.

set "RL=%USERPROFILE%\.runelite"
set "ENDPOINT=https://rootsoft.org/rootplugins"
set "OUT=%~1"
if "%OUT%"=="" set "OUT=%USERPROFILE%\Desktop\rootplugins-settings.zip"

echo.
echo   Exporting RuneLite settings
echo.

if not exist "%RL%" (
	echo   [!!] No RuneLite folder at %RL%.
	echo.
	pause
	exit /b 1
)

rem Only what a machine can't rebuild for itself: settings, macros, templates.
rem Plugin jars, caches and logs are deliberately left out.
set "ITEMS="
for %%D in (profiles2 MacroCombat PacketUtils bank-templates notifications) do (
	if exist "%RL%\%%D" (
		set "ITEMS=!ITEMS!'%RL%\%%D',"
		echo   [ok] including %%D
	)
)

rem Carry the importer along, so the other PC needs this one file and nothing else.
set "STAGE=%TEMP%\rootplugins-export"
if exist "%STAGE%" rd /s /q "%STAGE%"
mkdir "%STAGE%"
if exist "%~dp0import-settings.bat" (
	copy /y "%~dp0import-settings.bat" "%STAGE%\import-settings.bat" >nul
) else (
	curl -fsS -o "%STAGE%\import-settings.bat" "%ENDPOINT%/import-settings.bat" 2>nul
)
if exist "%STAGE%\import-settings.bat" (
	set "ITEMS=!ITEMS!'%STAGE%\import-settings.bat',"
	echo   [ok] including import-settings.bat
	> "%STAGE%\READ-ME-FIRST.txt" echo To load these settings on this PC:
	>> "%STAGE%\READ-ME-FIRST.txt" echo.
	>> "%STAGE%\READ-ME-FIRST.txt" echo   1. Close RuneLite.
	>> "%STAGE%\READ-ME-FIRST.txt" echo   2. Unzip this folder somewhere, e.g. your Desktop.
	>> "%STAGE%\READ-ME-FIRST.txt" echo   3. Drag this zip onto import-settings.bat, or leave the
	>> "%STAGE%\READ-ME-FIRST.txt" echo      zip on your Desktop and double-click import-settings.bat.
	set "ITEMS=!ITEMS!'%STAGE%\READ-ME-FIRST.txt',"
)

if "%ITEMS%"=="" (
	echo   [!!] Nothing to export.
	echo.
	pause
	exit /b 1
)

if exist "%OUT%" del "%OUT%"
powershell -NoProfile -Command "Compress-Archive -Path %ITEMS:~0,-1% -DestinationPath '%OUT%' -Force"
if errorlevel 1 (
	echo   [!!] Couldn't write the zip. Close RuneLite and try again.
	echo.
	pause
	exit /b 1
)

echo.
echo   Saved to %OUT%
echo   Copy it to the other PC and run import-settings.bat there.
echo.
pause
