@echo off
setlocal

set key_path="HKEY_LOCAL_MACHINE\SOFTWARE\BlueStacks_tc_dt"
set value_name="InstallDir"
set temp_file_name=uninst_vm_temp_2015_09_15

set query_str=reg query %key_path% /v %value_name%
set install_dir=""
for /F "delims=;"  %%j in ('%query_str%  2^>NUL') do (
    echo %%j > %temp_file_name%
    for /f "tokens=1,2*" %%x in ('findstr /i %value_name% %temp_file_name%') do (
        set install_dir=%%z
    )
)

if exist "%temp_file_name%" del %temp_file_name%

if "%install_dir%" EQU """" goto :eof

setlocal ENABLEDELAYEDEXPANSION
if "%install_dir%" EQU "" goto :eof

set install_dir=%install_dir:~0,-1%

set errorlevel = 2

set uninstaller_path=%install_dir%HD-DeployTool_Uninstaller.exe
set temp_dir = "%install_dir%..\..\BlueStacks_tc_dt*"

if exist "%uninstaller_path%"  (
    "%uninstaller_path%" -f
    if "%errorlevel%" EQU "0" (
        if exist "%uninstaller_path%"  (  
            rmdir /s /q "%uninstaller_path%\..\..\..\BlueStacks_tc_dt" 
            rmdir /s /q "%uninstaller_path%\..\..\..\BlueStacks_tc_dt1" 
            rmdir /s /q "%uninstaller_path%\..\..\..\BlueStacks_tc_dt2" 
        )
    )

    goto :eof
)

set uninstaller_path="%install_dir%..\uninstaller\HD-DeployTool_Uninstaller.exe"
rem echo uninstaller_path=%uninstaller_path%
%uninstaller_path% -f

rem echo uninstaller exist code is %errorlevel%

set uninstaller_path="%install_dir%.."
rem echo uninstaller_path=%uninstaller_path%

if "%errorlevel%" EQU "0" (
    if exist %uninstaller_path% rmdir /s /q %uninstaller_path%
)
goto:eof
