@echo off

setlocal EnableDelayedExpansion

REM Uninstall this project's OHOS app from all hdc-connected devices.

REM bundleName from harmonyApp\AppScope\app.json5 unless -b overrides.

REM After uninstall, local entry build / HAP / intermediates are removed by default.
REM -c: no-device mode — still clean local dirs; uninstalls only if hdc and devices exist.

pushd "%~dp0"

set "SCRIPT_DIR=%CD%"

set "APP_JSON5=%SCRIPT_DIR%\harmonyApp\AppScope\app.json5"

set "BUNDLE_NAME="

set "CLEAN=0"

:parse

if "%~1"=="" goto done_parse

if /i "%~1"=="-c" (set "CLEAN=1" & shift & goto parse)

if /i "%~1"=="-j" (set "APP_JSON5=%~2" & shift & shift & goto parse)

if /i "%~1"=="-b" (set "BUNDLE_NAME=%~2" & shift & shift & goto parse)

if /i "%~1"=="-h" goto show_help

echo Unknown option: %~1

goto show_help

:done_parse

if "!BUNDLE_NAME!"=="" (

  if not exist "!APP_JSON5!" (

    echo Error: app.json5 not found: !APP_JSON5!

    popd

    exit /b 1

  )

  for /f "usebackq tokens=4 delims=^"" %%V in (`findstr /l /c:"bundleName" "!APP_JSON5!"`) do (

    set "BUNDLE_NAME=%%V"

    set "BUNDLE_NAME=!BUNDLE_NAME:,=!"

  )

)

if "!BUNDLE_NAME!"=="" (

  echo Error: could not parse bundleName from !APP_JSON5!

  popd

  exit /b 1

)

if "!CLEAN!"=="1" goto do_clean_combo

if "!DEVECO_PATH!"=="" (

  if exist "C:\Program Files\Huawei\DevEco Studio" (

    set "DEVECO_PATH=C:\Program Files\Huawei\DevEco Studio"

  ) else if exist "D:\Program Files\Huawei\DevEco Studio" (

    set "DEVECO_PATH=D:\Program Files\Huawei\DevEco Studio"

  ) else (

    echo Error: DevEco Studio not found. Set DEVECO_PATH.

    popd

    exit /b 1

  )

)

set "HDC_BIN=!DEVECO_PATH!\sdk\default\openharmony\toolchains\hdc.exe"

if not exist "!HDC_BIN!" (

  echo Error: hdc not found at !HDC_BIN!

  popd

  exit /b 1

)

echo Scanning devices ^(hdc list targets^)...

set "DEV_FILE=%TEMP%\uninstall_ohos_dev_%RANDOM%.txt"

del "!DEV_FILE!" 2>nul

for /f "tokens=1*" %%a in ('"!HDC_BIN!" list targets 2^>nul') do (

  if not "%%a"=="" echo %%a >>"!DEV_FILE!"

)

if not exist "!DEV_FILE!" (

  echo Error: no device found. Connect device and run: hdc list targets

  popd

  exit /b 1

)

for %%F in ("!DEV_FILE!") do if %%~zF equ 0 (

  echo Error: no device found. Connect device and run: hdc list targets

  del "!DEV_FILE!" 2>nul

  popd

  exit /b 1

)

echo Bundle: !BUNDLE_NAME!

echo Targets:

type "!DEV_FILE!"

echo ------------------------------------------------------------

for /f "usebackq delims=" %%T in ("!DEV_FILE!") do (

  set "TID=%%T"

  set "TID=!TID: =!"

  if not "!TID!"=="" call :uninstall_one "!TID!"

)

del "!DEV_FILE!" 2>nul

echo Cleaning local harmonyApp\entry artifacts...

call :clean_entry

echo ------------------------------------------------------------

echo Done. Uninstall attempted for bundle !BUNDLE_NAME!

popd

exit /b 0

:do_clean_combo

if "!DEVECO_PATH!"=="" (

  if exist "C:\Program Files\Huawei\DevEco Studio" (

    set "DEVECO_PATH=C:\Program Files\Huawei\DevEco Studio"

  ) else if exist "D:\Program Files\Huawei\DevEco Studio" (

    set "DEVECO_PATH=D:\Program Files\Huawei\DevEco Studio"

  )

)

set "HDC_BIN="

if not "!DEVECO_PATH!"=="" (

  set "HDC_BIN=!DEVECO_PATH!\sdk\default\openharmony\toolchains\hdc.exe"

)

if exist "!HDC_BIN!" (

  set "DEV_FILE=%TEMP%\uninstall_ohos_dev_%RANDOM%.txt"

  del "!DEV_FILE!" 2>nul

  for /f "tokens=1*" %%a in ('"!HDC_BIN!" list targets 2^>nul') do (

    if not "%%a"=="" echo %%a >>"!DEV_FILE!"

  )

  set "HAS_DEV=0"

  if exist "!DEV_FILE!" for %%F in ("!DEV_FILE!") do if %%~zF gtr 0 set "HAS_DEV=1"

  if "!HAS_DEV!"=="1" (

    echo Scanning devices ^(hdc list targets^)...

    echo Bundle: !BUNDLE_NAME!

    echo Targets:

    type "!DEV_FILE!"

    echo ------------------------------------------------------------

    for /f "usebackq delims=" %%T in ("!DEV_FILE!") do (

      set "TID=%%T"

      set "TID=!TID: =!"

      if not "!TID!"=="" call :uninstall_one "!TID!"

    )

    echo ------------------------------------------------------------

    echo Uninstall attempted for bundle !BUNDLE_NAME!

  ) else (

    echo No connected device; skip uninstall.

  )

  del "!DEV_FILE!" 2>nul

) else (

  echo hdc not available; skip uninstall.

)

echo Cleaning local harmonyApp\entry artifacts...

call :clean_entry

echo Done.

popd

exit /b 0

:clean_entry

set "ENTRY_BASE=!SCRIPT_DIR!\harmonyApp\entry"

if not exist "!ENTRY_BASE!" (

  echo Warning: entry directory not found: !ENTRY_BASE!

  exit /b 0

)

if exist "!ENTRY_BASE!\builds" rmdir /s /q "!ENTRY_BASE!\builds" 2>nul

if exist "!ENTRY_BASE!\build" rmdir /s /q "!ENTRY_BASE!\build" 2>nul

if exist "!ENTRY_BASE!\.cxx" rmdir /s /q "!ENTRY_BASE!\.cxx" 2>nul

if exist "!ENTRY_BASE!\libs\" (
  for /r "!ENTRY_BASE!\libs" %%F in (*.so) do if exist "%%F" del /f /q "%%F" 2>nul
)

if exist "!ENTRY_BASE!\libs\arm64-v8a" rmdir /s /q "!ENTRY_BASE!\libs\arm64-v8a" 2>nul

if exist "!ENTRY_BASE!\libs\x86_64" rmdir /s /q "!ENTRY_BASE!\libs\x86_64" 2>nul

echo Local clean: entry\build, builds, .cxx, all *.so under libs, libs\arm64-v8a, libs\x86_64

exit /b 0

:uninstall_one

set "TARGET_ID=%~1"

echo [!TARGET_ID!] aa force-stop ^(ignore errors^)

"!HDC_BIN!" -t !TARGET_ID! shell aa force-stop !BUNDLE_NAME! 2>nul

echo [!TARGET_ID!] bm uninstall -n !BUNDLE_NAME!

"!HDC_BIN!" -t !TARGET_ID! shell bm uninstall -n !BUNDLE_NAME!

exit /b 0

:show_help

echo Usage: %~nx0 [options]

echo   -c        No-device mode: clean local entry build; uninstall if devices exist

echo   -j PATH   Path to app.json5 ^(default: harmonyApp\AppScope\app.json5^)

echo   -b NAME   Override bundle name

echo   -h        Show help

popd

exit /b 0

