@echo off

@REM :
@REM : Author:  John Thomas - Fri Mar 2, 2007,  9:30 PM
@REM :
@REM : Install part 2

echo install2.cmd 

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

setlocal enabledelayedexpansion

@REM : #defined constant(s)
set ERROR_SUCCESS_REBOOT_REQUIRED=3010

set MyErrLvl=

time /t
set MyTryCmd=\hp\tmp\src\OUTPUTDIR_%lcode%\AcroRead.msi /qn REBOOT=ReallySuppress
%MyTryCmd%
if errorlevel 1 (
   call :save_errorlevel
   set MyErrMsg=errorlevel is !MyErrLvl!
   if not "!MyErrLvl!"=="%ERROR_SUCCESS_REBOOT_REQUIRED%" goto :error
   )
time /t


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

@REM :---------------------------------------------------------------------
@REM : The Startup directory may have been added to by the install. We
@REM : don't want this, so restore the original startup menu from the
@REM : temp directory that we made and then delete the temp directory.
@REM :---------------------------------------------------------------------

echo Remove Startup Links
pushd %common startup%
del /f /q *
popd

@REM :---------------------------------------------------------------------
@REM : Section 2.2
@REM :---------------------------------------------------------------------
set MyErrSec=2.2
echo MyErrSec=%MyErrSec%
echo Restore Original Startup Links

set Found=0
for %%i in (\hp\tmp\StartupFolderSave\*) do set Found=1

@REM : Restore Startup links back to original.
set MyTryCmd=move /y \hp\tmp\StartupFolderSave\* "%COMMON STARTUP%"
set MyErrLvl=
set MyErrMsg=

if "%Found%"=="1" (
   %MyTryCmd%
   if errorlevel 1 (
      call :save_errorlevel
      set MyErrMsg=errorlevel is !MyErrLvl!
      )
   )


@REM : ---------------------------------------------------------------------
@REM : JGT Block Template:
@REM : ---------------------------------------------------------------------
@REM : Begin Automatic Desktop Link Name Processing (part 2)
@REM : ---------------------------------------------------------------------
@REM : Search for the desktop link added by setup.  The new link will exist
@REM : in the %COMMON DESKTOP% folder but not in .\DesktopLinksSave\.  Once
@REM : we have the name of the new desktop link, we can determine by the
@REM : SWIT POR if we should keep it or not.  
@REM : ---------------------------------------------------------------------
echo
echo Desktop Link Processing
echo
for %%l in ( "%COMMON DESKTOP%\*.lnk" ) do (
   echo Common Desktop= %%l
   if exist "%MyDtLinksSave%\%%~nxl" echo "%MyDtLinksSave%\%%~nxl" exists

   if not exist "%MyDtLinksSave%\%%~nxl" (
      echo "%MyDtLinksSave%\%%~nxl" does not exist

      @REM : If control reaches here, we know that setup has added a new
      @REM : Desktop Link.

      @REM :---------------------------------------------------------------
      @REM : Block Template:
      @REM : If running in Audit mode, save Desktop Links in \hp\dtshortcuts.
      @REM :---------------------------------------------------------------
      if "%MyAuditModeFlag%"=="1" (

         @REM :------------------------------------------------------------
         @REM : If Desktop Links are desired (i.e., DTLinkFlag is 1), copy our
         @REM : app link to the desktop but do it such that there will not
         @REM : be duplicate icons appearing on the desktop.  
         @REM :------------------------------------------------------------
         if "%DTLinkFlag%"=="1" (

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

            @REM : If non-existent, create the desktop links folder and copy the
            @REM : desktop links to it.

            @REM : Prepare for error reporting.
            set MyTryCmd=xcopy /y /i "%%l" "%DTShortCutsFolder%\"
            set MyErrLvl=
            set MyErrMsg=

            !MyTryCmd!
            if errorlevel 1 (
               call :get_errorlevel
               goto :xcopy_error
               )

            )

         @REM :------------------------------------------------------------
         @REM : Remove the potentially duplicate link from the desktop.  The
         @REM : build process will restore all desktop links from
         @REM : \hp\dtshortcuts i.e., %DTShortCutsFolder% later at the end of
         @REM : the build process.  

         @REM : Then only a single instance of this application's link
         @REM : should remain showing on the desktop.  This technique
         @REM : leveraged from Doug Jaccard.  
         @REM :------------------------------------------------------------

         @REM : Remember during debugging: Prior to first boot completion, the
         @REM : desktop icon will not appear.
         del /f /q "%%l"

      ) else (

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

         @REM : We're ***NOT*** running in Audit mode.  Therefore, if the
         @REM : DTLinkFlag flag is not 1, remove the link from the desktop.  

         if not "%DTLinkFlag%" == "1" (
            if exist "%%l" (
               del /f /q "%%l"
               )

            )

         )

      )

   )

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

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 install2.cmd in 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 in 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



