# apply-brightness.ps1 — one-click: install bt-brightness + sudoers via USB/adb. # Usage: plug device, then run apply.cmd param([string]$Adb) $ErrorActionPreference = 'Continue' $Here = Split-Path -Parent $MyInvocation.MyCommand.Path $Scripts = Join-Path $Here 'scripts' function Log($m) { Write-Host "[brightness] $m" -ForegroundColor Green } function Warn($m) { Write-Host "[warn] $m" -ForegroundColor Yellow } function Die($m) { Write-Host "[error] $m" -ForegroundColor Red Read-Host 'Press Enter' exit 1 } function AdbOut([string[]]$ArgList) { (& $Adb @ArgList 2>$null | Out-String) } if (-not $Adb) { $cand = Join-Path $Here 'bin\adb.exe' if (Test-Path $cand) { $Adb = $cand } else { $Adb = 'adb' } } if (-not (Get-Command $Adb -ErrorAction SilentlyContinue)) { Die 'adb not found. Put adb.exe into .\bin\' } foreach ($s in @('bt-brightness', 'buttontask.sudoers', 'install-on-device.sh')) { $p = Join-Path $Scripts $s if (-not (Test-Path $p)) { Die "missing scripts\$s" } } & $Adb start-server 2>$null | Out-Null Write-Host '[brightness] Connect the device via USB...' -ForegroundColor Cyan $n = 0 for ($i = 0; $i -lt 60; $i++) { $n = (@(& $Adb devices) | Select-Object -Skip 1 | Where-Object { $_ -match '\sdevice$' }).Count if ($n -eq 1) { break } Start-Sleep -Seconds 1 } if ($n -ne 1) { Die "need exactly one adb device (found: $n)" } & $Adb root 2>$null | Out-Null & $Adb wait-for-device $uid = (AdbOut @('shell', 'id -u')).Trim() if ($uid -eq '0') { $sudo = '' Log 'adb shell is root' } else { $sudo = 'sudo ' Warn "adb shell uid=$uid - using sudo" } $remote = '/tmp/bt-brightness-apply' Log 'pushing helpers...' & $Adb shell 'rm -rf /tmp/bt-brightness-apply' & $Adb shell 'mkdir -p /tmp/bt-brightness-apply' & $Adb push (Join-Path $Scripts 'bt-brightness') '/tmp/bt-brightness-apply/bt-brightness' | Out-Null & $Adb push (Join-Path $Scripts 'buttontask.sudoers') '/tmp/bt-brightness-apply/buttontask.sudoers' | Out-Null & $Adb push (Join-Path $Scripts 'install-on-device.sh') '/tmp/bt-brightness-apply/install-on-device.sh' | Out-Null $upd = Join-Path $Scripts 'bt-update' if (Test-Path $upd) { & $Adb push $upd '/tmp/bt-brightness-apply/bt-update' | Out-Null } Log 'installing on device...' $cmd = ($sudo + 'bash /tmp/bt-brightness-apply/install-on-device.sh /tmp/bt-brightness-apply').Trim() $output = (& $Adb shell $cmd 2>&1 | Out-String) Write-Host ($output -replace "`r", '') if ($output -notmatch 'BRIGHTNESS-OK') { Die 'install failed (no BRIGHTNESS-OK). See output above.' } & $Adb shell 'rm -rf /tmp/bt-brightness-apply' 2>$null | Out-Null Log 'restarting services...' $restart = ($sudo + 'systemctl restart buttontask-web buttontask').Trim() & $Adb shell $restart 2>&1 | Out-Null Start-Sleep -Seconds 2 Write-Host '' Write-Host ' ========================================' -ForegroundColor Green Write-Host ' DONE. Brightness ready in web / Qt UI.' -ForegroundColor Green Write-Host ' ========================================' -ForegroundColor Green Write-Host '' Read-Host 'Press Enter to exit'