@echo off

set tmpHPSUM=""

for /f "eol=# tokens=1,2* delims==" %%i in (' findstr "temp_dir" hpsum.ini ') do (set tmpHPSUM=%%j)
set tmpHPSUM=%tmpHPSUM:/=\%

IF %tmpHPSUM%=="" (
  set tmpHPSUM=%tmp%
)
	
IF "%1"=="/h" (
  echo syntax: %0 [/y] [/h]
  echo /y = continue without pausing for prompts
  goto :EOF
)

IF "%1"=="/y" (
  set SKIP=1
) ELSE (
  set SKIP=0
)


echo HP Smart Update Manager cached data delete script
echo                       -
echo Removing HP SUM cache files will require re-entering
echo all baseline locations, nodes and credentials.
echo                       -
echo This script should not be run while HP SUM is running.
echo                       -
REM Look for HP SUM processes.  Returns '0' if found, '1' if not found
tasklist | find "hpsum"
if %errorlevel% EQU 0 (
  echo                       -
  echo ========================================================
  echo WARNING: HP SUM instances were found currently running.
  echo Please exit HP SUM using Logout and shutdown from the 
  echo browser running HP SUM or use 'hpsum shutdownengine'
  echo before calling clean-cache to prevent file conflicts.
  echo ========================================================
  echo                       -
  REM This is now only a warning because it might be a script or other process with hpsum in the name (SIM)
  REM goto end
  )
  
IF %SKIP%==1 goto oktodelete
set /p ok="Are you sure you want to delete HP SUM cache files? [y or n] : "

if "%ok%"=="y" goto oktodelete
echo Exiting without deleting any files.
goto end

:oktodelete
IF EXIST %tmpHPSUM%\HPSUM (

  IF EXIST %tmpHPSUM%\HPSUM\baseline (
    echo Removing %tmpHPSUM%\HPSUM\baseline
    rmdir /s /Q %tmpHPSUM%\HPSUM\baseline
  ) ELSE (
    echo %tmpHPSUM%\HPSUM\baseline already deleted.
  )

  FOR %%J IN ( hpsum.pdb hpsum.pdb-wal hpsum.pdb-shm hpsum_remote.pdb ) DO (
     FOR /F "usebackq" %%I in (`dir /s /b %tmpHPSUM%\HPSUM ^| find "%%J"`) DO ( 
	        echo Removing %%I
			erase /Q %%I
	 )
  )

  FOR /F "usebackq" %%P IN (`dir /ad /b %tmpHPSUM%\HPSUM ^| findstr /B /R ._._._.`) DO (
        echo Removing directory %tmpHPSUM%\HPSUM\%%P
        rmdir /S /Q %tmpHPSUM%\HPSUM\%%P
  )

) ELSE (
  echo No HP SUM temp directory found to delete. (%tmpHPSUM%\HPSUM)
)

:end
IF %SKIP%==0 (
  pause
)

