@ECHO OFF

::
:: sign.bat --
::
:: Extensible Adaptable Generalized Logic Engine (Eagle)
:: Release Signing 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
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 %*

REM SET DFLAGS=/L

SET FLAGS=/V /F /G /H /R /Y /Z

IF NOT DEFINED TIMESTAMP (
  SET TIMESTAMP=-t http://timestamp.verisign.com/scripts/timstamp.dll -tr 10 -tw 60
)

SET CONFIGURATION=%1

IF DEFINED CONFIGURATION (
  CALL :fn_UnquoteVariable CONFIGURATION
) ELSE (
  %_AECHO% No configuration specified, using default...
  SET CONFIGURATION=Release
)

%_VECHO% Configuration = '%CONFIGURATION%'

SET DUMMY2=%2

IF DEFINED DUMMY2 (
  GOTO usage
)

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

IF NOT EXIST "%PVK_FILE%" (
  ECHO The PVK_FILE file, "%PVK_FILE%", does not exist.
  GOTO errors
)

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

IF NOT EXIST "%SPC_FILE%" (
  ECHO The SPC_FILE file, "%SPC_FILE%", does not exist.
  GOTO errors
)

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

SET SOURCE=%~dp0\..\..
SET SOURCE=%SOURCE:\\=\%

%_VECHO% PvkFile = '%PVK_FILE%'
%_VECHO% SpcFile = '%SPC_FILE%'
%_VECHO% SignUrl = '%SIGN_URL%'
%_VECHO% TimeStamp = '%TIMESTAMP%'

SET LIBRARY=%SOURCE%\bin\%CONFIGURATION%\bin\Eagle.dll
SET TASKS=%SOURCE%\bin\%CONFIGURATION%\bin\EagleTasks.dll
SET CMDLETS=%SOURCE%\bin\%CONFIGURATION%\bin\EagleCmdlets.dll
SET EXTENSIONS=%SOURCE%\bin\%CONFIGURATION%\bin\EagleExtensions.dll
SET SHELL=%SOURCE%\bin\%CONFIGURATION%\bin\EagleShell.exe
SET UPDATE=%SOURCE%\bin\%CONFIGURATION%\bin\Hippogriff.exe
SET ALIAS=%SOURCE%\bin\%CONFIGURATION%\bin\es.exe
SET UTILITY_X86=%SOURCE%\bin\%CONFIGURATION%\bin\x86\Spilornis.dll
SET UTILITY_X64=%SOURCE%\bin\%CONFIGURATION%\bin\x64\Spilornis.dll

%_VECHO% Source = '%SOURCE%'
%_VECHO% Library = '%LIBRARY%'
%_VECHO% Tasks = '%TASKS%'
%_VECHO% Shell = '%SHELL%'
%_VECHO% Alias = '%ALIAS%'
%_VECHO% UtilityX86 = '%UTILITY_X86%'
%_VECHO% UtilityX64 = '%UTILITY_X64%'

CALL :fn_ResetErrorLevel

IF EXIST "%LIBRARY%" (
  %_AECHO% Signing file "%LIBRARY%"...
  %_CECHO% SignCode.exe -a sha1 -n "Eagle" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%LIBRARY%"
  %__ECHO% SignCode.exe -a sha1 -n "Eagle" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%LIBRARY%"

  CALL :fn_CheckErrorLevel

  IF ERRORLEVEL 1 (
    ECHO Failed to sign file "%LIBRARY%".
    GOTO errors
  )
)

IF EXIST "%TASKS%" (
  %_AECHO% Signing file "%TASKS%"...
  %_CECHO% SignCode.exe -a sha1 -n "Eagle Tasks" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%TASKS%"
  %__ECHO% SignCode.exe -a sha1 -n "Eagle Tasks" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%TASKS%"

  CALL :fn_CheckErrorLevel

  IF ERRORLEVEL 1 (
    ECHO Failed to sign file "%TASKS%".
    GOTO errors
  )
)

IF EXIST "%CMDLETS%" (
  %_AECHO% Signing file "%CMDLETS%"...
  %_CECHO% SignCode.exe -a sha1 -n "Eagle Cmdlets" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%CMDLETS%"
  %__ECHO% SignCode.exe -a sha1 -n "Eagle Cmdlets" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%CMDLETS%"

  CALL :fn_CheckErrorLevel

  IF ERRORLEVEL 1 (
    ECHO Failed to sign file "%CMDLETS%".
    GOTO errors
  )
)

IF EXIST "%EXTENSIONS%" (
  %_AECHO% Signing file "%EXTENSIONS%"...
  %_CECHO% SignCode.exe -a sha1 -n "Eagle Extensions" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%EXTENSIONS%"
  %__ECHO% SignCode.exe -a sha1 -n "Eagle Extensions" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%EXTENSIONS%"

  CALL :fn_CheckErrorLevel

  IF ERRORLEVEL 1 (
    ECHO Failed to sign file "%EXTENSIONS%".
    GOTO errors
  )
)

IF EXIST "%SHELL%" (
  %_AECHO% Signing file "%SHELL%"...
  %_CECHO% SignCode.exe -a sha1 -n "Eagle Shell" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%SHELL%"
  %__ECHO% SignCode.exe -a sha1 -n "Eagle Shell" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%SHELL%"

  CALL :fn_CheckErrorLevel

  IF ERRORLEVEL 1 (
    ECHO Failed to sign file "%SHELL%".
    GOTO errors
  ) ELSE (
    %__ECHO% ECHO F %PIPE% XCOPY "%SHELL%" "%ALIAS%" %FLAGS% %DFLAGS%

    IF ERRORLEVEL 1 (
      ECHO Failed to copy "%SHELL%" to "%ALIAS%".
      GOTO errors
    )
  )
)

IF EXIST "%UPDATE%" (
  %_AECHO% Signing file "%UPDATE%"...
  %_CECHO% SignCode.exe -a sha1 -n "Eagle Updater" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%UPDATE%"
  %__ECHO% SignCode.exe -a sha1 -n "Eagle Updater" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%UPDATE%"

  CALL :fn_CheckErrorLevel

  IF ERRORLEVEL 1 (
    ECHO Failed to sign file "%UPDATE%".
    GOTO errors
  )
)

IF EXIST "%UTILITY_X86%" (
  %_AECHO% Signing file "%UTILITY_X86%"...
  %_CECHO% SignCode.exe -a sha1 -n "Eagle Native Utility Library (Spilornis)" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%UTILITY_X86%"
  %__ECHO% SignCode.exe -a sha1 -n "Eagle Native Utility Library (Spilornis)" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%UTILITY_X86%"

  CALL :fn_CheckErrorLevel

  IF ERRORLEVEL 1 (
    ECHO Failed to sign file "%UTILITY_X86%".
    GOTO errors
  )
)

IF EXIST "%UTILITY_X64%" (
  %_AECHO% Signing file "%UTILITY_X64%"...
  %_CECHO% SignCode.exe -a sha1 -n "Eagle Native Utility Library (Spilornis)" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%UTILITY_X64%"
  %__ECHO% SignCode.exe -a sha1 -n "Eagle Native Utility Library (Spilornis)" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%UTILITY_X64%"

  CALL :fn_CheckErrorLevel

  IF ERRORLEVEL 1 (
    ECHO Failed to sign file "%UTILITY_X64%".
    GOTO errors
  )
)

GOTO no_errors

:fn_UnquoteVariable
  IF NOT DEFINED %1 GOTO :EOF
  SETLOCAL
  SET __ECHO_CMD=ECHO %%%1%%
  FOR /F "delims=" %%V IN ('%__ECHO_CMD%') DO (
    SET VALUE=%%V
  )
  SET VALUE=%VALUE:"=%
  REM "
  ENDLOCAL && SET %1=%VALUE%
  GOTO :EOF

:fn_ResetErrorLevel
  VERIFY > NUL
  GOTO :EOF

:fn_SetErrorLevel
  VERIFY MAYBE 2> NUL
  GOTO :EOF

:fn_CheckErrorLevel
  IF %ERRORLEVEL% NEQ 0 (
    CALL :fn_SetErrorLevel
  )
  GOTO :EOF

:usage
  ECHO.
  ECHO Usage: %~nx0 [configuration]
  ECHO.
  ECHO The PVK_FILE environment variable must be set to the full path and file name of
  ECHO an existing private key file.
  ECHO.
  ECHO The SPC_FILE environment variable must be set to the full path and file name of
  ECHO an existing certificate file.
  ECHO.
  ECHO The SIGN_URL environment variable should be set to the URL containing further
  ECHO information about the file to be signed ^(e.g. the URL for the company web site^).
  GOTO errors

:errors
  CALL :fn_SetErrorLevel
  ENDLOCAL
  ECHO.
  ECHO Signing failure, errors were encountered.
  GOTO end_of_file

:no_errors
  CALL :fn_ResetErrorLevel
  ENDLOCAL
  ECHO.
  ECHO Signing success, no errors were encountered.
  GOTO end_of_file

:end_of_file
%__ECHO% EXIT /B %ERRORLEVEL%
