@ECHO OFF

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

REM SET DFLAGS=/L

%_VECHO% DFlags = '%DFLAGS%'

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

%_VECHO% Flags = '%FLAGS%'

SET FFLAGS=/V /F /G /H /I /R /Y /Z

%_VECHO% FFlags = '%FFLAGS%'

SET CONFIGURATION=%2

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

%_VECHO% Configuration = '%CONFIGURATION%'

SET TARGET=%1

IF NOT DEFINED TARGET (
  GOTO usage
)

CALL :fn_UnquoteVariable TARGET

SET DUMMY2=%3

IF DEFINED DUMMY2 (
  GOTO usage
)

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

%_VECHO% Source = '%SOURCE%'
%_VECHO% Target = '%TARGET%'

SET LIBTESTFILES=all.eagle constraints.eagle epilogue.eagle pkgIndex.eagle
SET LIBTESTFILES=%LIBTESTFILES% pkgIndex.tcl prologue.eagle

%_VECHO% LibTestFiles = '%LIBTESTFILES%'

CALL :fn_ResetErrorLevel

%__ECHO% XCOPY "%SOURCE%\bin\%CONFIGURATION%\bin" "%TARGET%\bin" %FLAGS% %DFLAGS%

IF ERRORLEVEL 1 (
  ECHO Failed to copy "%SOURCE%\bin\%CONFIGURATION%\bin" to "%TARGET%\bin".
  GOTO errors
)

%__ECHO% XCOPY "%SOURCE%\bin\%CONFIGURATION%\lib" "%TARGET%\lib" %FLAGS% %DFLAGS%

IF ERRORLEVEL 1 (
  ECHO Failed to copy "%SOURCE%\bin\%CONFIGURATION%\lib" to "%TARGET%\lib".
  GOTO errors
)

IF NOT DEFINED NOLIB (
  %__ECHO% XCOPY "%SOURCE%\lib" "%TARGET%\lib" %FLAGS% %DFLAGS%

  IF ERRORLEVEL 1 (
    ECHO Failed to copy "%SOURCE%\lib" to "%TARGET%\lib".
    GOTO errors
  )
)

IF NOT DEFINED NOTESTS (
  %__ECHO% XCOPY "%SOURCE%\Library\Tests" "%TARGET%\Tests" %FLAGS% %DFLAGS%

  IF ERRORLEVEL 1 (
    ECHO Failed to copy "%SOURCE%\Library\Tests" to "%TARGET%\Tests".
    GOTO errors
  )
) ELSE (
  IF NOT DEFINED NOLIB (
    IF DEFINED LIBTESTFILES (
      IF NOT EXIST "%TARGET%\lib\Test1.0" (
        %__ECHO% MKDIR "%TARGET%\lib\Test1.0"

        IF ERRORLEVEL 1 (
          ECHO Could not create directory "%TARGET%\lib\Test1.0".
          GOTO errors
        )
      )

      FOR %%F IN (%LIBTESTFILES%) DO (
        %__ECHO% XCOPY "%SOURCE%\Library\Tests\%%F" "%TARGET%\lib\Test1.0" %FLAGS% %DFLAGS%

        IF ERRORLEVEL 1 (
          ECHO Failed to copy "%SOURCE%\Library\Tests\%%F" to "%TARGET%\lib\Test1.0".
          GOTO errors
        )
      )
    )
  )
)

IF NOT DEFINED NORELEASE (
  %__ECHO% XCOPY "%SOURCE%\README" "%TARGET%" %FFLAGS% %DFLAGS%

  IF ERRORLEVEL 1 (
    ECHO Failed to copy "%SOURCE%\README" to "%TARGET%".
    GOTO errors
  )

  %__ECHO% XCOPY "%SOURCE%\license.terms" "%TARGET%" %FFLAGS% %DFLAGS%

  IF ERRORLEVEL 1 (
    ECHO Failed to copy "%SOURCE%\license.terms" to "%TARGET%".
    GOTO errors
  )

  %__ECHO% XCOPY "%SOURCE%\*.url" "%TARGET%" %FFLAGS% %DFLAGS%

  IF ERRORLEVEL 1 (
    ECHO Failed to copy "%SOURCE%\*.url" to "%TARGET%".
    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

:usage
  ECHO.
  ECHO Usage: %~nx0 ^<target^> [configuration]
  GOTO errors

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

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

:end_of_file
%__ECHO% EXIT /B %ERRORLEVEL%
