@echo off

if "%1" == "did-copy" goto copied
cd %~dp0
rmdir /s /q %TEMP%\xonotic-rsync-updater
mkdir %TEMP%\xonotic-rsync-updater
for %%f in (*.exe *.dll *.bat) do copy /b %%f %TEMP%\xonotic-rsync-updater\
%TEMP%\xonotic-rsync-updater\%~n0 did-copy
exit

:copied

set buildtype=release
if "%~n0" == "update-to-autobuild" set buildtype=autobuild

set options=-Prtzil --executability --delete-after --delete-excluded --stats

if exist Xonotic-low goto xonoticlow
if exist Xonotic-high goto xonotichigh
if exist ..\..\..\.git goto xonoticdatagit
if exist ..\..\..\data goto xonoticdata
goto xonotic
:xonoticlow
	set url=rsync://beta.xonotic.org/%buildtype%-Xonotic-low/
	set target=Xonotic-low/
	goto endxonotic
:xonotichigh
	set url=rsync://beta.xonotic.org/%buildtype%-Xonotic-high/
	set target=Xonotic-high/
	goto endxonotic
:xonoticdatagit
	echo NOTE: this is a git repository download. Using the regular update method.
	..\..\..\all update
	goto end
:xonoticdata
	if exist ..\..\..\misc\tools\rsync-updater\rsync.exe goto xonoticdatarsync
	echo FATAL: rsync not in misc\tools\rsync-updater. This update script cannot be used.
	goto end
:xonoticdatarsync
	set PATH=misc\tools\rsync-updater;%PATH%
	cd ..\..\..
	if exist data\xonotic-rsync-data-low.pk3 goto xonoticdatalow
	if exist data\xonotic-*-data-low.pk3 goto xonoticdatalowfuzzy
	if exist data\xonotic-rsync-data-high.pk3 goto xonoticdatahigh
	if exist data\xonotic-*-data-high.pk3 goto xonoticdatahighfuzzy
	if exist data\xonotic-rsync-data.pk3 goto xonoticdatanormal
	if exist data\xonotic-*-data.pk3 goto xonoticdatanormalfuzzy
	echo FATAL: unrecognized Xonotic build. This update script cannot be used.
	goto end
:xonoticdatalow
		set url=rsync://beta.xonotic.org/%buildtype%-Xonotic-low/
		goto endxonoticdata
:xonoticdatalowfuzzy
		set url=rsync://beta.xonotic.org/%buildtype%-Xonotic-low/
		set options=%options% -y
		goto endxonoticdata
:xonoticdatahigh
		set url=rsync://beta.xonotic.org/%buildtype%-Xonotic-high/
		goto endxonoticdata
:xonoticdatahighfuzzy
		set url=rsync://beta.xonotic.org/%buildtype%-Xonotic-high/
		set options=%options% -y
		goto endxonoticdata
:xonoticdatanormal
		set url=rsync://beta.xonotic.org/%buildtype%-Xonotic/
		goto endxonoticdata
:xonoticdatanormalfuzzy
		set url=rsync://beta.xonotic.org/%buildtype%-Xonotic/
		set options=%options% -y
		goto endxonoticdata
:endxonoticdata
	set target=./
	goto endxonotic
:xonotic
	set url=rsync://beta.xonotic.org/%buildtype%-Xonotic/
	set target=Xonotic/
	goto endxonotic
:endxonotic

set excludes=
if not "%XONOTIC_INCLUDE_ALL%" == "" goto endbit
set excludes=%excludes% --exclude=/xonotic-linux*
set excludes=%excludes% --exclude=/xonotic-osx-*
set excludes=%excludes% --exclude=/Xonotic*.app
set excludes=%excludes% --exclude=/gmqcc/gmqcc.linux*
set excludes=%excludes% --exclude=/gmqcc/gmqcc.osx

if "%ProgramFiles(x86)%" == "" goto bit32
:bit64
	if not "%XONOTIC_INCLUDE_32BIT%" == "" goto endbit
	set excludes=%excludes% --exclude=/xonotic-x86.exe
	set excludes=%excludes% --exclude=/xonotic-x86-wgl.exe
	set excludes=%excludes% --exclude=/xonotic-x86-dedicated.exe
	set excludes=%excludes% --exclude=/gmqcc/gmqcc.exe
	set excludes=%excludes% --exclude=/bin32
	set excludes=%excludes% --exclude=/*.dll
	goto endbit
:bit32
	set excludes=%excludes% --exclude=/xonotic.exe
	set excludes=%excludes% --exclude=/xonotic-wgl.exe
	set excludes=%excludes% --exclude=/xonotic-dedicated.exe
	set excludes=%excludes% --exclude=/gmqcc/gmqcc-x64.exe
	set excludes=%excludes% --exclude=/bin64
	goto endbit
:endbit

for %%f in (*.exe *.dll) do copy /b %%f %TEMP%\xonotic-rsync-updater\
%TEMP%\xonotic-rsync-updater\rsync %options% %excludes% %url% %target%
%TEMP%\xonotic-rsync-updater\chmod -R a+x %target%

:end
pause
rmdir /s /q %TEMP%\xonotic-rsync-updater
