@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

rem remove trailing back slash
IF "!HPSUM_ROOT:~-1!"=="\" SET HPSUM_ROOT=!HPSUM_ROOT:~0,-1!

SET dstpath=!HPSUM_ROOT!

SET REMOTE=0
rem check whether it is Virtual mapped Drive 
rem 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
rem if %withincmd% EQU 0 echo This batch-file: %~dpf0 was executed directly (from Windows Explorer, ...).
rem if %withincmd% EQU 1 echo This batch-file: %~dpf0 was executed from within a Command Prompt

rem Select 32-bit or 64-bit
SET HostIs64Bit=%ProgramFiles(x86)%
IF "%HostIs64Bit%"=="" (
    set HPSUM_ARCH=x86
) ELSE (
    set HPSUM_ARCH=x64
)

rem Check if any of the following switches were passed as arguments.  
rem They will not yet require a copy of the HPSUM Files if remote.
rem check for: /v, /version (version information)
rem check for: /h, /?, /help (help)

rem check for silent as it is a different mode for HPSUM
rem check for: /s, /silent

rem Initialize
SET INPUTFILEPATH=
SET USELOCATION=0
SET LEGACYMODE=0
SET ERRORCODE=0

:loop
IF NOT "%~1"=="" (

rem Get if silent flag is set. Treat it exactly like express_install.
    IF "%~1"=="/s" SET LEGACYMODE=1
    IF "%~1"=="/silent" SET LEGACYMODE=1

rem Get if express_install flag is set. Treat it exactly like silent mode.
    IF "%~1"=="/express_install" SET LEGACYMODE=1

rem Handle working directory
    IF "%~1"=="/use_location" (
        SET USELOCATION=1
        SHIFT
    )

rem Handle input file
    IF "%~1"=="/inputfile" (
        SET INPUTFILEPATH="%~2"
        SHIFT
    )
rem handle logdir	
	IF "%~1"=="/logdir" (
        SHIFT
    )
rem Handle version
    IF "%~1"=="/v" SET VERSION=1
    IF "%~1"=="/version" SET VERSION=1

rem Handle help
    IF "%~1"=="/h" SET HELP=1
    IF "%~1"=="/help" SET HELP=1
    IF "%~1"=="/?" SET HELP=1

    SHIFT
    GOTO :loop
)


rem If help or version commands were detected, then skip the copy and execute HPSUM
IF defined HELP GOTO :runhpsum
IF defined VERSION GOTO :runhpsum
IF %REMOTE% EQU 0 GOTO :runhpsum


rem Check if there's an existing hpsum service running..
tasklist /FI "IMAGENAME eq hpsum_service_%HPSUM_ARCH%.exe" 2>NUL | find /I /N "hpsum_service_%HPSUM_ARCH%.exe" >NUL

IF %ERRORLEVEL% NEQ 0 (
    echo Copying HP SUM files from !HPSUM_ROOT! to !dstpath!

    IF NOT EXIST !dstpath! (        
        md !dstpath! 
        
        rem Check if the folder is now present.
        IF NOT EXIST !dstpath! (
            echo Could not create the temporary directory !dstpath!
            SET ERRORCODE=-4
            goto :WrapUp
        )
    )

    xcopy /s /y /i /q "!HPSUM_ROOT!\%HPSUM_ARCH%" "!dstpath!\%HPSUM_ARCH%"
    SET ERRORCODE=%ERRORLEVEL%
    IF !ERROCODE! NEQ 0 (
        SET ERRORCODE=-4
        goto :WrapUp
    )

    xcopy /s /y /i /q "!HPSUM_ROOT!\assets" "!dstpath!\assets"
    SET ERRORCODE=%ERRORLEVEL%
    IF !ERROCODE! NEQ 0 (
        SET ERRORCODE=-4
        goto :WrapUp
    )

    xcopy /s /y /i /q "!HPSUM_ROOT!\cygwin" "!dstpath!\cygwin"
    SET ERRORCODE=%ERRORLEVEL%
    IF !ERROCODE! NEQ 0 (
        SET ERRORCODE=-4
        goto :WrapUp
    )

    xcopy /y /i /q "!HPSUM_ROOT!\masterdependency.xml" "!dstpath!\"
    SET ERRORCODE=%ERRORLEVEL%
    IF !ERROCODE! NEQ 0 (
        SET ERRORCODE=-4
        goto :WrapUp
    )

    xcopy /y /i /q "!HPSUM_ROOT!\nic_pciid.xml" "!dstpath!\"
    SET ERRORCODE=%ERRORLEVEL%
    IF !ERROCODE! NEQ 0 (
        SET ERRORCODE=-4
        goto :WrapUp
    )

    IF EXIST "%INPUTFILEPATH%" xcopy /y /i /q "%INPUTFILEPATH%" "!dstpath!\"
    SET ERRORCODE=%ERRORLEVEL%
    IF !ERROCODE! NEQ 0 (
        SET ERRORCODE=-4
        goto :WrapUp
    )

    rem If working directory is not set, and it is a read-only then append use_location with the mount point path
    IF %LEGACYMODE% EQU 1 (
        IF %USELOCATION% NEQ 1 (
            SET "HPSUM_CMD_ARGS=!HPSUM_CMD_ARGS! /use_location "!HPSUM_ROOT!""
        )
    )

) ELSE (
    IF %LEGACYMODE% EQU 1 (
        rem ONLY if a silent switch is detected, do not allow running of hpsum again. 
        rem Otherwise, let it through as it could be interactive commands.
        echo An existing HP SUM session is detected.
        set ERRORCODE=-4
        goto :WrapUp
rem        IF %withincmd% EQU 0 (
rem            call :delay
rem        )
rem        GOTO :Exit
    )
)


:runhpsum
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
)

echo %HPSUM_BIN% !HPSUM_CMD_ARGS!

%HPSUM_BIN% !HPSUM_CMD_ARGS!

set ERRORCODE=%ERRORLEVEL%

rem the acutal binary should have displayed an error
rem message.  We still want to delay to allow
rem people who started by clicking on the icon
rem a chance to see the message
:WrapUp
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%
