@echo off
REM - Compiles a Delphi project using msbuild
REM
REM - There are the following ways to determine the name of the dproj file:
REM - 1. It can be passed as %1 parameter
REM - 2. If __SetProjectName.cmd exists in the current directory, it gets called
REM -    and should set the %project% environment variable.
REM - 3. It is assumed that the current directory is also the project name (this is probably
REM -    the most common case).
REM - The Delphi version is assumed to be Delphi 2007 unless a __SetDelphiVersion.cmd exists
REM - in the current directory. If it exists, it gets called and is supposed to set the
REM - %DelphiVer% environment variable to one of
REM - 6, 7, 2005, 2006, 2007, 2009, 2010, XE, XE2, XE3, XE4, XE5, XE6, XE7, XE8
REM
REM - The following environment variables are evaluated:
REM - MaxHints -> the maximum allowed number of hints, passed to msbuildfilter as --maxhints=%MaxHints%
REM - MaxWarnings -> the maximum allowed number of warnings, passed to msbuildfilter as --maxwarnings=%MaxWarnings%
REM - MSBuildConfig -> the build configuration to use, passed to msbuild as as /p:config=%MSBuildConfig%

setlocal

call "%~dp0\_DetermineProject.cmd" %1

set DelphiVer=2007
if exist __SetDelphiVersion.cmd call __SetDelphiVersion.cmd
@rem remove spaces which might have been added in the script
set DelphiVer=%DelphiVer: =%

set OldPath=%PATH%
echo building project %project%.dproj using Delphi %DelphiVer%
call "%~dp0\delphipath.cmd" %DelphiVer%
call "%DelphiPath%\bin\rsvars.bat"

set MSBuildOptions=

if NOT "%DelphiVer%"=="2007" goto no2007
rem Delphi 2007 sets the FrameworkDir incorrectly on 64 bit Windows, so we fix this here
SET FrameworkDir=%SystemRoot%\Microsoft.NET\Framework\
SET PATH=%FrameworkDir%%FrameworkVersion%;%FrameworkSDKDir%;%OldPath%
rem also, set it to be quiet, otherwise the compiler outputs lots of empty lines
set MSBuildOptions=-p:DCC_Quiet=true %MSBuildOptions%
:no2007

set BuildFilterOptions=
if "%MaxHints%" == "" goto noMaxHint
  echo Setting --maxhints=%MaxHints%
  set BuildFilterOptions=--maxhints=%MaxHints%
:noMaxHint
if "%MaxWarnings%" == "" goto noMaxWarn
  echo Setting --maxwarnings=%MaxWarnings%
  set BuildFilterOptions=%BuildFilterOptions% --maxwarnings=%MaxWarnings% 
:NoMaxWarn
echo BuildFilterOptions=%BuildFilterOptions%

if "%MSBuildConfig%" == "" goto NoBuildCfg
  if "%DelphiVer%"=="2007" goto is2007
    echo Setting /p:config=%MSBuildConfig%
    set MSBuildOptions=/p:config=%MSBuildConfig% %MSBuildOptions%
    goto NoBuildCfg
:is2007
    rem for Delphi 2007, the parameter is Configuration rather than config
    echo Setting /p:Configuration=%MSBuildConfig%
    set MSBuildOptions=/p:Configuration=%MSBuildConfig% %MSBuildOptions%
    goto NoBuildCfg
:NoBuildCfg
echo MSBuildOptions=%MSBuildOptions%

pushd src
set DPROJ=%project%.%DelphiVer%.dproj
if not exist %DPROJ% set DPROJ=%project%.dproj
echo building %DPROJ%
msbuild %DPROJ% /target:rebuild %MSBuildOptions% | "%~p0"\msbuildfilter %BuildFilterOptions% ..\errors.txt
popd
if errorlevel 1 goto error

endlocal
if "%BatchBuild%"=="1" goto nopause
pause
:nopause
goto :eof

:error
echo ************************************
echo ***** Error building %project%
echo ************************************
if "%MAILSERVER%"=="" goto nomail
if "%MAILSENDER%"=="" goto nomail
if "%MAILRECEIVER%"=="" goto nomail
if "%MAILDOMAIN%"=="" goto nomail
echo sending e-mail to "%MAILRECEIVER%" via "%MAILSERVER%"
%~dp0\mailsend.exe +bc +cc -smtp %MAILSERVER% -d %MAILDOMAIN% -t %MAILRECEIVER% -f %MAILSENDER% -sub "Error building %project%" -M "Build Error %CD%\src\%DPROJ%" -attach errors.txt,text/plain,a
echo done sending e-mail
endlocal
goto :eof

:nomail
endlocal
pause
goto :eof
