@Echo off
REM --------------------------------------------------------------------------
REM  "Kill processes" script for modules used in SVP Software
REM                 Copy right. 2017  Hitachi. Ltd.
REM
REM Argument :Waiting time[seconds] for services stop
REM
REM History  :DATE        :Coder       : Patch No
REM HM800    :2017.09.11  :Y.Numada    : #34821 new
REM --------------------------------------------------------------------------

setlocal enabledelayedexpansion

REM ==============================================
REM startup

set APP=%~dp0dllused.exe

set FIND_SERVICE_BAT=%~dp0find_service.bat

set DEFALT_SC_STOP_WAIT_TIME=180

set HTFRONT=HtFront
set HTBACK=HtBack

set FWUPDATE=MAppFirmwareUpdate.exe

set IGNORE=IGNORE_REMOTEFC
set STOP_LOAD=STOP_LOAD_SERVICE
set SKIP_LOAD=SKIP


set TMP_TXT=%~dp0%~n0.tmp
set SC_TXT=%~dp0%~n0_sc.tmp

set INSTALL_KEY=HKEY_LOCAL_MACHINE\SOFTWARE\HITACHI\HM_RAID
set INSTALL_KEY64=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\HITACHI\HM_RAID
set INSTALL_VALUE_NAME=Install_Root

REM ----------------
mkdir %~dp0log > NUL 2>&1

REM ----------------
set mydate=%date:/=%
set mydate=%mydate: =%
set mydate=%mydate::=%
set mydate=%mydate:.=%

set mytime=%time:/=%
set mytime=%mytime: =%
set mytime=%mytime::=%
set mytime=%mytime:.=%

set LOG_FILE=%~dp0log\%~n0_%mydate%_%mytime%.log

REM ----------------
call :ClearTmp



REM ==============================================
REM main

call :Println "Start %0"

REM ----------------
call :CechkExecAdmin
if %ERRORLEVEL% neq 0 (
	call :Println "Error: It is not running as administrator."
	goto :AbnormalEnd
)

REM ----------------
set SC_STOP_WAIT_TIME=%1
if "%SC_STOP_WAIT_TIME%"=="" (
	set SC_STOP_WAIT_TIME=%DEFALT_SC_STOP_WAIT_TIME%
)

if "%2"=="" (
	set TARGET_DIR=Unknown
)
if not "%2"=="" (
	set TARGET_DIR=%2
)

if "%3"=="" (
	set HI_STOP=SKIP
)
if not "%3"=="" (
	set HI_STOP=%3
)

REM ----------------
if !TARGET_DIR! ==Unknown (
	reg query %INSTALL_KEY% /v %INSTALL_VALUE_NAME%> %TMP_TXT%  2>&1
	for /f "delims=" %%A in ('type %TMP_TXT%') do (
		for /f "tokens=1,3" %%I in ("%%A") do (
			set value_name=%%I
			set value=%%J
			if !value_name! ==%INSTALL_VALUE_NAME% (
				set TARGET_DIR=!value!
				goto :escape_loop1
			)
		)
	)
	reg query %INSTALL_KEY64% /v %INSTALL_VALUE_NAME%> %TMP_TXT%  2>&1
	for /f "delims=" %%A in ('type %TMP_TXT%') do (
		for /f "tokens=1,3" %%I in ("%%A") do (
			set value_name=%%I
			set value=%%J
			if !value_name! ==%INSTALL_VALUE_NAME% (
				set TARGET_DIR=!value!
				goto :escape_loop1
			)
		)
	)

:escape_loop1
	if !TARGET_DIR! ==Unknown (
		call :Println "Cannot find SVP Software install directory."
		
		set /P TARGET_DIR="Enter SVP Software install directory : "
		call :Println_log "Enter SVP Software install directory : "
		call :Println "Enter: !TARGET_DIR!"
	)
)

REM ----------------
set PKILLFRAG=N

if !HI_STOP!==SKIP (
	call :Println "This Program will kill processes that using dlls for SVP(!TARGET_DIR!)."
	set  /P PKILLFRAG="Enter(Y):Kill processes, Enter(N):Display just information : "
	call :Println_log "Enter(Y):Kill processes, Enter(N):Display just information : "

	call :Println "'%PKILLFRAG%' has been selected."
) else (
	call :Println "This Program will kill processes that using dlls for SVP(!TARGET_DIR!)."
	set PKILLFRAG=Y
)

REM ----------------
sc queryex > %SC_TXT%

call :Println "=== Check out result ==="

REM ----------------
call :Println "--- Services ---"

set /a exec_num=0
for /f "delims=" %%A in ('%APP% !TARGET_DIR!') do (
	for /f "delims=, tokens=1" %%I in ("%%A") do (
		set pid=%%I
		call %FIND_SERVICE_BAT% !pid! "%SC_TXT%"
		
		if !HI_STOP!==%IGNORE% (
			if !service_name!==%HTFRONT% (
				goto HiTrackChk01
			)
			
			if !service_name!==%HTBACK% (
				goto HiTrackChk01
			)
		)
		
		if not !service_name! ==Unknown (
			call :DiplayOrStopService !pid! "!service_name!"
			set /a exec_num=!exec_num!+1
		)
:HiTrackChk01
		echo off
	)
)

if /i %PKILLFRAG%==Y (
	if /i not !exec_num!==0 (
		echo Wait for %SC_STOP_WAIT_TIME% seconds to stop services...
		timeout /T %SC_STOP_WAIT_TIME%  /NOBREAK
	)
)

REM ----------------
call :Println "--- Processes ---"

for /f "delims=" %%A in ('%APP% !TARGET_DIR!') do (
	for /f "delims=, tokens=1,4" %%I in ("%%A") do (
		set pid=%%I
		set pname=%%J
		
		if !HI_STOP!==%IGNORE% (
			call %FIND_SERVICE_BAT% !pid! "%SC_TXT%"
			if !service_name!==%HTFRONT% (
				goto HiTrackChk02
			)
			
			if !service_name!==%HTBACK% (
				goto HiTrackChk02
			)
			
			echo !pname! | find "%FWUPDATE%" >NUL
			if not ERRORLEVEL 1 (
				goto HiTrackChk02
			)
		)
		call :DiplayOrKillProcess !pid! "!pname!"
:HiTrackChk02
		echo off
	)
)


REM ----------------
:NormalEnd
call :GatheringInfo
call :ClearTmp
call :Println "Normal end  %0"
exit /b 0

REM ==============================================
:Println
set msg=%~1
echo %msg%
echo %msg%>> %LOG_FILE%
exit /b 0

REM ==============================================
:Println_log
set msg=%~1
echo %msg%>> %LOG_FILE%
exit /b 0

REM ==============================================
:CechkExecAdmin
for /f "tokens=1 delims=," %%i in ('whoami /groups /FO CSV /NH') do (
    if "%%~i"=="BUILTIN\Administrators" set ADMIN=yes
    if "%%~i"=="Mandatory Label\High Mandatory Level" set ELEVATED=yes
)
if "%ADMIN%" neq "yes" (
    exit /b 1
)
if "%ELEVATED%" neq "yes" (
    exit /b 1
)
exit /b 0

REM ==============================================
:DiplayOrStopService

set pid=%1
set s_name=%~2

call :Println "pid=%pid%, service=%s_name%"

if /i %PKILLFRAG%==Y (
	echo sc stop "%s_name%" > %TMP_TXT%
	sc stop "%s_name%" >> %TMP_TXT% 2>&1
	type %TMP_TXT%
	type %TMP_TXT% >> %LOG_FILE%
)
exit /b 0


REM ==============================================
:DiplayOrKillProcess
set pid=%1
set pname=%~2

call :Println "pid=%pid%, name=%pname%"

if /i %PKILLFRAG%==Y (
	echo taskkill /F /FI "PID eq %pid%" > %TMP_TXT%
	taskkill /F /FI "PID eq %pid%" >> %TMP_TXT% 2>&1
	type %TMP_TXT%
	type %TMP_TXT% >> %LOG_FILE%
)
exit /b 0


REM ==============================================
:GatheringInfo

call :Println_log "==============="
call :Println "Gathering Enviroment Information after execute..."
call :Println_log "--- Logging 'sc queryex' ---"
sc queryex >> %LOG_FILE%  2>&1

call :Println_log "--- Logging all process and loaded modules ---"
%APP%  >> %LOG_FILE%  2>&1

exit /b 0


REM ==============================================
:ClearTmp

del /F /Q %TMP_TXT% > NUL 2>&1
del /F /Q %SC_TXT% >  NUL 2>&1

exit /b 0


REM ==============================================
:AbnormalEnd
call :ClearTmp
call :Println "Abnormal end  %0"
exit /b 1

