@echo off
REM set DBG=echo

REM  Hitachi SVP                              
REM  Tomcat Uninstall tool for Hitachi SVP
REM       All Rights Reserved, Copyright (C) 2016,Hitachi, Ltd. 
REM
REM History  :Version    :DATE        : Coder        : Patch No
REM Hitachi SVP 
REM          :01-00      :2016.07.15  : 
REM          :01-01      :2018.02.16  : Y.Kumagai   :  #44854 (Remote Micro Exchange Support)
REM          :01-02      :2018.11.12  : Y.Kawaguchi :  #62575 (Remote Micro Exchange with Java11)
REM          :01-03      :2018.11.19  : Y.Kawaguchi :  #62935
REM
REM Document
REM Apache Tomcat Uninstaller
REM
REM 


set LOGFILE="c:\SN2Setup_log.txt"
set UNINSTTOMCAT="C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0"

set HKEYSOFTWAREADDR=^
"HKEY_LOCAL_MACHINE\SOFTWARE\^
Apache Software Foundation\^
Tomcat\7.0\Tomcat7"

echo ----- >> %LOGFILE% 2>>&1
echo # Start Program >> %LOGFILE% 2>>&1
echo %date%-%time% >> %LOGFILE% 2>>&1
echo ----- >> %LOGFILE% 2>>&1


REM whoami /group
for /f "tokens=1 delims=," %%i in ('whoami /groups /FO CSV /NH') do (
	if "%%~i"=="BUILTIN\Administrators" set ADMINISTRATOR=yes
	if "%%~i"=="Mandatory Label\High Mandatory Level" set ELEVATED=yes
	if "%%~i"=="Mandatory Label\System Mandatory Level" set ELEVATED=yes
)

REM Administrator user check
if "%ADMINISTRATOR%" neq "yes" (
	echo Not the Administrator user. Run the Administrator user
	pause
	exit 1
)

REM Elevation check
if "%ELEVATED%" neq "yes" (
	echo Must be "run as Administrator"
	pause
	exit 1
)


REM services stop
call C:\DKC200\restapi\uninstall.bat >> %LOGFILE% 2>>&1
echo "error:%errorlevel%" >> %LOGFILE% 2>>&1

sc.exe query SVPRestAPIServer >> %LOGFILE% 2>>&1
if %ERRORLEVEL% == 0 (
echo SVPRestAPIServer can not uninstall >> %LOGFILE% 2>>&1
echo SVPRestAPIServer can not uninstall
goto :error
 ) 

net stop "SVPAccountManager" >> %LOGFILE% 2>>&1
SET SVPAM_STOP_FLG=0
  for /f "tokens=1" %%i in ('tasklist /fi "IMAGENAME eq SVPAccountService.exe"') do (
    IF "%%i"=="SVPAccountService.exe" (
      SET SVPAM_STOP_FLG=1
    )
  )

  IF %SVPAM_STOP_FLG%==1 (
	pushd %~dp0
  	echo SVPAccountManager can not stop >> %LOGFILE% 2>>&1
  	echo SVPAccountManager can not stop
	goto :error
  )

sc stop tomcat7 >> %LOGFILE% 2>>&1

timeout /T 60 >> %LOGFILE% 2>>&1

 SET TOMCAT_STOP_FLG=0
  for /f "tokens=1" %%i in ('tasklist /fi "IMAGENAME eq Tomcat7.exe"') do (
    IF "%%i"=="Tomcat7.exe" (
      SET TOMCAT_STOP_FLG=1
    )
  )
  
  IF %TOMCAT_STOP_FLG%==1 (
	pushd %~dp0
  	echo Tomcat can not stop >> %LOGFILE% 2>>&1
  	echo Tomcat can not stop
	goto :error
  )

REM services stop END

if "%1"=="Remote" (
	call %UNINSTTOMCAT%\Uninstall.exe  /S -ServiceName="Tomcat7"
) else (
	call %UNINSTTOMCAT%\Uninstall.exe -ServiceName="Tomcat7"
)

REG DELETE %HKEYSOFTWAREADDR% /reg:64 /f
REG DELETE %HKEYSOFTWAREADDR% /reg:32 /f

echo ----- >> %LOGFILE% 2>>&1
echo # End Program >> %LOGFILE% 2>>&1
echo %date%-%time% >> %LOGFILE% 2>>&1
echo ----- >> %LOGFILE% 2>>&1


exit 0

:error
echo Error occured.
exit 240
