@echo off
setlocal enabledelayedexpansion
::
:: Copyright (c) 2017-2020, HarpyWar (harpywar@gmail.com)
::
@call module\config.inc.bat

echo.
echo ###############################################################################
echo #                    PvPGN Visual Studio Solution Builder                     #
echo # --------------------------------------------------------------------------- #
call %i18n% 10_0_0
call %i18n% 10_0_1
echo # --------------------------------------------------------------------------- #
echo ###############################################################################
echo.

:: redefine %PVPGN_BUILD% without a slash
set PVPGN_BUILD=build


if not exist %PVPGN_BUILD%\pvpgn.sln (
	call %i18n% 10_1
	goto :THEEND
)

call %i18n% 10_2 "%PVPGN_DEVKIT%"
choice
echo.
echo --------------------------------------------------------------------------------

if %errorlevel%==2 goto :THEEND


if not exist %PVPGN_DEVKIT% mkdir %PVPGN_DEVKIT%

:: copy config.h
copy /Y "%PVPGN_BUILD%\config.h" "%PVPGN_DEVKIT%\"

:: copy pvpgn.sln
copy /Y "%PVPGN_BUILD%\pvpgn.sln" "%PVPGN_DEVKIT%\"

:: build each project
call :build_project src\bnetd bnetd true
call :build_project src\bnpass bnpass true
call :build_project src\bnpass sha1hash true
call :build_project src\bntrackd bntrackd true
call :build_project src\bniutils bni2tga true
call :build_project src\bniutils bnibuild true
call :build_project src\bniutils bniextract true
call :build_project src\bniutils bnilist true
call :build_project src\bniutils tgainfo true
call :build_project src\client bnbot true
call :build_project src\client bnchat true
call :build_project src\client bnftp true
call :build_project src\client bnstat true
call :build_project src\d2cs d2cs true
call :build_project src\d2dbs d2dbs true
:: keep original output dir for libs
call :build_project src\common common false
call :build_project src\compat compat false
call :build_project src\win32 win32 false
call :build_project fmt fmt false


:: remove unused projects generated by cmake
call :remove_sln_project "%PVPGN_DEVKIT%pvpgn.sln" ALL_BUILD
call :remove_sln_project "%PVPGN_DEVKIT%pvpgn.sln" RUN_TESTS
call :remove_sln_project "%PVPGN_DEVKIT%pvpgn.sln" INSTALL
call :remove_sln_project "%PVPGN_DEVKIT%pvpgn.sln" purge
call :remove_sln_project "%PVPGN_DEVKIT%pvpgn.sln" uninstall

:: create shortcut on pvpgn.sln
call :create_shortcut "%CURRENT_PATH%%PVPGN_DEVKIT%pvpgn.sln" "%CURRENT_PATH%pvpgn.sln.lnk"

echo.
echo --------------------------------------------------------------------------------
call %i18n% 10_3 "%PVPGN_DEVKIT%"
call %i18n% 10_4 pvpgn.sln "%PVPGN_DEVKIT%pvpgn.sln"
call %i18n% 10_5 "release\"
echo --------------------------------------------------------------------------------


goto :THEEND


:build_project <path> <name> <change_out_dir>
	if not exist "%PVPGN_DEVKIT%projects\%2" mkdir "%PVPGN_DEVKIT%projects\%2"
	copy /Y "%PVPGN_BUILD%\%1\%2.vcxproj*" "%PVPGN_DEVKIT%projects\%2"
	
	call :create_vcxproj_user "%PVPGN_DEVKIT%projects\%2\%2.vcxproj.user"
	
	rem replace paths
	
	:: set atlmfc and release into additional include dir section
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "%CURRENT_PATH%%PVPGN_BUILD%;%CURRENT_PATH%source\src;" "..\..\;%PROJECT_ROOT_DIR%source\src;%PROJECT_ROOT_DIR%release;%PROJECT_ROOT_DIR%module\include\atlmfc;"
	:: replace source path
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "%CURRENT_PATH%source" "%PROJECT_ROOT_DIR%source"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj.filters" "%CURRENT_PATH%source" "%PROJECT_ROOT_DIR%source"
	
	:: replace release and debug paths
	if [%3]==[true] (
		@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "%CURRENT_PATH%%PVPGN_BUILD%\%1\Debug" "%PROJECT_ROOT_DIR%release"
		@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "%CURRENT_PATH%%PVPGN_BUILD%\%1\Release" "%PROJECT_ROOT_DIR%release"
		@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "%CURRENT_PATH%%PVPGN_BUILD%\%1\RelWithDebInfo" "%PROJECT_ROOT_DIR%release"
	)
	
	:: replace unix like build path for a project
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "%CURRENT_PATH_BACKSLASH%%PVPGN_BUILD%/%1" "../%2"
	:: replace other full paths to relative
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "%CURRENT_PATH_BACKSLASH%" "../../../"
	
	:: replace path for magic builder module 
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "%CURRENT_PATH%module" "%PROJECT_ROOT_DIR%module"
	:: replace new path in pvpgn.sln
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%pvpgn.sln" "%1\%2" "projects\%2\%2"

	:: libs now build into release
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\common\Debug\common.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\src\common\Debug\common.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\compat\Debug\compat.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\src\compat\Debug\compat.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\win32\Debug\win32.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\src\win32\Debug\win32.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\..\fmt\Debug\fmtd.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\fmt\Debug\fmtd.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\common\Release\common.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\src\common\Release\common.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\compat\Release\compat.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\src\compat\Release\compat.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\win32\Release\win32.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\src\win32\Release\win32.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\..\fmt\Release\fmt.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\fmt\Release\fmt.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\common\RelWithDebInfo\common.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\src\common\RelWithDebInfo\common.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\compat\RelWithDebInfo\compat.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\src\compat\RelWithDebInfo\compat.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\win32\RelWithDebInfo\win32.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\src\win32\RelWithDebInfo\win32.lib"
	@call %TOOLS_PATH%replacer.bat "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" "..\..\fmt\RelWithDebInfo\fmt.lib" "%PROJECT_ROOT_DIR%%PVPGN_BUILD%\fmt\RelWithDebInfo\fmt.lib"

	:: remove reduntant cmake node that executes cmake.exe on each build
	call :remove_xml_node "%PVPGN_DEVKIT%projects\%2\%2.vcxproj" Project/ItemGroup/CustomBuild
	
	exit /b 0
	
	
:create_vcxproj_user <filename>

	echo ^<?xml version="1.0" encoding="utf-8"?^> > %1
	echo ^<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"^> >> %1
	echo   ^<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"^> >> %1
	echo 	^<LocalDebuggerWorkingDirectory^>%PROJECT_ROOT_DIR%release^</LocalDebuggerWorkingDirectory^> >> %1
	echo 	^<DebuggerFlavor^>WindowsLocalDebugger^</DebuggerFlavor^> >> %1
	echo   ^</PropertyGroup^> >> %1
	echo   ^<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"^> >> %1
	echo 	^<LocalDebuggerWorkingDirectory^>%PROJECT_ROOT_DIR%release^</LocalDebuggerWorkingDirectory^> >> %1
	echo 	^<DebuggerFlavor^>WindowsLocalDebugger^</DebuggerFlavor^> >> %1
	echo   ^</PropertyGroup^> >> %1
	echo   ^<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='RelWithDebInfo|Win32'"^> >> %1
	echo 	^<LocalDebuggerWorkingDirectory^>%PROJECT_ROOT_DIR%release^</LocalDebuggerWorkingDirectory^> >> %1
	echo 	^<DebuggerFlavor^>WindowsLocalDebugger^</DebuggerFlavor^> >> %1
	echo   ^</PropertyGroup^> >> %1
	echo ^</Project^> >> %1
	
	exit /b 0

:create_shortcut <source> <destination>
	set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
	echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
	echo sLinkFile = %2 >> %SCRIPT%
	echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
	echo oLink.TargetPath = %1 >> %SCRIPT%
	echo oLink.Save >> %SCRIPT%
	
	cscript /nologo %SCRIPT%
	del %SCRIPT%
	
	exit /b 0

:: removes given xml node from xml file
:remove_xml_node "<filename.vcxproj>" nodeName
	set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
	echo Set objXMLDoc = Wscript.CreateObject("Microsoft.XMLDOM") >> %SCRIPT%
	echo objXMLDoc.async = False >> %SCRIPT%
	echo Dim XMLFile >> %SCRIPT%
	echo XMLFile = %1 >> %SCRIPT%
	echo objXMLDoc.load(XMLFile)  >> %SCRIPT%
	echo Set nodes = objXMLDoc.selectNodes("%2") >> %SCRIPT%
	echo For Each node In nodes >> %SCRIPT%
	echo   node.parentNode.removeChild(node) >> %SCRIPT%
	echo Next >> %SCRIPT%
	echo objXMLDoc.Save(XMLFile) >> %SCRIPT%
	
	cscript /nologo %SCRIPT%
	del %SCRIPT%	
	
	exit /b 0

:: removes a project block from vs solution file
:remove_sln_project "<filename.sln>" <projectName> 
	set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

	echo Dim newText >> %SCRIPT%
	echo Dim inProject >> %SCRIPT%
	echo Dim isBadProject >> %SCRIPT%
	echo Dim line >> %SCRIPT%
	
	echo Set fso = CreateObject("Scripting.FileSystemObject") >> %SCRIPT%
	echo Set f = fso.OpenTextFile(%1, 1) >> %SCRIPT%

	echo Do Until f.AtEndOfStream >> %SCRIPT%
	echo 	line = f.ReadLine >> %SCRIPT%
	echo 	If InStr(1, line, "Project(") = 1 Then ' if line starts with Project( >> %SCRIPT%
	echo 		inProject = true >> %SCRIPT%
	echo 		If InStr(1, line, "%2") > 0 Then ' if line contains a given project name >> %SCRIPT%
	echo 			isBadProject = true >> %SCRIPT%
	echo 		Else >> %SCRIPT%
	echo 			isBadProject = false >> %SCRIPT%
	echo 		End If >> %SCRIPT%
	echo 	End If >> %SCRIPT%
		
	echo 	If Not isBadProject Or Not inProject Then  >> %SCRIPT%
	echo 		newText = newText + line ^& vbCrLf >> %SCRIPT%
	echo 	End If >> %SCRIPT%
		
	echo 	If line = "EndProject" Then ' if line equal EndProject >> %SCRIPT%
	echo 		inProject = false >> %SCRIPT%
	echo 	End If >> %SCRIPT%
	echo Loop >> %SCRIPT%
	echo f.Close >> %SCRIPT%

	echo Set objFile = fso.CreateTextFile(%1, True) >> %SCRIPT%
	echo objFile.Write newText >> %SCRIPT%
	echo objFile.Close >> %SCRIPT%

	
	cscript /nologo %SCRIPT%
	del %SCRIPT%	
	
	exit /b 0


	
:THEEND
pause
