@echo off

@REM :
@REM : Author:  John Thomas - Wed Jan 18, 2006,  1:30 PM
@REM :

setlocal enabledelayedexpansion

@REM : Variables used:
@REM :
@REM : "MyTryCmd" should be set to log the command returning an error status.
@REM : "MyErrLvl" should be set to log the ERRORLEVEL associated with the
@REM :            error.
@REM : "MyErrMsg" should be set to log the reason for the error.
@REM : "MyErrSec" should be set to log the section of this script detecting
@REM :            the error.
@REM : "MySrc"    should be set to the path of the vendor source files.


@REM : Initialize the MyTryCmd (command-line image), MyErrMsg (error message),
@REM :   MyErrLvl (ERRORLEVEL), and MyErrSec (code section) to nothing.
@REM :   These variables will be used in error logging.
set MyTryCmd=
set MyErrMsg=
set MyErrLvl=
set MyErrSec=

set MySrc=C:\hp\tmp\src
set MyAuditModeFlag=0

@REM : A Link to Adobe icon on the desktop is not allowed for Adobe by
@REM : current specifications.  Therefore set the DeskTop Link flag to 0.
set DTLinkFlag=0
  
@REM :---------------------------------------------------------------------
@REM : Section 0.0
@REM :---------------------------------------------------------------------
set MyErrSec=0.0

@REM :---------------------------------------------------------------------
@REM : Block Template:
@REM :---------------------------------------------------------------------
@REM : Begin Automatic Desktop Link Name Processing (part 1)
@REM : 
@REM : Save all original desktop shortcuts prior to running setup.  We'll
@REM : use this list to compare with the desktop shortcuts existing after
@REM : setup runs.  Then we can determine which desktop link is new in this
@REM : install.  The goal is to have an algorithm that works for any
@REM : linkname change and be language-independent.  
@REM : 
@REM :---------------------------------------------------------------------

@REM : Save directory name containing desktop links prior to running setup.
set MyDtLinksSave=\hp\tmp\DesktopLinksSave

if not exist %MyDtLinksSave% (
   set MyTryCmd=md %MyDtLinksSave%
   set MyErrMsg=

   !MyTryCmd!
   if errorlevel 1 (
      call :get_errorlevel

      @REM : Note: Never do a "set" command between the command setting the
      @REM :   the errorevel and where errorlevel is being checked because
      @REM :   "set" destroys the ERRORLEVEL value.

      set MyErrMsg=Setup failed.  ErrorLevel= !MyErrLvl!.
      goto :error
      )

   if not exist %MyDtLinksSave% (
      set MyErrMsg=Attempt to create Desktop Links Save directory failed.
      )

   ) else (
      @REM : Ensure no pre-exiting Desktop links are saved.
      del /f /q \hp\tmp\DesktopLinksSave\
      )

@REM :---------------------------------------------------------------------
@REM : Section 0.1
@REM :---------------------------------------------------------------------
set MyErrSec=0.1

copy "%COMMON DESKTOP%\*" "%MyDtLinksSave%\."


@REM :---------------------------------------------------------------------
@REM : End Automatic Desktop Link Name Processing (part 1 of 2)
@REM :---------------------------------------------------------------------


@REM : The startup folder link names are language localized.  Therefore
@REM : let's copy what we find already existing in the Startup folder to a
@REM : temporary backup directory.  Prepare for error reporting.  

@REM :---------------------------------------------------------------------
@REM : Section 0.2
@REM :---------------------------------------------------------------------
set MyErrSec=0.2

set MyTryCmd=xcopy /y /i "%COMMON STARTUP%\*" ".\StartupFolderSave\"
set MyErrLvl=
set MyErrMsg=

@REM : Make sure the temporary directory gets created.  Xcopy won't create the
@REM : directory if the source is empty.
md \hp\tmp\StartupFolderSave

%MyTryCmd%
if errorlevel 1 (
   call :save_errorlevel
   goto :xcopy_error
   )


@REM :---------------------------------------------------------------------
@REM : Section 1.0
@REM :---------------------------------------------------------------------
set MyErrSec=1.0

@reg query HKLM\System\Setup | find /i "AuditInProgress" | find /i "0x1" >NUL && set MyAuditModeFlag=1

@REM : Define jtDebugFlag in when we want to test as if in Audit Mode.
if defined jtDebugFlag set MyAuditModeFlag=1


@REM : Determine the block name in case we need it for error reporting.
for %%f in (*.block) do set BlockName=%%~nf
if "%BlockName%"=="" set BlockName=[ Block name not found ]

if not exist %MySrc%\OUTPUTDIR_%lcode%\setup.exe (
   set MyTryCmd=if not exist %MySrc%\OUTPUTDIR_%lcode%\setup.exe...
   set MyErrMsg=Setup file non-existent. Check filename. Check OS language setting.
   set MyErrLvl=
   goto :error
   )


@REM :---------------------------------------------------------------------
@REM : Section 1.1
@REM :---------------------------------------------------------------------
set MyErrSec=1.1
echo MyErrSec=%MyErrSec%


@REM : Run the cmd.exe installation program.
\hp\bin\cmd.exe /c install2.cmd


goto :EOF



@REM :---------------------------------------------------------------------
@REM : The %ERRORLEVEL% environment variable is not very persistent.
@REM : Therefore this inefficient but simple routine saves the %ERRORLEVEL%
@REM : in MyErrLvl.
@REM : Note: In order to have this routine function properly, MyErrLvl
@REM : must not be defined upon calling this routine.
@REM :---------------------------------------------------------------------
:save_errorlevel
@for /L %%i in ( 32767, -1, 0 ) do @(
   if errorlevel %%i (
      if not defined MyErrLvl set MyErrLvl=%%i
      )
   )

exit /b %MyErrLvl%


@REM :---------------------------------------------------------------------
@REM : Translate errorlevel to a reason in text for the xcopy failure.
@REM :---------------------------------------------------------------------
:xcopy_error
set MyErrMsg=

if %MyErrLvl% EQU 5 (
   set MyErrMsg=xcopy errorlevel 5: Disk write error occurred.
   )

if %MyErrLvl% EQU 4 (
   set MyErrMsg=xcopy errorlevel 4: Insufficient memory or disk space, invalid drive name, or invalid syntax on command line.
   )

@REM : There is no error text associated with errorlevel 3.

if %MyErrLvl% EQU 2 (
   set MyErrMsg=xcopy errorlevel 2: The user pressed CTRL+C to terminate xcopy.
   )

if %MyErrLvl% EQU 1 (
   set MyErrMsg=xcopy errorlevel 1: No files were found to copy.
   )

@REM : If we don't know the error decode, log what little we do know.
if "%MyErrMsg%"=="" (
   set MyErrMsg= xcopy: ErrorLevel=%MyErrLvl%, Unknown error.
   )


@REM :---------------------------------------------------------------------
@REM : Block Template:
@REM : Log error to error.txt file.
@REM : Note: Build tools will use the existence of c:\blocks\error.txt as
@REM :   an indicator that an error occurred and stop the build.  The build
@REM :   tools append the error text to "C:\hp\support\finis_it.txt" and/or
@REM :   "C:\hp\support\mlerr.txt".
@REM :---------------------------------------------------------------------
:error
@if exist \blocks\allblocks (
                       echo %BlockName%: %date% %time% - Error detected by install.cmd, code section %MyErrSec% >> \blocks\error.txt
   if defined MyTryCmd echo %BlockName%: CMD=%MyTryCmd% >> \blocks\error.txt
                       echo %BlockName%: ERR=%MyErrMsg% >> \blocks\error.txt
) else (
                       echo %BlockName%: %date% %time% - Error detected by install.cmd, code section %MyErrSec% >> \hp\support\error.txt
   if defined MyTryCmd echo %BlockName%: CMD=%MyTryCmd% >> \hp\support\error.txt
                       echo %BlockName%: ERR=%MyErrMsg% >> \hp\support\error.txt
)

:EOF

set BlockName=
set DTLinkFlag=
set MyAuditModeFlag=
set MyDtLinksSave=
set MyErrLvl=
set MyErrMsg=
set MyErrSec=
set MySrc=
set MyTryCmd=

endlocal
exit


