@ECHO OFF

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

%_AECHO% Running %0 %*

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

SET FILENAME=%1

IF DEFINED FILENAME (
  CALL :fn_UnquoteVariable FILENAME
) ELSE (
  GOTO usage
)

%_VECHO% FileName = '%FILENAME%'

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.

  IF DEFINED VIA_BUILD (
    GOTO usage
  ) ELSE (
    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.

  IF DEFINED VIA_BUILD (
    GOTO usage
  ) ELSE (
    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%'

CALL :fn_ResetErrorLevel

IF EXIST "%FILENAME%" (
  %_AECHO% Signing file "%FILENAME%"...
  %_CECHO% "%SOURCE%\Library\Tools\SignCode.exe" -a sha1 -n "Eagle Package for Tcl (Garuda)" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%FILENAME%"
  %__ECHO% "%SOURCE%\Library\Tools\SignCode.exe" -a sha1 -n "Eagle Package for Tcl (Garuda)" -i "%SIGN_URL%" -v "%PVK_FILE%" -spc "%SPC_FILE%" %TIMESTAMP% "%FILENAME%"

  CALL :fn_CheckErrorLevel

  IF ERRORLEVEL 1 (
    ECHO Failed to sign file "%FILENAME%".
    GOTO errors
  )
) ELSE (
  ECHO Cannot sign file "%FILENAME%", it does not exist.
  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
  IF DEFINED VIA_BUILD (
    GOTO no_errors
  ) ELSE (
    ECHO.
    ECHO Usage: %~nx0 [fileName]
    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^).
  )

:errors
  CALL :fn_SetErrorLevel
  ENDLOCAL
  IF NOT DEFINED VIA_BUILD (
    ECHO.
    ECHO Signing failure, errors were encountered.
  )
  GOTO end_of_file

:no_errors
  CALL :fn_ResetErrorLevel
  ENDLOCAL
  IF NOT DEFINED VIA_BUILD (
    ECHO.
    ECHO Signing success, no errors were encountered.
  )
  GOTO end_of_file

:end_of_file
%__ECHO% EXIT /B %ERRORLEVEL%
