@echo off
rem ===========================================================================
rem  SMSBlaster - one-click phone installer for Windows (hosted tenants).
rem
rem  This sideloads the SMSBlaster Android app onto a USB-connected phone. It is
rem  a standalone replacement for the desktop app's built-in installer, for
rem  customers who use the hosted web edition and have no Electron app.
rem
rem  Pairing is NOT done here: after the app installs, the operator pairs the
rem  phone from the website (Devices -> Pair a phone -> scan the QR).
rem
rem  Requires Windows 10 1803+ (ships curl.exe and tar.exe in System32). No
rem  admin rights and no separate "install adb" step: Google's platform-tools
rem  are downloaded into %TEMP% on first run. https://app.smsblaster.verilexdata.com is substituted by
rem  the server when it serves this file, so the APK comes from the same server.
rem ===========================================================================
setlocal enabledelayedexpansion
title SMSBlaster - Install app on phone
color 0b

set "SERVER_URL=https://app.smsblaster.verilexdata.com"
set "PKG=net.performancewest.smsblaster"
set "WORK=%TEMP%\smsblaster-installer"
set "PTZIP=%WORK%\platform-tools.zip"
set "PTDIR=%WORK%\platform-tools"
set "APK=%WORK%\smsblaster.apk"
set "ADB=%PTDIR%\adb.exe"
set "LOG=%WORK%\install.log"

echo.
echo  ============================================================
echo     SMSBlaster - Install the app on your phone (over USB)
echo  ============================================================
echo.
echo   Server: %SERVER_URL%
echo.
echo   Before you start, on the phone:
echo     1. Settings - About phone - tap "Build number" 7 times
echo        to unlock Developer options.
echo     2. Settings - System - Developer options - turn on
echo        "USB debugging".
echo     3. Plug the phone into this PC with a USB cable.
echo.
echo  ------------------------------------------------------------
echo.

rem --- prerequisites (Windows 10 1803+ ships these) --------------------------
where curl.exe >nul 2>&1
if errorlevel 1 (
  echo  [X] This installer needs Windows 10 version 1803 or newer.
  echo      ^(curl.exe was not found.^)
  goto :fail
)
where tar.exe >nul 2>&1
if errorlevel 1 (
  echo  [X] This installer needs Windows 10 version 1803 or newer.
  echo      ^(tar.exe was not found.^)
  goto :fail
)

if not exist "%WORK%" mkdir "%WORK%"

rem --- adb: prefer one already on PATH, else download platform-tools ----------
set "SYSADB="
for /f "delims=" %%I in ('where adb.exe 2^>nul') do if not defined SYSADB set "SYSADB=%%I"
if defined SYSADB (
  set "ADB=%SYSADB%"
  echo  [1/4] Using adb already installed on this PC.
) else if exist "%ADB%" (
  echo  [1/4] Phone tools already downloaded.
) else (
  echo  [1/4] Downloading phone tools ^(adb^)...
  curl.exe -L --fail --progress-bar -o "%PTZIP%" "https://dl.google.com/android/repository/platform-tools-latest-windows.zip"
  if errorlevel 1 (
    echo  [X] Could not download phone tools. Check your internet connection.
    goto :fail
  )
  echo        Extracting...
  tar.exe -xf "%PTZIP%" -C "%WORK%"
  if errorlevel 1 (
    echo  [X] Could not extract phone tools.
    goto :fail
  )
  del /q "%PTZIP%" >nul 2>&1
  if not exist "%ADB%" (
    echo  [X] adb.exe missing after extract.
    goto :fail
  )
)

rem --- download the APK from the same server ---------------------------------
echo  [2/4] Downloading the SMSBlaster app...
curl.exe -L --fail --progress-bar -o "%APK%" "%SERVER_URL%/api/app/apk"
if errorlevel 1 (
  echo  [X] Could not download the app from %SERVER_URL%.
  echo      Is the server URL correct and reachable from this PC?
  goto :fail
)
rem sanity-check the size (a real APK is multiple MB; an error page is tiny).
for %%A in ("%APK%") do set "APKSIZE=%%~zA"
if not defined APKSIZE set "APKSIZE=0"
if %APKSIZE% LSS 1000000 (
  echo  [X] The downloaded app looks wrong ^(%APKSIZE% bytes^).
  echo      The server may not have an APK published yet.
  goto :fail
)

rem --- find a connected, authorized phone -------------------------------------
echo  [3/4] Looking for your phone...
"%ADB%" start-server >nul 2>&1
set /a TRIES=0
:waitdev
set "SERIAL="
set "STATE="
for /f "skip=1 tokens=1,2" %%A in ('"%ADB%" devices 2^>nul') do (
  if not "%%A"=="" (
    set "SERIAL=%%A"
    set "STATE=%%B"
  )
)
if "%STATE%"=="device" goto :install
if "%STATE%"=="unauthorized" (
  echo        ^>^> Look at the phone and tap "Allow" / "Always allow" on the
  echo           USB debugging prompt, then keep waiting...
) else if "%STATE%"=="offline" (
  echo        Phone is reconnecting...
) else (
  echo        No phone detected yet. Connect it via USB with USB debugging on.
)
set /a TRIES+=1
if %TRIES% GEQ 90 (
  echo.
  echo  [X] Gave up waiting for a phone after 3 minutes.
  goto :fail
)
timeout /t 2 >nul
goto :waitdev

rem --- install + launch -------------------------------------------------------
:install
echo        Found phone: %SERIAL%
echo  [4/4] Installing the app...
"%ADB%" -s %SERIAL% install -r -d "%APK%" > "%LOG%" 2>&1
type "%LOG%"
find /i "Success" "%LOG%" >nul
if errorlevel 1 (
  echo.
  echo  [X] Install did not report success ^(see the message above^).
  goto :fail
)
rem best-effort launch; ignore failure.
"%ADB%" -s %SERIAL% shell monkey -p %PKG% -c android.intent.category.LAUNCHER 1 >nul 2>&1

echo.
echo  ============================================================
echo    [OK] Installed!
echo.
echo    Next: open SMSBlaster on the phone, then pair it from the
echo    website ^(Devices - Pair a phone - scan the QR code^).
echo  ============================================================
echo.
goto :done

:fail
echo.
echo  Installation did not complete. You can close this window and try again.
echo.

:done
endlocal
echo  Press any key to close this window.
pause >nul
