@echo off
setlocal DisableDelayedExpansion
rem ** At this point the delayedExpansion should be disabled
rem ** otherwise an exclamation mark in %* will be interpreted as special command
SET HPSUM_CMD_ARGS=%*

setlocal EnableDelayedExpansion

SET HPSUM_ROOT=%~dp0

::remove trailing back slash
IF "!HPSUM_ROOT:~-1!"=="\" SET HPSUM_ROOT=!HPSUM_ROOT:~0,-1!

SET dstpath=!HPSUM_ROOT!

SET REMOTE=0
::check whether it is Virtual mapped Drive 
::check whether it is N/W mapped Drive or UNC path
set virtualdrive=%~d0 - CD-ROM Drive
FOR /F "tokens=*" %%A IN ('"fsutil fsinfo drivetype"" "%~d0""') do (
      if /i "%%A"=="!virtualdrive!" (
            echo %~d0 is a CD-ROM Drive
            set REMOTE=1
            set dstpath=%temp%\localhpsum
            goto :run
      )
)
IF "!HPSUM_ROOT:~0,2!"=="\\" (
    echo %HPSUM_ROOT is a remote UNC path
    set REMOTE=1
    set dstpath=%temp%\localhpsum
) ELSE (
    for /f "skip=6 tokens=2" %%d in ('net use') do (
        if /i "!HPSUM_ROOT:~0,2!"=="%%d" (
            echo !HPSUM_ROOT! is a Network Mapped Drive
            set REMOTE=1
            set dstpath=%temp%\localhpsum
            goto :run
        )
    )
)

:run
set newcmdcmdline=%cmdcmdline:"=-%
set withincmd=1
echo %newcmdcmdline% | find /i "cmd /c --%~dpf0%-"
if %errorlevel% EQU 0 set withincmd=0
:: if %withincmd% EQU 0 echo This batch-file: %~dpf0 was executed directly (from Windows Explorer, ...).
:: if %withincmd% EQU 1 echo This batch-file: %~dpf0 was executed from within a Command Prompt

:: Select 32-bit or 64-bit
SET HostIs64Bit=%ProgramFiles(x86)%
IF "%HostIs64Bit%"=="" (
    set HPSUM_ARCH=x86
) ELSE (
    set HPSUM_ARCH=x64
)

IF %REMOTE%==1 (
    echo Copying HP SUM files from !HPSUM_ROOT! to !dstpath!
    IF NOT EXIST !dstpath! (
        md !dstpath!
    )
    
    SET INPUTFILENAME=
    SET INPUTFILEPATH=
    FOR %%A IN (!HPSUM_CMD_ARGS!) DO (      
        IF "!INPUTFILENAME!"=="1" (
           SET INPUTFILEPATH=%%A 
           goto :copyFiles
        )
        IF "%%A"=="/inputfile" (
           SET INPUTFILENAME=1
        )
    )
:copyFiles
    ::echo INPUTFILEPATH:%INPUTFILEPATH%
    xcopy /s /y /i /q !HPSUM_ROOT!\%HPSUM_ARCH% !dstpath!\%HPSUM_ARCH%
    xcopy /s /y /i /q !HPSUM_ROOT!\assets !dstpath!\assets
    xcopy /s /y /i /q !HPSUM_ROOT!\cygwin !dstpath!\cygwin
    xcopy /y /i /q !HPSUM_ROOT!\masterdependency.xml !dstpath!\
    xcopy /y /i /q !HPSUM_ROOT!\nic_pciid.xml !dstpath!\
    IF EXIST %INPUTFILEPATH% xcopy /y /i /q %INPUTFILEPATH% !dstpath!\
)

set HPSUM_BIN="!dstpath!\%HPSUM_ARCH%\hpsum_bin_%HPSUM_ARCH%.exe"

IF NOT EXIST %HPSUM_BIN% (
    @echo Unable to locate %HPSUM_BIN%
    IF %withincmd% EQU 0 (
        call :delay
    )
    set ERRORCODE=-4
    goto :Exit
)

%HPSUM_BIN% !HPSUM_CMD_ARGS!
set ERRORCODE=%ERRORLEVEL%

:: the acutal binary should have displayed an error
:: message.  We still want to delay to allow
:: people who started by clicking on the icon
:: a chance to see the message
IF %ERRORCODE% NEQ 0 (
    if %withincmd% EQU 0 (
        call :delay
    )
    goto :Exit
)    

goto :Exit

:delay
where ping > nul 2>&1
IF %ERRORLEVEL% NEQ 0 (
    pause
) else (
    @echo Pausing for 10 seconds ...
    ping -n 11 127.0.0.1 > nul
)
goto :EOF

:Exit
exit /b %ERRORCODE%
