@echo off
REM TV Clean kit - de-bloat a Fire TV, Android TV or Google TV over Wi-Fi. No root, fully reversible.
REM Copyright (c) 2026 Tyson K. Kreger. All rights reserved.   https://tysonkreger.com/tvclean/
REM
REM Needs: Windows 10 or 11 (curl is built in), adb.exe next to this file or in a platform-tools folder
REM next to it, and the laptop on the same Wi-Fi as the TV. The TV needs ADB debugging ON (see the web page).
REM The de-bloat list is pulled live from the server every run so the kit never goes stale.
REM Every run writes clients\<IP>_<date>\ : full package backup, what we disabled, log.
setlocal enabledelayedexpansion
cd /d "%~dp0"
set SERVER=https://tysonkreger.com/tvclean/api.php

set ADB=
if exist "%~dp0adb.exe" set ADB=%~dp0adb.exe
if not defined ADB if exist "%~dp0platform-tools\adb.exe" set ADB=%~dp0platform-tools\adb.exe
if not defined ADB if exist "%USERPROFILE%\platform-tools\adb.exe" set ADB=%USERPROFILE%\platform-tools\adb.exe
if not defined ADB where adb >nul 2>&1 && set ADB=adb
if not defined ADB (
  echo.
  echo  adb.exe not found. Download platform-tools-latest-windows.zip from
  echo  https://developer.android.com/tools/releases/platform-tools and unzip it next to this file.
  pause & exit /b 1
)

echo.
echo  ==========================================
echo   TV CLEAN  -  your TV, minus the ads
echo  ==========================================
echo.
set /p IP=  TV IP address (TV Settings, About, Network):
if "%IP%"=="" exit /b 1
set TV=%IP%:5555
for /f "tokens=2 delims==" %%a in ('wmic os get localdatetime /value 2^>nul') do set DT=%%a
set D=%DT:~0,8%
if "%D%"=="" set D=run
set CLIENT=clients\%IP%_%D%
if not exist "%CLIENT%" mkdir "%CLIENT%"
set LOG=%CLIENT%\log.txt

echo.
echo   1 = SCAN     read-only, backs up the app list, shows what would change
echo   2 = APPLY    turn off the ads, tracking and nag screens (needs a paid job code)
echo   3 = RESTORE  put back everything APPLY turned off on this TV
echo   4 = STATUS   list what is currently turned off
echo.
set /p MODE=  Choose 1-4:
echo === MODE %MODE% %DATE% %TIME% TV %TV% >> "%LOG%"

"%ADB%" connect %TV% >> "%LOG%" 2>&1
timeout /t 4 /nobreak >nul
"%ADB%" -s %TV% get-state >nul 2>&1
if errorlevel 1 (
  "%ADB%" connect %TV% >> "%LOG%" 2>&1
  timeout /t 6 /nobreak >nul
  "%ADB%" -s %TV% get-state >nul 2>&1
)
if errorlevel 1 (
  echo.
  echo  The TV has not approved this laptop yet.
  echo  LOOK AT THE TV: a box says Allow USB debugging. Check "Always allow" and press OK.
  echo  Then run this again. If no box appears, ADB debugging is not on yet (see the web page).
  pause & exit /b 1
)

for /f "delims=" %%m in ('"%ADB%" -s %TV% shell getprop ro.product.model') do set MODEL=%%m
for /f "delims=" %%m in ('"%ADB%" -s %TV% shell getprop ro.build.version.name') do set OSNAME=%%m
for /f "delims=" %%m in ('"%ADB%" -s %TV% shell getprop ro.product.manufacturer') do set MFR=%%m
set OS=androidtv
echo %OSNAME% | find /i "Fire OS" >nul && set OS=firetv
echo.
echo   TV: %MFR% %MODEL%   OS: %OSNAME%   profile: %OS%
echo MODEL %MODEL% MFR %MFR% OS %OSNAME% PROFILE %OS% >> "%LOG%"

curl -s -f -o "%CLIENT%\list.txt" "%SERVER%?a=list&os=%OS%"
if errorlevel 1 (
  if exist "lists\%OS%.txt" (copy /y "lists\%OS%.txt" "%CLIENT%\list.txt" >nul & echo   (offline, using the local list copy)) else (
    echo  Could not download the list and no local copy found. Check internet. & pause & exit /b 1)
)

"%ADB%" -s %TV% shell pm list packages > "%CLIENT%\packages-before.txt"
for /f %%c in ('find /c "package:" ^< "%CLIENT%\packages-before.txt"') do set TOTAL=%%c
set N=0
for /f "usebackq eol=# tokens=1" %%P in ("%CLIENT%\list.txt") do (
  findstr /x /c:"package:%%P" "%CLIENT%\packages-before.txt" >nul && set /a N+=1
)

if "%MODE%"=="1" goto scan
if "%MODE%"=="2" goto apply
if "%MODE%"=="3" goto restore
if "%MODE%"=="4" goto status
exit /b 1

:scan
"%ADB%" -s %TV% shell pm list packages -d > "%CLIENT%\disabled-before.txt"
echo.
echo   %TOTAL% apps and services on this TV. %N% of them are ad, tracking or nag packages that APPLY will turn off.
echo   Backup saved: %CLIENT%\packages-before.txt
call :report scan 0
pause & exit /b 0

:apply
echo.
set /p CODE=  Job code from your receipt (TV-XXXXXX):
set PAID=0
if exist "%~dp0owner.key" set PAID=1
if "%PAID%"=="0" (
  curl -s -f "%SERVER%?a=job&code=%CODE%" > "%CLIENT%\job.json" 2>nul
  findstr /c:"\"paid\":true" "%CLIENT%\job.json" >nul && set PAID=1
)
if "%PAID%"=="0" (
  echo.
  echo  That code is not paid yet or was typed wrong. Pay at https://tysonkreger.com/tvclean/ and use the code on the receipt.
  echo  SCAN and RESTORE always work without a code.
  pause & exit /b 1
)
echo.
echo   Turning off %N% packages (each one is logged and reversible)...
for /f "usebackq eol=# tokens=1" %%P in ("%CLIENT%\list.txt") do (
  findstr /x /c:"package:%%P" "%CLIENT%\packages-before.txt" >nul
  if !errorlevel! == 0 (
    echo     %%P
    "%ADB%" -s %TV% shell pm disable-user --user 0 %%P >> "%LOG%" 2>&1
    echo %%P >> "%CLIENT%\disabled-by-us.txt"
  )
)
"%ADB%" -s %TV% shell settings put global window_animation_scale 0.5 >> "%LOG%" 2>&1
"%ADB%" -s %TV% shell settings put global transition_animation_scale 0.5 >> "%LOG%" 2>&1
"%ADB%" -s %TV% shell settings put global animator_duration_scale 0.5 >> "%LOG%" 2>&1
"%ADB%" -s %TV% shell pm list packages -d > "%CLIENT%\disabled-after.txt"
for /f %%c in ('find /c "package:" ^< "%CLIENT%\disabled-after.txt"') do set DN=%%c
echo.
echo   DONE. %DN% packages are now off. Restart the TV once: hold the remote power button, choose Restart.
call :report apply %DN%
pause & exit /b 0

:restore
set SRC=
for /d %%f in (clients\%IP%_*) do if exist "%%f\disabled-by-us.txt" set SRC=%%f\disabled-by-us.txt
if not defined SRC (
  echo   No record of what this kit turned off on %IP%. Nothing to restore.
  pause & exit /b 1
)
echo   Restoring from %SRC%
for /f "usebackq eol=# tokens=1" %%P in ("%SRC%") do (
  echo     %%P
  "%ADB%" -s %TV% shell pm enable --user 0 %%P >> "%LOG%" 2>&1
)
"%ADB%" -s %TV% shell settings put global window_animation_scale 1.0 >> "%LOG%" 2>&1
"%ADB%" -s %TV% shell settings put global transition_animation_scale 1.0 >> "%LOG%" 2>&1
"%ADB%" -s %TV% shell settings put global animator_duration_scale 1.0 >> "%LOG%" 2>&1
echo   Restored.
call :report restore 0
pause & exit /b 0

:status
"%ADB%" -s %TV% shell pm list packages -d
pause & exit /b 0

:report
set RMODE=%1
set RDN=%2
> "%CLIENT%\report.json" echo {"code":"%CODE%","mode":"%RMODE%","model":"%MODEL%","mfr":"%MFR%","os":"%OSNAME%","total":%TOTAL%,"matched":%N%,"disabled_after":%RDN%,"ip":"%IP%"}
curl -s -o nul -X POST -H "Content-Type: application/json" --data-binary "@%CLIENT%\report.json" "%SERVER%?a=report" 2>nul
exit /b 0
