@echo off
setlocal EnableExtensions
if "%~1"=="" (
  echo Usage: oneplus8t-push-b-c-safe.bat SERIAL
  echo Example: oneplus8t-push-b-c-safe.bat e94778f3
  echo Example: oneplus8t-push-b-c-safe.bat 192.168.2.186:38773
  exit /b 1
)

set "SERIAL=%~1"
set "ADB=adb"
set "APK=Settings-kebab-uvc-20261002.apk"

echo === B+C safe push ===
echo.

echo %SERIAL%| findstr /C:":" >nul && (
  "%ADB%" connect %SERIAL%
)

"%ADB%" disconnect
"%ADB%" kill-server
"%ADB%" start-server

"%ADB%" -s "%SERIAL%" root
"%ADB%" -s "%SERIAL%" remount
if errorlevel 1 (
  echo remount failed. Need userdebug. Try USB cable.
  exit /b 1
)

if not exist "libcameraservice.so" goto missing
if not exist "cameraserver" goto missing
if not exist "%APK%" goto missing

echo [B] libcameraservice + cameraserver...
"%ADB%" -s "%SERIAL%" push libcameraservice.so /system/lib64/libcameraservice.so
"%ADB%" -s "%SERIAL%" push cameraserver /system/bin/cameraserver
"%ADB%" -s "%SERIAL%" shell chmod 644 /system/lib64/libcameraservice.so
"%ADB%" -s "%SERIAL%" shell chmod 755 /system/bin/cameraserver
"%ADB%" -s "%SERIAL%" shell restorecon -F /system/lib64/libcameraservice.so
"%ADB%" -s "%SERIAL%" shell restorecon -F /system/bin/cameraserver

echo [C] Settings + delete oat...
"%ADB%" -s "%SERIAL%" push "%APK%" /system_ext/priv-app/Settings/Settings.apk
"%ADB%" -s "%SERIAL%" shell chmod 644 /system_ext/priv-app/Settings/Settings.apk
"%ADB%" -s "%SERIAL%" shell rm -rf /system_ext/priv-app/Settings/oat
"%ADB%" -s "%SERIAL%" shell restorecon -RF /system_ext/priv-app/Settings

echo Restart cameraserver and reboot...
"%ADB%" -s "%SERIAL%" shell setprop ctl.restart cameraserver
"%ADB%" -s "%SERIAL%" reboot

echo Done. After boot check kebab-uvc-20261002 in Settings.
goto end

:missing
echo Missing libcameraservice.so or cameraserver or %APK%
exit /b 1

:end
endlocal
