@ECHO OFF

::
:: preFlight.bat --
::
:: Extensible Adaptable Generalized Logic Engine (Eagle)
:: Release Preparation Testing Tool
::
:: Copyright (c) 2007-2012 by Joe Mistachkin.  All rights reserved.
::
:: See the file "license.terms" for information on usage and redistribution of
:: this file, and for a DISCLAIMER OF ALL WARRANTIES.
::
:: RCS: @(#) $Id: $
::

SETLOCAL

REM SET __ECHO=ECHO
REM SET __ECHO3=ECHO
IF NOT DEFINED _AECHO (SET _AECHO=REM)
IF NOT DEFINED _CECHO (SET _CECHO=REM)
IF NOT DEFINED _VECHO (SET _VECHO=REM)

SET PIPE=^|
IF DEFINED __ECHO SET PIPE=^^^|

%_AECHO% Running %0 %*

SET TOOLS=%~dp0
SET TOOLS=%TOOLS:~0,-1%

%_VECHO% Tools = '%TOOLS%'

IF NOT DEFINED TEMP (
  ECHO The TEMP environment variable must be set first.
  GOTO usage
)

IF NOT EXIST "%TEMP%" (
  ECHO The TEMP directory, "%TEMP%", does not exist.
  GOTO usage
)

%_VECHO% Temp = '%TEMP%'

CALL :fn_ResetErrorLevel

%__ECHO% mtee.exe /? > NUL 2>&1

IF ERRORLEVEL 1 (
  ECHO The "mtee.exe" tool appears to be missing.
  GOTO usage
)

IF EXIST "%CD%\mtee.exe" (
  %_AECHO% Using "mtee.exe" tool from the current directory...
) ELSE (
  %_AECHO% Using "mtee.exe" tool from the PATH...
)

IF DEFINED NOLKG GOTO skip_lastKnownGood
IF NOT DEFINED LKG GOTO skip_lastKnownGood

SET SRCLKGDIR=%LKG%\Eagle\bin
SET SRCLKGDIR=%SRCLKGDIR:\\=\%

%_VECHO% SrcLkgDir = '%SRCLKGDIR%'

%__ECHO% "%SRCLKGDIR%\EagleShell.exe" /? > NUL 2>&1

IF NOT EXIST "%SRCLKGDIR%\EagleShell.exe" (
  ECHO The file "%SRCLKGDIR%\EagleShell.exe" does not exist.
  GOTO usage
)

IF ERRORLEVEL 1 (
  ECHO The "%SRCLKGDIR%\EagleShell.exe" tool appears to be missing.
  GOTO usage
)

%_AECHO% Using LKG "EagleShell.exe" tool from "%SRCLKGDIR%"...

REM
REM NOTE: Skip checking for the "EagleShell.exe" tool along the PATH because we
REM       just found it in the LKG location.
REM
GOTO skip_eagleShell

:skip_lastKnownGood

%__ECHO% EagleShell.exe /? > NUL 2>&1

IF ERRORLEVEL 1 (
  ECHO The "EagleShell.exe" tool appears to be missing.
  GOTO usage
)

IF EXIST "%CD%\EagleShell.exe" (
  %_AECHO% Using "EagleShell.exe" tool from the current directory...
) ELSE (
  %_AECHO% Using "EagleShell.exe" tool from the PATH...
)

:skip_eagleShell

REM ****************************************************************************
REM
REM NOTE: Attempt to detect if Inno Setup is installed.  If it is not, disable
REM       all release processing steps that require it.
REM
REM ****************************************************************************

IF DEFINED NOBAKE GOTO skip_innoSetup

IF EXIST "%ProgramFiles%\Inno Setup 5" (
  %_AECHO% Found Inno Setup in Program Files directory.
  GOTO skip_innoSetup
)

IF EXIST "%ProgramFiles(x86)%\Inno Setup 5" (
  %_AECHO% Found Inno Setup in x86-specific Program Files directory.
  GOTO skip_innoSetup
)

SET NOBAKE=1
%_AECHO% Skipping all setup creation steps...

:skip_innoSetup

REM ****************************************************************************
REM
REM NOTE: Attempt to detect if WinRAR is installed.  If it is not, disable all
REM       release processing steps that require it.
REM
REM ****************************************************************************

IF DEFINED NOARCHIVE (
  IF DEFINED NORELEASE (
    GOTO skip_winRar
  )
)

IF EXIST "%ProgramFiles%\WinRAR" (
  %_AECHO% Found WinRAR in Program Files directory.
  GOTO skip_winRar
)

IF EXIST "%ProgramFiles(x86)%\WinRAR" (
  %_AECHO% Found WinRAR in x86-specific Program Files directory.
  GOTO skip_winRar
)

IF NOT DEFINED NOARCHIVE (
  SET NOARCHIVE=1
  %_AECHO% Skipping all source code archiving steps...
)

IF NOT DEFINED NORELEASE (
  SET NORELEASE=1
  %_AECHO% Skipping all binary archiving steps...
)

:skip_winRar

IF NOT DEFINED NOARCHIVE (
  IF EXIST "%TOOLS%\data\EagleSource.sfx" (
    %_AECHO% Found source code archive base SFX...
  ) ELSE (
    SET NOARCHIVE=1
    %_AECHO% Skipping all source code archiving steps...
  )
)

IF NOT DEFINED NORELEASE (
  IF EXIST "%TOOLS%\data\EagleBinary.sfx" (
    %_AECHO% Found binary archive base SFX...
  ) ELSE (
    SET NORELEASE=1
    %_AECHO% Skipping all binary archiving steps...
  )
)

REM ****************************************************************************
REM
REM NOTE: Attempt to detect if an Authenticode certificate and the associated
REM       private key are available.  If not, disable all release processing
REM       steps that require them.
REM
REM ****************************************************************************

IF NOT DEFINED NOSIGN (
  IF NOT DEFINED PVK_FILE (
    SET NOSIGN=1
    %_AECHO% The PVK_FILE environment variable must be set first.
    %_AECHO% Skipping all steps that require signing...
    GOTO skip_sign
  )

  IF NOT EXIST "%PVK_FILE%" (
    SET NOSIGN=1
    %_AECHO% The PVK_FILE file, "%PVK_FILE%", does not exist.
    %_AECHO% Skipping all steps that require signing...
    GOTO skip_sign
  )

  IF NOT DEFINED SPC_FILE (
    SET NOSIGN=1
    %_AECHO% The SPC_FILE environment variable must be set first.
    %_AECHO% Skipping all steps that require signing...
    GOTO skip_sign
  )

  IF NOT EXIST "%SPC_FILE%" (
    SET NOSIGN=1
    %_AECHO% The SPC_FILE file, "%SPC_FILE%", does not exist.
    %_AECHO% Skipping all steps that require signing...
    GOTO skip_sign
  )

  IF NOT DEFINED SIGN_URL (
    SET NOSIGN=1
    %_AECHO% The SIGN_URL environment variable must be set first.
    %_AECHO% Skipping all steps that require signing...
    GOTO skip_sign
  )
)

:skip_sign

REM ****************************************************************************
REM
REM NOTE: The following environment variables control settings used during the
REM       Eagle release process.  This tool uses these variables to help quality
REM       check the release process itself.  It is primarily intended to be used
REM       by members of the Eagle Development Team; however, anybody can use it
REM       if they so desire.  If you are a member of the Eagle Development Team
REM       and believe that different settings may be necessary for your specific
REM       environment, please coordinate the necessary changes with the team.
REM
REM ****************************************************************************

REM SET COMMERCIAL=1
REM SET COREONLY=1
REM SET ENTERPRISE=1
REM SET NETFX20ONLY=1
REM SET NETFX35ONLY=1
REM SET NETFX40ONLY=1
REM SET NETFX45ONLY=1
REM SET NETFX451ONLY=1
REM SET NETFX452ONLY=1
REM SET NOADMINISTRATOR=1
REM SET NOARCHIVE=1
REM SET NOATTRIBUTE=1
REM SET NOBAKE=1
REM SET NOBINARY=1
REM SET NOBUILD=1
REM SET NOBUILDINFO=1
REM SET NOCLEAN=1
REM SET NOCORE=1
REM SET NOEMBED=1
REM SET NOFRAMEWORK=1
REM SET NOHASH=1
REM SET NOLIB=1
REM SET NOLKG=1
REM SET NOLOGS=1
REM SET NOMSBUILD64=1
REM SET NOMSBUILDLIB=1
REM SET NONATIVE=1
REM SET NONUGET=1
REM SET NONUGETPUSH=1
REM SET NOPACKAGE=1
REM SET NOPATCHLEVEL=1
REM SET NOPERFORMANCE=1
REM SET NORELEASE=1
REM SET NORUNTIME=1
REM SET NOSIGCHECK=1
REM SET NOSOURCE=1
REM SET NOSOURCEID=1
REM SET NOSOURCEONLY=1
REM SET NOSTRONGNAME=1
REM SET NOSYMBOLS=1
REM SET NOTAG=1
REM SET NOTEST=1
REM SET NOTESTLIB=1
REM SET NOTOOL=1
REM SET NOVENDOR=1
REM SET NOVERIFY=1
REM SET OFFICIAL=1
REM SET PACKAGE_PATCHLEVEL=1.0.X.X
REM SET PATCHLEVEL=1.0.X.X
REM SET SHELLONLY=1

REM ****************************************************************************
REM
REM NOTE: The following environment variables control the build configurations
REM       that will be skipped during the release process.  By default, most of
REM       them are disabled to speed up the release testing process.
REM
REM ****************************************************************************

SET NONETFX20=1
SET NONETFX35=1
SET NONETFX40=1
SET NONETFX45=1
SET NONETFX451=1
SET NOBARE=1
SET NOLEAN=1
SET NODATABASE=1
SET NOUNIX=1
SET NODEVELOPMENT=1
SET NONUGETPUSH=1
SET _CECHO=ECHO

%__ECHO3% CALL "%TOOLS%\flight.bat" %* %PIPE% mtee.exe "%TEMP%\EagleFlight.log"

IF ERRORLEVEL 1 (
  ECHO Release testing failed.
  GOTO errors
)

GOTO no_errors

:fn_ResetErrorLevel
  VERIFY > NUL
  GOTO :EOF

:fn_SetErrorLevel
  VERIFY MAYBE 2> NUL
  GOTO :EOF

:usage
  ECHO.
  ECHO Usage: %~nx0 [...]
  ECHO.
  ECHO The TEMP environment variable must be set to an appropriate directory.
  ECHO.
  ECHO The "mtee.exe" tool is required to exist in the current directory or somewhere
  ECHO along your PATH.  It can be downloaded for free from:
  ECHO.
  ECHO                          http://www.commandline.co.uk/
  ECHO.
  ECHO The latest Eagle binaries ^(including the "EagleShell.exe" tool^) are required to
  ECHO exist in the "%%LKG%%\Eagle\bin" directory or somewhere along your PATH.  They
  ECHO can be downloaded for free from:
  ECHO.
  ECHO                          http://eagle.to/
  ECHO.
  GOTO errors

:errors
  CALL :fn_SetErrorLevel
  ENDLOCAL
  GOTO end_of_file

:no_errors
  CALL :fn_ResetErrorLevel
  ENDLOCAL
  GOTO end_of_file

:end_of_file
%__ECHO% EXIT /B %ERRORLEVEL%
