@ECHO OFF
REM File: make_version.bat
REM What: Create version.h and load up with SVN build version numbers.
REM
REM svnversion switches:
REM   -n Omit the usual trailing newline from the output.
REM
REM Set paths for Visual Studio "Build Events > Pre-Build Event"
REM If no path defined, assume current dir.
REM (Allow running outside Visual Studio.)

REM Determine the bin path from the %0 argument
SET BINPATH=%~dp0

SET SRCPATH=%BINPATH%\..

REM if the SRCPATH is passed use it, otherwise generate a dummy
IF "%SRCPATH%"=="" GOTO NO_SRC
  SET SVNPATH=%BINPATH%\svnversion.exe
  date /T >  %BINPATH%/VERSION_H_GENERATED.txt
  time /T >> %BINPATH%/VERSION_H_GENERATED.txt
  GOTO CONFIG_DONE
:NO_SRC
  IF EXIST %BINPATH%/VERSION_H_GENERATED.txt GOTO EOF
  SET SVNPATH=REM
:CONFIG_DONE

REM the output is a temporary file that will be moved later
SET OUTPUT=version.h.new

echo #ifndef _VERSION_H_  > %OUTPUT%
echo #define _VERSION_H_ >> %OUTPUT%
echo. >> %OUTPUT%
echo #include ^"release.h^" >> %OUTPUT%
echo. >> %OUTPUT%
echo /*----------------------------------------------------------------------------------- >> %OUTPUT%
echo This file has been generated by make_version.bat and should not be manually modified. >> %OUTPUT%
REM -------------- Removing the two lines that regularly change making diffs useless -------------
REM echo Source folder: %cd%  >> %OUTPUT%
REM echo Creation date: %date%  Time:%time% >> %OUTPUT%
REM ----------------------------------------------------------------------------------------------
echo Example:   pWnd-^>SetWindowText(IGUANAIR_VER_STR(IGUANAIR_VERSION));  >> %OUTPUT%
echo ------------------------------------------------------------------------------------ >> %OUTPUT%
echo svnversion.exe version: >> %OUTPUT%
%SVNPATH% --version >> %OUTPUT%
echo ------------------------------------------------------------------------------------ >> %OUTPUT%
REM: From svnversion.exe -h
echo The version number will be a single number if the working copy is single revision, >> %OUTPUT%
echo unmodified, not switched and with an URL that matches the TRAIL_URL argument. >> %OUTPUT%
echo If the working copy is unusual the version number will be more complex: >> %OUTPUT%
echo   4123:4168     mixed revision working copy >> %OUTPUT%
echo   4168M         modified working copy 	>> %OUTPUT%
echo   4123S         switched working copy 	>> %OUTPUT%
echo   4123P         partial working copy, from a sparse checkout >> %OUTPUT%
echo   4123:4168MS   mixed revision, modified, switched working copy >> %OUTPUT%			
echo  If invoked on a directory that is not a working copy, an exported directory say, >> %OUTPUT%
echo  the program will output 'exported'.>> %OUTPUT%
echo -----------------------------------------------------------------------------------*/ >> %OUTPUT%

REM: Include manually edited header file for release versions.
REM: Application dialogs use IDC_RELEASE_NUMBER for dialog formatting.
REM echo #include "release.h"	// Manually edited release version numbers. >> %OUTPUT%
REM echo. >> %OUTPUT%

REM: Alert user if DEBUG version was fielded.
echo #ifdef _DEBUG>> %OUTPUT%
echo #define IGUANAIR_VER_STR(a) ^"DEBUG!: %USERNAME%@%COMPUTERNAME%^">> %OUTPUT%
echo #else>> %OUTPUT%

IF "%SVNPATH%"=="REM" GOTO IGBUILD
  echo #define IGUANAIR_VER_STR(a) ^"Release: ^"a^"-^"IGUANAIR_RELEASE^"-^"IGUANAIR_VERSION>>   %OUTPUT%
  GOTO DEFINES_DONE
:IGBUILD
  echo #define IGUANAIR_VER_STR(a) ^"Built on %COMPUTERNAME%^">> %OUTPUT%
:DEFINES_DONE

echo #endif>> %OUTPUT%
echo. >> %OUTPUT%


REM: IguanaIR base version level:
echo #define IGUANAIR_VERSION ^"\>> %OUTPUT%
%SVNPATH% -n  %SRCPATH% >>        %OUTPUT%
echo ^" >> %OUTPUT%


echo. >> %OUTPUT%
echo #endif>> %OUTPUT%


REM Always write a temp file then move it so that parallel builds do not stomp
REM on each other's versions file.  Further, if the content has not changed
REM then do NOT replace the existing file so that later builds will not think
REM that the file changed based on the timestamp.
fc /b %OUTPUT% %SRCPATH%\version.h | find "FC: no differences encountered" > NUL
IF "%ERRORLEVEL%" == "0" GOTO DELETE_OUTPUT
copy %OUTPUT% %SRCPATH%\version.h

:DELETE_OUTPUT
del %OUTPUT%

:EOF
REM echo on
